0% found this document useful (0 votes)
48 views2 pages

3 Led 3 Button

The code defines constants for pin connections of three LEDs (green, blue, yellow) and three buttons. It initializes variables to store the button states and LED states. In setup, the pins are configured as outputs for the LEDs and inputs for the buttons. The main loop reads the button states and toggles the corresponding LED when a button is pressed by changing its state variable and writing to the pin.

Uploaded by

harris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

3 Led 3 Button

The code defines constants for pin connections of three LEDs (green, blue, yellow) and three buttons. It initializes variables to store the button states and LED states. In setup, the pins are configured as outputs for the LEDs and inputs for the buttons. The main loop reads the button states and toggles the corresponding LED when a button is pressed by changing its state variable and writing to the pin.

Uploaded by

harris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const int ledGreen = 8;

const int ledBlue = 9;


const int ledYellow = 10;

const int button2 = 2;


const int button3 = 3;
const int button4 = 4;

int buttonState2, buttonState3, buttonState4;


int oldButton2, oldButton3, oldButton4 = 0;
int state2, state3, state4 = 0;

void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledYellow, OUTPUT);

pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
}
void loop() {
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
buttonState4 = digitalRead(button4);

if(!buttonState2 && !oldButton2){


if(state2 == 0){
digitalWrite(ledGreen, HIGH);
state2 = 1;
}else{
digitalWrite(ledGreen, LOW);
state2 = 0;
}
oldButton2 = 1;
}else if(buttonState2 && oldButton2){
oldButton2 = 0;
}

if(!buttonState3 && !oldButton3){


if(state3 == 0){
digitalWrite(ledBlue, HIGH);
state3 = 1;
}else{
digitalWrite(ledBlue, LOW);
state3 = 0;
}
oldButton3 = 1;
}else if(buttonState3 && oldButton3){
oldButton3 = 0;
}

if(!buttonState4 && !oldButton4){


if(state4 == 0){
digitalWrite(ledYellow, HIGH);
state4 = 1;
}else{
digitalWrite(ledYellow, LOW);
state4 = 0;
}
oldButton4 = 1;
}else if(buttonState4 && oldButton4){
oldButton4 = 0;
}
}

You might also like