EFA:002 – Microcontrollers and Arduino Basics

Inspiration

Today’s Artists

Class Content

Firstly, we will talk about the Final Project and see some examples from previous years.

After that, we go through the basics of working with the Arduino. See this separate page about it: Arduino Basics

Example Done in Class

This is the code for the final example we ended up doing in class

int btnState;
int pot;
int b;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  btnState = digitalRead(2);
  pot = analogRead(A0);
  b = map(pot, 0, 1023, 0, 255); 
  Serial.print(pot);
  Serial.print(",");
  Serial.println(b);
  
  if (btnState == HIGH) {
    analogWrite(9, b);
  } else {
    analogWrite(9, 0);
  }
}

Homework

For next week, you have two assignments to complete. They will mainly repeat the things we did in class with slightly more complex code. So go back to the Arduino Basics page if you forgot how to do something.

Homework Project #1

  • Connect a light sensor to your Arduino using a voltage divider with a 10kΩ resistor.
  • Connect a potentiometer to your Arduino.
  • 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.

This page about analog sensors should help you out.

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

Homework Project #2

  • 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.

Extra Challenge (not compulsory to do)

Were those too easy? Try this:

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

Still too easy?

  • 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

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

Matti’s Solution:

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