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);
  }
}

Toongenerator op basis van lichtinval

Standaard

Een Arduino Uno starterskit kwam vandaag met de post aan. Een vrij kleine doos met een Arduino Uno bord en diverse componenten. Mijn eerste ervaring met Arduino valt best mee. Gezien het feit dat ik sinds mijn secundair onderwijs (1996) niets meer met elektronica heb gedaan, maar toch veel ervaring als programmeur heb opgedaan sindsdien, is het bouwen van elektronische circuits terug snel opgestart.

Als bewijs hiervan heb ik op zeer korte tijd een toongenerator op basis van lichtinval gemaakt. Startende met een lichtafhankelijke weerstand (LDR), een piezo speaker, een Arduino Uno en een paar weerstanden.

We geven alvast de Arduino code van dit project vrij:

/*
  Light Depending Music Box

  Loops and plays notes
  When light changes the pitch shifts

  created 2011

  The circuit:
  - Piezo speaker
  - 2x 100 Ohm resistor
  - 1x LDR

*/

void setup() {
  // Serial read spead 9600 baud
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  // eighth note = 1000/8
  int noteDuration = 1000/8;
  // set tone on digital pin 8
  tone(8,sensorValue,noteDuration);
  // to distinguish the notes, set a minimum time between them.
  // the note's duration + 30% seems to work well:
  int pauseBetweenNotes = noteDuration * 1.30;
  delay(pauseBetweenNotes);
  // stop the tone playing:
  noTone(8);
}