Hello, I am trying to push one button and it will trigger two alternating leds, and when you press the button again it will automatically shut them off. Any sort of programming/wiring diagram could help. Any attempt I make just fails!!
Show your attempt(s); both a wiring diagram (photo of hand drawn is ok) and your code.
//This is the coding I used
const int buttonPin = 2;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2,HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
Look at the StateChangeDetection example in 02. Digital.
//Coding
//Pin 3: Input
//pin 2: Output
int ButtonValue = 0;
int Button = 3;
int LED1 = 2;
int LED2 = 4;
void setup() {
pinMode(Button, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
ButtonValue = digitalRead(Button);
if(ButtonValue != 0) {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
else{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}
}
//I just cannot get it to alternate correctly due to coding once I press the button.
HIGH equals 1, LOW equals 0. So your code in reply #5 is basically the same as the code in reply #2.
Follow the advise in reply #4.