Hello everyone, I have this code to make to turn ON 3 LEDs (RED, GREEN, YELLOW) with a single push button. If we click the button once the red led is on and stays on, click again the green led is on and red and yellow off, and click again the yellow is ON and red and green off, click again all of them are off, click again the loop starts again. What I want is to make the LEDs to blink as well, but not just a single blink, I mean in different delays each one, and I know the only way to do that is with mills instead of delays but I can't get it right, this Mills function still very complicated for me. I really appreciate ur help. Thanks a lot
Find attached my code, the one that turns the LEDs ON but not blinking.
thanks Mike for that great explanation, that code is almost what I wanted to do, can you tell me which values should I change to make the button works this way:
First push: LED is ON and stays ON until button push again
Second Push: LED blinks 1000ms and delays 1000ms (continue doing this until button is pushed again)
Third PUsh: LED blinks 2000ms and delays 1000ms (continue doing this until button is pushed again)
Fourth PUsh: LED blinks 3000ms and delays 1000ms (continue doing this until button is pushed again)
Fifth PUsh: the loop stops until button is pushed and the loop starts again
I suppose in this code part below are the values that we have to change to get what I want. Right?
void checkPush(){
int buttonNow = digitalRead(button);
if(buttonNow != pushState) { // something has changed
pushState = buttonNow; // this is the new state of the button to remember
if(buttonNow == LOW) { // button has gone from a not being pushed to pushed
flashState++; // increment the state variable
goTime = millis(); // make the change immediately
if(flashState > 2) { // wrap round the state variable
flashState = 0;
nextTime = 1000; // wrap round the speed
} else {
nextTime = nextTime / 2; // make flashing twice the speed
}
}
}
}
thanks, Mike , I still have lots to learn I am just starting with this great world of codes, really fascinating.
So #2 talks about a different project than the top post, right? That snippet misses lots of essential info so not too much of idea what it's supposed to do.
You need to keep track of various states:
the button itself: but only it being pushed, not it being released.
the LED: whether it's on or off.
the blinks: what sequence it's in (the 1000, 2000 or 3000 ms).
Easiest for the time checking will be to set your check time to the future rather than using an interval. Something like:
if (millis() - nextBlink > 0) {
// Do the next stage of the blink.
if (ledState == HIGH) { // assume HIGH = on
ledState = LOW;
digitalWrite (ledPin, LOW);
nextBlink += 1000;
}
else { // it was off, switch on for some time.
ledState = HIGH;
digitalWrite(ledPin, HIGH);
switch blinkSequence {
case 1:
nextBlink += 1000;
break;
case 2:
nextBlink += 2000;
break;
case 3:
nextBlink += 3000;
break;
}
}
}
The button cycles through the blinkSequence; this code takes care of the various intervals.
Hey, thank you very much for your help. I really appreciate the time you have taken for this. Sorry for my ignorance, I am just starting to learn how to uses this codes and probably trying to get ahead too fast. The code you gave me, I tried to join it with mine and something definitely wrong, here is the way I have it:
, #define button 3 //Push button on 03 #define greenLED 6 //green LED on D6
int state = 0; //integer to hold current state
int old = 0; //integer to hold last state
int buttonPoll = 0; // integer to hold button state
void setup() {
pinMode(button,INPUT); //button set as input
pinMode(greenLED,OUTPUT);
digitalWrite(greenLED,LOW); //set initial state as off
}
void loop() {
//debouncing routine to read button
buttonPoll = digitalRead(button); //poll the state button
if(buttonPoll == 1){ //check if have been pressed
delay(50); //wait 50ms
buttonPoll = digitalRead(button); //poll button again
if(buttonPoll == 0){ //if is 0 considered one press
state = old + 1; // increases state by 1
}}
else{ //if button has not been pressed
delay(100); //wait 100ms
}
if (millis() - nextBlink > 0) {
// Do the next stage of the blink.
if (ledState == HIGH) { // assume HIGH = on
ledState = LOW;
digitalWrite (ledPin, LOW);
nextBlink += 1000;
}
else { // it was off, switch on for some time.
ledState = HIGH;
digitalWrite(ledPin, HIGH);
switch blinkSequence {
switch blinkSequence {
case 1:
nextBlink += 1000;
break;
case 2:
nextBlink += 2000;
break;
case 3:
nextBlink += 3000;
break;
}
}
}
I suppose this code is really wrong because I do not know how to put all together, the code for the push button is definitely not right and probably the declaration of different values as well. Thanks very much, I will wait for your comments.
I took that out but now says nextBlink was not declared in this scope:
#define button 3 //Push button on 03 #define greenLED 6 //green LED on D6
int state = 0; //integer to hold current state
int old = 0; //integer to hold last state
int buttonPoll = 0; // integer to hold button state
void setup() {
pinMode(button,INPUT); //button set as input
pinMode(greenLED,OUTPUT);
digitalWrite(greenLED,LOW); //set initial state as off
}
void loop() {
//debouncing routine to read button
buttonPoll = digitalRead(button); //poll the state button
if(buttonPoll == 1){ //check if have been pressed
delay(50); //wait 50ms
buttonPoll = digitalRead(button); //poll button again
if(buttonPoll == 0){ //if is 0 considered one press
state = old + 1; // increases state by 1
}}
else{ //if button has not been pressed
delay(100); //wait 100ms
}
if (millis() - nextBlink > 0) {
// Do the next stage of the blink.
if (ledState == HIGH) { // assume HIGH = on
ledState = LOW;
digitalWrite (ledPin, LOW);
nextBlink += 1000;
}
else { // it was off, switch on for some time.
ledState = HIGH;
digitalWrite(ledPin, HIGH);
switch blinkSequence {
case 1:
nextBlink += 1000;
break;
case 2:
nextBlink += 2000;
break;
case 3:
nextBlink += 3000;
break;
}
}
}
Start learning about variable declarations and variable types in C++, their differences, and why it matters!
Of course I can give you the line but this is so basic to the language it's something you should know, and post #10 should be completely clear to even a beginning programmer.
I understand you, really. I am not a programmer, I just like this, and like to do things, little projects. I read and watch videos but never been in college to learn professionally, I will do my best to become a programmer, step by step; so meanwhile I will learn by my own and focus on variables declarations, actually yesterday I was reading much more about it and I will try to apply that to all the info you have given me, I really appreciate your help and do not want you to think I just want the code or info, I really want to understand each piece of info you provide met as well. The unsigned declaration you mentioned before which value it should it be... 0?
No need to declare a value here.
It is important that it's unsigned, not a regular long, and another nice property is that overflows are handled automagically.
I did not work, many things are wrong in my code. I need this code working and I thought I could do it but no, I have the hardware ready, just waiting for the code, I need to control 12v pump with a relay in five different cycles as I mentioned, I could complete the same goal with 5 nano boards and a 5 position switch selector but I think is a waste of boards since one program can control the relay in 5 different ways. Do you know someone who does this for a living and charges me to make this code?