#lang racket ;; Change as appropriate for your OS (require "firmata-linux.rkt") ;; the PINs (define input1 3) (define green 8) (define orange 9) (define red 10) ;; The sequence of lights (define lightSeq (list (list green) (list orange) (list red) (list red orange))) ;; The interval between transitions, in milliseconds (define interval 1000) ;; The initial state of the switch (define curState "UP") ;; We want to monitor changes in the position: we store the old value (define previousState "UP") ;; We store the time in milliseconds when we start ;; (and the update this at the end of the loop) (define previousTime (current-inexact-milliseconds)) ;; We will use this to store the current time (define currentTime 0) ;; Just setting up things (define (setup) (report-digital-port! 0 1) (report-digital-port! input1 1) (set-pin-mode! input1 INPUT_MODE) (set-pin-mode! green OUTPUT_MODE) (set-pin-mode! orange OUTPUT_MODE) (set-pin-mode! red OUTPUT_MODE) ) ;; we simply keep looping (define (goTo) (set! currentTime (current-inexact-milliseconds)) ;; If the last update to traffic lights was > interval... (cond ( (> (- currentTime previousTime) interval) ;; If the last update to traffic lights was > interval... ;; we clear all the pins (clear-arduino-pin! red) (clear-arduino-pin! orange) (clear-arduino-pin! green) ;; We set the appropriate pins (map (λ (i) (set-arduino-pin! i)) (first lightSeq)) ;; And then we do a cycle of traffic lights (set! lightSeq (append (rest lightSeq) (list (first lightSeq)))) ;; Finally, we store the time of this update (set! previousTime (current-inexact-milliseconds)) ) ) ;; if we have a signal, the switch is pressed (cond ((is-arduino-pin-set? input1) (set! curState "DOWN") ) (else (set! curState "UP")) ) ;; The button is released when the previous state was DOWN and current state is UP: (cond ( (and (equal? curState "UP") (equal? previousState "DOWN")) (printf "The button was released\n"))) ;; We just save the current value of the button (set! previousState curState) (goTo) ) (define (test) (open-firmata "/dev/ttyACM0") (setup) (goTo) )