Arduino Exercise 3

This week we deep dived into more Arduino exercises and started to add on knobs and switches to do more with the LED. Overall the lessons was a  pretty straightforward process likewise to the previous exercises. It was not until the third part of writing your own code, that made me realize how important it was to state everything and understand all the terminology more carefully.

First was the Digital Input

Next it was the Serial Monitor

Lastly I choose to do the option of turning on and off the LED with two buttons

This is the code:

 

//Turn on LED when button1 and turn off LED when button2 is presseD

const int LED = 12; //the pin for the LED
const int BUTTON1 = 9; //the input pin where the pushbutton is connected
const int BUTTON2 = 10; //the input pin where the pushbutton is connected

int val1 = 0; //val will be used to store the state of the input pin
int val2 = 0; //val will be used to store the state of the input pin
bool ButtonState = false;

void setup() {
  pinMode(LED, OUTPUT); //tell Arduino LED is an output
  pinMode(BUTTON1, INPUT_PULLUP); //button1 is an input
  pinMode(BUTTON2, INPUT_PULLUP); //button1 is an input
  Serial.begin(9600);
}
void loop() {
  // read the state of the push button
  val1 = digitalRead(BUTTON1); //read input value and store it
  val2 = digitalRead(BUTTON2); //read input value and store it
  //check what happened to the pushbutton1 when

  if(val1 != 1){
    ButtonState = true;
  }

  if(val2 != 1){
    ButtonState = false;
  }

  if (ButtonState == true) {
    //turn LED on:
    digitalWrite (LED, HIGH);
  }

  //check what happened to the pushbutton2 when pressed
  if (ButtonState == false) {
    //turn LED off:
    digitalWrite (LED, LOW);
  }

  Serial.println(val1);
}

One thought on “”

Comments are closed.