Danica’s Arduino Homework 2

I choose the two buttons control one LED (on and off), and click either or both of the button can turn the LED off.

Here is the video:

Here is the circuit and code:

const int buttonPin1 = 2;
 const int buttonPin2 = 7;//the number of two button pin
 const int ledPin =  13;      // the number of the LED pin
 // variables will change:
 int buttonState1 = 0; 
 int buttonState2 = 0;// variable for reading the pushbutton status
 void setup() {
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);
   // initialize the pushbutton pin as an input:
   pinMode(buttonPin1, INPUT);
   pinMode(buttonPin2, INPUT);
 }
 void loop() {
   // read the state of the pushbutton value:
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
   if (buttonState1 == HIGH&buttonState2==HIGH) {
     // only when two button is off when turn LED on:
     digitalWrite(ledPin, HIGH);
   } else {
     // turn LED off:
     digitalWrite(ledPin, LOW);
   }
 }

ps: I tried to embed the Tinkercad link, but I couldn’t find the project in my own Dashboard, it’s missing. SoI can only share the link.

Discover more from Making Studio

Subscribe now to keep reading and get access to the full archive.

Continue reading