{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);
}
//else if counter is equal to 1, redPin is HIGH
else if(counter == 1)
{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);
}
//else if counter is equal to 2 greenPin is HIGH
else if(counter ==2)
{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);
}
//else if counter is equal to 3 bluePin is HIGH
else if(counter ==3)
{
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,HIGH);
}
//else reset the counter to 0 (which turns all pins off)
else
{
counter =0;
}
}
Code to Note
pinMode(buttonPin, INPUT);
The digital pins can be used as inputs as well as outputs. Before you do
either, you need to tell the Arduino which direction you’re going.
buttonState = digitalRead(buttonPin);
To read a digital input, you use the
digitalRead()
function. It will return
HIGH if there’s 3.3V present at the pin, or LOW if there’s 0V present at the
pin.
if (button1State == LOW)
Because we’ve connected the button to GND, it will read LOW when it’s
being pressed. Here we’re using the “equivalence” operator (“==”) to see if
the button is being pressed.
What You Should See
You should see the LED turn on if you press either button, and off if you
press both buttons. (See the code to find out why!) If it isn’t working, make
sure you have assembled the circuit correctly and verified and uploaded the
code to your board, or see the Troubleshooting section.
Page 33 of 63