Writing Code to Make Your Button Work | Task 3


Below is the code to make your button work.


    // Push buttons

int buttonPin = 3;
int ledPin = 9;
int buttonValue;
int dt = 100;

void setup() {
  // put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
buttonValue = digitalRead(buttonPin);

if (buttonValue == 1) {
  digitalWrite(ledPin, LOW);
}
else {
  digitalWrite(ledPin, HIGH);
}
Serial.println(buttonValue);
delay(dt);
}

        

The final task.


It's time to experiment, now add some LED's and make some come on when the button is pushed and others will turn off. Add a second button and make it turn on a different LED when pressed. You can also try to make the LED's flash when the button is pressed.