Hello, does anyone know a code that make 3 LEDs light up as I press the button, type one at a time? (squeeze the button once and the LED 1 is lit until I press again, and so the 2nd LED lights, erasing the 1st .. and on until all are off) ...
I am sure you could code this if you first go through the examples that come with the Arduino IDE.
I searched the examples and tried to create the code, but without success D:
You wont find exactly what you want there but you will get the knowledge to accomplish your task .
I searched the examples and tried to create the code,
So, show us the code you tried to create and we can help you make it work, or I can move this to gigs and collaborations.
I tried, but it only lights the red LED, and also, when I released the button and LED also goes off.
int greenled = A5;
int yellowled = A4;
int redled = A3;
const int buttonPin = 2;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(greenled, OUTPUT);
pinMode(yellowled, OUTPUT);
pinMode(redled, OUTPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(redled, HIGH);
}
else {
digitalWrite(redled, LOW);
digitalWrite(yellowled, LOW);
digitalWrite(greenled, LOW);
}
}
I also tried the example Debounce (http://arduino.cc/en/Tutorial/Debounce) but it only works in 1 LED ...
OK, so rather than just detecting that the input pin is high (or low), try detecting when it goes from low to high, and count the events.
You'll need a global variable to remember the state of the pin last time you read it.
It only lights the red led because that's the ONLY pin you turn high....
That's the problem, I do not know how to do this ...
I'm on my phone...
But what you need to do is write or draw all the logical steps and then have the code match...
A counter to count how many times the button is pressed.
Debounce code
If counter==1
....
Etc
int greenled = A5;
int yellowled = A4;
int redled = A3;
const int buttonPin = 2;
int buttonState = 0;
int var;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(greenled, OUTPUT);
pinMode(yellowled, OUTPUT);
pinMode(redled, OUTPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
digitalWrite(redled, LOW);
var = 1;
delay(1000);
}
if (buttonState == HIGH && var == 1) {
digitalWrite(greenled, LOW);
digitalWrite(yellowled, HIGH);
digitalWrite(redled, LOW);
var = 2;
delay(1000);
}
}
Now, just the yellow led turn on --'
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int greenled = A5;
const int yellowled = A4;
const int redled = A3;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(buttonPin, INPUT);
pinMode(greenled, OUTPUT);
pinMode(yellowled, OUTPUT);
pinMode(redled, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter == 1) {
digitalWrite(redled, HIGH);
}
if (buttonPushCounter == 2) {
digitalWrite(yellowled, HIGH);
digitalWrite(redled, LOW);
}
if (buttonPushCounter == 3) {
digitalWrite(greenled, HIGH);
digitalWrite(yellowled, LOW);
}
if (buttonPushCounter % 4 == 0) {
digitalWrite(greenled, LOW);
digitalWrite(yellowled, LOW);
digitalWrite(redled, LOW);
buttonPushCounter = 0;
}
}
Got it!!!! XD
If you want to clean it up... look up Switch/Case select
Got it!!!!
Good for you.
It was all in your head, you just had to find it.