Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Stop Light
- Simulates a stop light, Parallel project to my BASIC Stamp 2 Project
- */
- // pin 9 for green
- // pin 10 for yellow
- // pin 11 for red
- #define green 9
- #define yellow 10
- #define red 11
- // pin 6 for button input
- #define button 6;
- // walk status
- boolean walk = false;
- void setLight(byte color){
- digitalWrite(red,LOW);
- digitalWrite(yellow,LOW);
- digitalWrite(green,LOW);
- switch (color) {
- case red:
- digitalWrite(red,HIGH);
- break;
- case yellow:
- digitalWrite(yellow,HIGH);
- break;
- case green:
- digitalWrite(green,HIGH);
- break;
- }
- }
- // setup
- void setup() {
- // Initialize pins
- pinMode(green, OUTPUT);
- pinMode(yellow, OUTPUT);
- pinMode(red, OUTPUT);
- pinMode(button, INPUT);
- //debugging stuff
- Serial.begin(9600);
- }
- // main loop
- void loop() {
- // read the value of the switch
- // digitalRead() checks to see if there is a voltage
- // on the pin or not
- setLight(green);
- for (int i = 0; i <= 2000; i++) {
- if (!walk) {
- walk = digitalRead(button);
- delay(1);
- } else {
- delay(2000 - i);
- i = 2000;
- }
- }
- setLight(yellow);
- for (i=0; i <= 1000; i++) {
- if (!walk) {
- walk = digitalRead(button);
- delay(1);
- } else {
- delay(1000-i);
- i = 1000;
- }
- }
- setLight(red);
- for (i=0; i <= 2500; i++) {
- if (!walk) {
- walk = digitalRead(button);
- delay(1);
- } else {
- delay(2500-i);
- i = 2500;
- }
- }
- if (walk) {
- delay(5000);
- }
- walk = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment