Verkeerslichten gestuurd met Arduino

Standaard

Maak je eigen verkeerslichten met behulp van Arduino. Wat heb je hiervoor nodig?

  • 1 breadboard
  • 2 groene leds
  • 2 rode leds
  • 2 oranje leds
  • 6 weerstanden van 220 Ohm
  • 1 Arduino Uno
  • Onderstaande code
/*

  Traffic lights

  Uses 6 leds (2 x orange, red and green) to simulate traffic lights. Initializes the program by blinking all leds 3 times

*/
int arrLedPins[] = {13,12,11,10,9,8};
int intLedPins = 6;

int arr13[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH};
int arr12[] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
int arr11[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,HIGH,HIGH,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
int arr10[] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
int arr9[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW,LOW};
int arr8[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,HIGH,HIGH};
int steps = 24;
int counter = 0;

boolean ledTestExecuted = false;

void setup() {
  // initialize all led output pins
  int i;
  for (i = 0; i < intLedPins; i = i + 1) {
    pinMode(arrLedPins[i], OUTPUT);
  }
}

void loop() {
  // Led test executed
  if (!ledTestExecuted) {
    // blink all leds 3 times
    // necessary for checking if all leds are still functioning
    int i, j;
    for (i = 0; i < 3;  i = i + 1) {
      for (j = 0; j < intLedPins; j = j + 1) {
        digitalWrite(arrLedPins[j], HIGH);
      }
      delay(200); // wait one second
      for (j = 0; j < intLedPins; j = j + 1) {
        digitalWrite(arrLedPins[j], LOW);
      }
      delay(200); // wait one second
    }
    ledTestExecuted = !ledTestExecuted;
  }
  else {
    if (counter > (steps - 1)) {
      counter = 0;
    }
    int j, ledStatus;
    for (j = 0; j < intLedPins; j = j + 1) {
      if (arrLedPins[j] == 13) {
        ledStatus = arr13[counter];
      }
      else if (arrLedPins[j] == 12) {
        ledStatus = arr12[counter];
      }
      else if (arrLedPins[j] == 11) {
        ledStatus = arr11[counter];
      }
      else if (arrLedPins[j] == 10) {
        ledStatus = arr10[counter];
      }
      else if (arrLedPins[j] == 9) {
        ledStatus = arr9[counter];
      }
      else {
        ledStatus = arr8[counter];
      }
      digitalWrite(arrLedPins[j], ledStatus);
    }
    counter = counter + 1;
    delay(1000);
  }
}