SHARE
TWEET

Untitled

a guest May 2nd, 2018 463 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>  
  2. #define btnRIGHT  0
  3. #define btnUP     1
  4. #define btnDOWN   2
  5. #define btnLEFT   3
  6. #define btnSELECT 4
  7. #define btnNONE   5  
  8. #define btnEncodeOK  6
  9.  
  10. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  
  11.  
  12. int lcd_key     = 0;
  13. int adc_key_in  = 0;
  14.  
  15. int read_LCD_buttons()
  16. {  
  17.     adc_key_in = analogRead(0);  
  18.     if (adc_key_in > 1000) return btnNONE;
  19.     if (adc_key_in < 50)   return btnLEFT;  
  20.     if (adc_key_in < 150)  return btnUP;  
  21.     if (adc_key_in < 250)  return btnRIGHT;  
  22.     if (adc_key_in < 450)  return btnSELECT;  
  23.     if (adc_key_in < 700)  return btnDOWN;    
  24.     if (adc_key_in < 850)  return btnEncodeOK;
  25.     return btnNONE;  // when all others fail, return this...
  26. }  
  27. // Pin 13 has an LED connected on most Arduino boards.
  28. // give it a name:
  29. int button;
  30. unsigned long time_left;
  31. unsigned long t;
  32. unsigned long last_time;
  33. unsigned long delta;
  34. int VERDE = 0, ROJO = 1, AMBAR = 2;
  35. int on = VERDE;
  36. int on_peaton = VERDE;
  37. int semaforo[3];
  38. int semaforo_peaton[2];
  39. unsigned long MAX_ROJO = 10000;  //Time the car's traffic light is red
  40. unsigned long MAX_AMBAR = 5000;  //Time the car's traffic light is yellow
  41. unsigned long GREEN_LOCK = 1000; //Time the car's traffic light is green
  42.  
  43. void setup() {
  44.   // Setup LCD display
  45.   lcd.begin(16, 2);
  46.   pinMode(10,OUTPUT);
  47.   digitalWrite(10, 1);
  48.   lcd.setCursor(0,0);
  49.   // Set pins for each LED
  50.   semaforo[VERDE] = 13;
  51.   semaforo[AMBAR] = 12;
  52.   semaforo[ROJO] = 11;
  53.   semaforo_peaton[VERDE] = A1;
  54.   semaforo_peaton[ROJO] = A2;
  55.   pinMode(semaforo[VERDE], OUTPUT);
  56.   pinMode(semaforo[AMBAR], OUTPUT);
  57.   pinMode(semaforo[ROJO], OUTPUT);
  58.   pinMode(semaforo_peaton[VERDE], OUTPUT);
  59.   pinMode(semaforo_peaton[ROJO], OUTPUT);
  60.  
  61.   on = VERDE;
  62.   on_peaton = ROJO;
  63.  
  64.   //Serial.begin(9600);  //debug
  65.   lcd.setCursor(0,0);   // move cursor to the first line and first column
  66.   lcd.print("CAR");     // display "CAR" on the LCD screen      
  67.   lcd.setCursor(13,0);  // move cursor to the first line and last column
  68.   lcd.print("PED");     // display "PED" on the LCD screen
  69. }
  70.  
  71. // the loop routine runs over and over again forever:
  72. void loop() {
  73.   delta = millis() - last_time; // calculate time between frames
  74.   last_time = millis();
  75.   t += delta;
  76.   lcd_key = read_LCD_buttons();  // read the buttons    
  77.   button = lcd_key;
  78.  
  79.   if (on == AMBAR && t >= MAX_AMBAR) { // set semaphore to red if the time in ambar has passed
  80.     digitalWrite(semaforo[on], LOW);   // and set the pedestrian semaphore to green
  81.     digitalWrite(semaforo_peaton[on_peaton], LOW);
  82.     on = ROJO;
  83.     on_peaton = VERDE;  
  84.     t = 0;
  85.     time_left = MAX_ROJO + 1000; // add one second to the time left because division truncates
  86.   } else if (on == ROJO && t >= MAX_ROJO) { // set semaphore to green if the time in red has passed
  87.     digitalWrite(semaforo[on], LOW);        // and set the pedestrian semaphore to red
  88.     digitalWrite(semaforo_peaton[on_peaton], LOW);
  89.     on = VERDE;
  90.     on_peaton = ROJO;  
  91.     t = 0;
  92.   } else if (button != 5 && on == VERDE && t >= GREEN_LOCK) { // set semaphore to amber if the time in green has passed
  93.     digitalWrite(semaforo[on], LOW);                          // and set the pedestrian semaphore to red
  94.     digitalWrite(semaforo_peaton[on_peaton], LOW);
  95.     on = AMBAR;  
  96.     on_peaton = ROJO;  
  97.     t = 0;
  98.   }
  99.  
  100.   lcd.setCursor(0,1);            // move cursor to second line "1" and 9 spaces over
  101.   lcd.print("                ");
  102.   if (on == ROJO) {
  103.     time_left = time_left - delta;
  104.     unsigned long a = time_left / 1000;
  105.     //Serial.println(a); //debug
  106.     lcd.setCursor(1,1);          
  107.     lcd.print((MAX_ROJO/1000) - a);
  108.     lcd.setCursor(14,1);
  109.     lcd.print(a);
  110.   }
  111.  
  112.   digitalWrite(semaforo[on], HIGH); // set the corresponding pin to on depending of the color
  113.   digitalWrite(semaforo_peaton[on_peaton], HIGH); // set the corresponding pin to on depending of the color
  114.   delay(100);
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top