EFA | Arduino Labs

#001 Adjustable Threshold

  1. Connect a light sensor to your Arduino using a voltage divider with a 10kΩ resistor.
  2. Connect a potentiometer to your Arduino.
  3. Connect two LEDs to your Arduino.

The code should do this:

  • Read both voltages (light sensor and potentiometer) and print the values out on one line separated by a comma using the Serial.print() and Serial.println() functions.
  • Use the value from the potentiometer as an adjustable threshold variable. The threshold changes depending on the value from the potentiometer.
  • If the reading from the light sensor is below the threshold, the first LED should turn on and the second LED should be off.
  • If the reading from the light sensor is above the threshold, the second LED should turn on and the first LED should be off.

Try to do this first on your own. If you get stuck or want to compare your solution to mine, see the video below.

#002 Cycle Through LEDs

  • Connect the momentary switch to your Arduino.
  • Connect three LEDs to your Arduino.

The code should do this:

  • At first all LEDs are off
  • When the switch is pressed the first LED turns on
  • When the switch is pressed the second time, the second LED turns on
  • When the switch is pressed the third time, the third LED turns on.
  • When the switch is pressed the fourth time, all of the LEDs turn off again
  • When the switch is pressed the fifth time, the first LED light up again
  • …repeat this pattern on a loop

Try to do this first on your own. If you get stuck or want to compare your solution to mine, see the video below.

#003 Knight Rider

Create the Knight Rider LED effect from KITT with your LEDs (make sure to grab some more LEDS, you’ll need at least 6).

  • Make the scrolling speed adjustable with the potentiometer or the light sensor
  • Make this adjustment more responsive (hint: when you use delay() the entire program freezes)
  • Use analogWrite() to create a smoother visual effect so that the LEDs don’t just suddenly turn on/off

Arduino Tutorial

There is an Arduino tutorial on how to do this in different ways but all of them depend on the use of multiple delays inside the main loop(). This causes the program to be unresponsive for long periods of time.

https://www.arduino.cc/en/Tutorial/KnightRider

Matti’s Solution:

This solution makes the program more responsive and creates a nice fading effect for the LEDs. It still uses one delay() in the code but it’s better than the Arduino tutorial. You could get rid of that by using millis().

// LEDs are connected to pins 3, 5, 6, 9, 10, 11
// all of these pins are PWM pins on the Arduino Uno
int ledPins[] = {3, 5, 6, 9, 10, 11};
// an array of brightness levels for the LEDs
int ledB[6];

int ledAmount = 6;
int stepAmount = 1;
int pot;
int stepDelay;

int counter = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 0; i < ledAmount; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);
    ledB[i] = 0;
  }
}

void loop() {
  pot = analogRead(A0);
  stepDelay = map(pot,0,1023,10,200);
  counter = counter + stepAmount;

  if (counter <= 0 || counter > ledAmount) {
    stepAmount = -stepAmount;
  }
  Serial.println(counter);

  for (int i = 0; i < ledAmount; i++) {
    if (counter == i ) {
      ledB[i] = 255;
    } else {
      ledB[i] -= 80;
    }
    ledB[i] = constrain(ledB[i], 0, 255);
  }

  for (int i = 0; i < ledAmount; i++) {
    analogWrite(ledPins[i], ledB[i]);
  }
  
  delay(stepDelay);
}