Evie’s Arduino Lesson #3

Hey all – below you’ll find my code & videos from Arduino Lesson #3.

Let’s start with the simple ones.

Digital Input – Button

Wahoo – the button! I followed the steps in the tutorial to change the code to have the LED start at OFF instead of ON – this is a video of that.

Serial Monitor

This one was pretty cool to me. It’s like my laptop and Arduino board were having a chat with each other – but I was the mediator.

Turn LED On/Off with 2 Buttons

Writing my own code was definitely a bit challenging at first. Although I have some experience with front-end web dev, I was never particularly good at Javascript (and I find Arduino code to be quite similar to Javascript with if/else functions, etc.). I used some of the existing Arduino examples as guides just to make sure I had the logic down.

During my first go, I had a ton of errors, due to omitted semicolons (ugh!). I added them back in and everything was fine and dandy—except the buttonON state worked, but the buttonOFF state did not.

For the longest time, I couldn’t figure out what was wrong with my code. Everything made sense. Turns out…nothing was wrong with my code. I had forgotten a wire on my breadboard 😖 But I fixed it!

Here’s the video:

And here’s the code:

//Two Buttons ON/OFF with 1 LED

//CONSTANTS
const int buttonOn = 3;   //the number of the ON pushbutton pin
const int buttonOff = 5;   //the number of the OFF pushbutton pin
const int ledPin = 12;       //the number of the LED pin

//VARIABLES
int buttonState1 = 0;         //variable for reading the pushbutton status
int buttonState2 = 1;         //other variable for reading status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the ON and OFF pushbutton pins as inputs:
  pinMode(buttonOn, INPUT);
  pinMode(buttonOff, INPUT);
  

}

void loop() {
  // read state of the pushbutton value:
  buttonState1 = digitalRead(buttonOn);
  buttonState2 = digitalRead(buttonOff);

  //check if pushbutton is pressed.
  //if it is, the buttonState is HIGH:
  if (buttonState1 == LOW) {
    //turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  if (buttonState2 == LOW) {
    //turn LED off:
    digitalWrite(ledPin, LOW);
  }

}

Side note: I think I definitely need to trim my resistors. They are getting quite cumbersome as we add more fun things each week.

Author: Evie

A freelance photographer / writer working to inspire curiosity and world travel.

One thought on “Evie’s Arduino Lesson #3”

Comments are closed.