Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- #define btnEncodeOK 6
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- int lcd_key = 0;
- int adc_key_in = 0;
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0);
- if (adc_key_in > 1000) return btnNONE;
- if (adc_key_in < 50) return btnLEFT;
- if (adc_key_in < 150) return btnUP;
- if (adc_key_in < 250) return btnRIGHT;
- if (adc_key_in < 450) return btnSELECT;
- if (adc_key_in < 700) return btnDOWN;
- if (adc_key_in < 850) return btnEncodeOK;
- return btnNONE; // when all others fail, return this...
- }
- // Pin 13 has an LED connected on most Arduino boards.
- // give it a name:
- int button;
- unsigned long time_left;
- unsigned long t;
- unsigned long last_time;
- unsigned long delta;
- int VERDE = 0, ROJO = 1, AMBAR = 2;
- int on = VERDE;
- int on_peaton = VERDE;
- int semaforo[3];
- int semaforo_peaton[2];
- unsigned long MAX_ROJO = 10000; //Time the car's traffic light is red
- unsigned long MAX_AMBAR = 5000; //Time the car's traffic light is yellow
- unsigned long GREEN_LOCK = 1000; //Time the car's traffic light is green
- void setup() {
- // Setup LCD display
- lcd.begin(16, 2);
- pinMode(10,OUTPUT);
- digitalWrite(10, 1);
- lcd.setCursor(0,0);
- // Set pins for each LED
- semaforo[VERDE] = 13;
- semaforo[AMBAR] = 12;
- semaforo[ROJO] = 11;
- semaforo_peaton[VERDE] = A1;
- semaforo_peaton[ROJO] = A2;
- pinMode(semaforo[VERDE], OUTPUT);
- pinMode(semaforo[AMBAR], OUTPUT);
- pinMode(semaforo[ROJO], OUTPUT);
- pinMode(semaforo_peaton[VERDE], OUTPUT);
- pinMode(semaforo_peaton[ROJO], OUTPUT);
- on = VERDE;
- on_peaton = ROJO;
- //Serial.begin(9600); //debug
- lcd.setCursor(0,0); // move cursor to the first line and first column
- lcd.print("CAR"); // display "CAR" on the LCD screen
- lcd.setCursor(13,0); // move cursor to the first line and last column
- lcd.print("PED"); // display "PED" on the LCD screen
- }
- // the loop routine runs over and over again forever:
- void loop() {
- delta = millis() - last_time; // calculate time between frames
- last_time = millis();
- t += delta;
- lcd_key = read_LCD_buttons(); // read the buttons
- button = lcd_key;
- if (on == AMBAR && t >= MAX_AMBAR) { // set semaphore to red if the time in ambar has passed
- digitalWrite(semaforo[on], LOW); // and set the pedestrian semaphore to green
- digitalWrite(semaforo_peaton[on_peaton], LOW);
- on = ROJO;
- on_peaton = VERDE;
- t = 0;
- time_left = MAX_ROJO + 1000; // add one second to the time left because division truncates
- } else if (on == ROJO && t >= MAX_ROJO) { // set semaphore to green if the time in red has passed
- digitalWrite(semaforo[on], LOW); // and set the pedestrian semaphore to red
- digitalWrite(semaforo_peaton[on_peaton], LOW);
- on = VERDE;
- on_peaton = ROJO;
- t = 0;
- } else if (button != 5 && on == VERDE && t >= GREEN_LOCK) { // set semaphore to amber if the time in green has passed
- digitalWrite(semaforo[on], LOW); // and set the pedestrian semaphore to red
- digitalWrite(semaforo_peaton[on_peaton], LOW);
- on = AMBAR;
- on_peaton = ROJO;
- t = 0;
- }
- lcd.setCursor(0,1); // move cursor to second line "1" and 9 spaces over
- lcd.print(" ");
- if (on == ROJO) {
- time_left = time_left - delta;
- unsigned long a = time_left / 1000;
- //Serial.println(a); //debug
- lcd.setCursor(1,1);
- lcd.print((MAX_ROJO/1000) - a);
- lcd.setCursor(14,1);
- lcd.print(a);
- }
- digitalWrite(semaforo[on], HIGH); // set the corresponding pin to on depending of the color
- digitalWrite(semaforo_peaton[on_peaton], HIGH); // set the corresponding pin to on depending of the color
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement