Created
January 16, 2015 04:54
-
-
Save Dante83/c46f56f7e302d6018e30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Program: Traffic Light Simulator | |
//Programmer: David Evans | |
//Program Date: 01/15/15 | |
//Language: Node.js | |
//This is the node.js version of the program, which is basically a simple traffic light... makes me wonder why traffic lights | |
//in reality are so damn expensive >_>. I already knew I could pull this off with Node.JS via the Cloud 9 IDE that comes with | |
//my beaglebone, but I just wanted something to test that everything was in running order so that the only isolated point of | |
//failure for my python code would be my python... not my setup. Iterative development with electronics... tasty! | |
var b = require('bonescript'); | |
var green_light = "P8_13"; | |
var yellow_light = "P8_15"; | |
var red_light = "P8_17"; | |
b.pinMode(green_light, 'out'); | |
b.pinMode(yellow_light, 'out'); | |
b.pinMode(red_light, 'out'); | |
var time = 0; | |
light_color = 'none' | |
runLights = function(){ | |
time += 1 | |
time = time % 33 | |
if(time < 15 && light_color != 'green'){ | |
b.digitalWrite(red_light, 0); | |
b.digitalWrite(yellow_light, 0); | |
b.digitalWrite(green_light, 1); | |
console.log('green light!'); | |
light_color = 'green' | |
} | |
else if((time >= 15) && (time <= 18) && light_color != 'yellow'){ | |
b.digitalWrite(green_light, 0); | |
b.digitalWrite(yellow_light, 1); | |
console.log('yellow light!'); | |
light_color = 'yellow' | |
} | |
else if (time > 18 && light_color != 'red'){ | |
b.digitalWrite(yellow_light, 0); | |
b.digitalWrite(red_light, 1); | |
console.log('red light!'); | |
light_color = 'red' | |
} | |
} | |
timer = setInterval(runLights, 1000); | |
stopTimer = function() { | |
clearInterval(timer); | |
b.digitalWrite(green_light, 0); | |
b.digitalWrite(yellow_light, 0); | |
b.digitalWrite(red_light, 0); | |
}; | |
setTimeout(stopTimer, 450000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment