perjantai 27. marraskuuta 2015

Programming Arduino UNO with Racket

Some weeks ago I found out about this Arduino-RaspberryPi robot that you can program using Racket language and DrRacket. I contacted the creators and got some Arduino boards and accessories to play with.

Normally you would program Arduino using C/C++ and their own development environment. Since I have chosen to use Racket for teaching programming in my math class and other computer science classes, to make it easier to me and my students I would prefer to use Racket also in my physics class. And thanks to Mr Raimondi and Mr Margolis now I can!

In addition to DrRacket installation you need to install Arduino UNO USB driver to your laptop. It comes with the Arduino IDE. Arduino UNO needs to have the ASIP firmware installed, also you need to include one Racket - file "AsipMain.rkt" and you are good to go.

Here are our first two working demos:

First one blinks two leds:

(require "AsipMain.rkt")
(define LED 13)
(define LED2 12)


(set-pin-mode! LED OUTPUT_MODE)
(set-pin-mode! LED2 OUTPUT_MODE)

(define (my-program)
    (digital-write LED HIGH)
    (sleep 0.2)
    (digital-write LED LOW)
    (digital-write LED2 HIGH)
    (sleep 0.2)
    (digital-write LED2 LOW)


And in the second one the led is controlled from the laptop's keyboard (spacebar):

(require "AsipMain.rkt")
(require 2htdp/universe)
(require 2htdp/image)

(define LED 13)
(set-pin-mode! LED OUTPUT_MODE)

(define (draw-image y)

(define (key1 y n)
  (if (key=? n " ")

      (begin (digital-write LED HIGH)

(define (key2 y n)
  (if (key=? n " ")

      (begin (digital-write LED LOW)

(big-bang #f
          (on-key key1)
          (on-release key2)
          (to-draw draw-image))

One of my students, 13 year old Lauri wrote a big part of this code and connected the wires for the demo. Thanks! We will develop this further in the coming weeks. I'm planning to use this to teach physics and programming this spring.