01 Introduction: Arduino Basics

Create a circuit and Arduino code that does the following

Circuit

  • Connect two LEDs to your Arduino using a breadboard
  • Connect one switch to your Arduino using a breadboard

Code

  1. Read a momentary switch being pressed
  2. When the program starts, both LEDs are off
  3. When the switch is pressed once, the first LED turns on
  4. When the switch is pressed the second time, the second LED turns on (the first one should also still be on)
  5. When the switch is pressed the third time, both LEDs turn off
  6. Repeat this same cycle of LEDs turning on and off in sequence (off, one LED, two LEDs, off…)
First attempt

With the knowledge I remembered from class, high school physics class and my first (unsuccessful) studies in electrical engineering, I started to draw a first circuit diagram. I then collected the parts I needed and built the circuit according to my drawing. I chose to work with digital pins 2 and 4, because they didn’t have the ~ symbol. I haven’t figured out what that symbol stands for, I’m guessing it’s alternating current (AC). Also, I didn’t know how to involve the connection to the Arduino in my circuit drawing at this point.

I pressed the button, but nothing happened. I checked the board and it turned out that the LEDs were pointing the wrong direction – a problem I still run into often. I fixed the problem and both LEDs turned on, when I pressed the button. A first small success, but not really the expected outcome of the assignment. When I opened up the Arduino programming software and tried to access LEDs and the button with the Arduino pins, I realized that I was missing a lot of theoretical understanding. Fortunately, Matti provided a clear and comprehensive tutorial on the course website, that provided all the information I needed to complete the assignment: https://learn.newmedia.dog/tutorials/arduino-and-electronics/arduino/

I learned how to set up the circuit properly, reading the signals from the LEDs and the button and storing signal changes as states. The final challenge for me was figuring out the correct syntax of my code. The key was understanding how to save the button state as pressed and released.

State 0
State 1
State 2

Arduino Code

// A button that first turns on one LED, then another, and then turns both LEDs off again
int btnPin = 2;
int ledPin1 = 9;
int ledPin2 = 11;
int btnState = false;
int prevBtnState = false;
int counter = 0;
void setup() {
  // Open the serial port
  Serial.begin(9600);
  // set the button pin to be an input
  pinMode(btnPin, INPUT);
  // set the LED pin to be an output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}
void loop() {
  // read the button pin
  btnState = digitalRead(btnPin);
  // State 0
  if (btnState != prevBtnState && counter == 0) {
    // button pressed down
    if (btnState == HIGH) {
      Serial.println("Light 1 on, Light 2 off");
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    }
    // button released
    if (btnState == LOW) {
      counter++;
      Serial.print("Count: ");
      Serial.println(counter);
    }
    prevBtnState = btnState;
  }
  // State 1
  if (btnState != prevBtnState && counter == 1) {
    // button pressed down
    if (btnState == HIGH) {
      Serial.println("Light 1 on, Light 2 on");
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
    }
    // button released
    if (btnState == LOW) {
      counter++;
      Serial.print("Count: ");
      Serial.println(counter);
    }
    prevBtnState = btnState;
  }
  // State 2
  if (btnState != prevBtnState && counter == 2) {
    // button pressed down
    if (btnState == HIGH) {
      Serial.println("Light 1 off, Light 2 off");
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
    }
    // button released
    if (btnState == LOW) {
      counter = 0;
      Serial.print("Count: ");
      Serial.println(counter);
    }
    prevBtnState = btnState;
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *