PaulRB:
- Your for-loop containing a delay() is what is called "blocking code" because it will not allow other actions to be performed until it is finished. Other actions include testing if a button is pressed. You will need to re-write the code to remove the blocking parts. In the programming questions forum section, there is a sticky post which explains how to do this.
Hi and thank you for your help. Code re-wrote (poorly) without delay()... the sequence seems slugish when leds transition... still no solution for second time the pushbutton is pressed in this code. Could you give me more hints please?
unsigned long currentMillis = 0;
int ledInterval = 500;
unsigned long previousLedMillis = 0;
int ledOn = 2; //initial pin number of 1st led to be ON
const int numLeds = 10;
const int buttonPin = 12;
const int buttonInterval = 300;
unsigned long previousButtonMillis = 0;
byte buttonState = HIGH;
void setup() {
pinMode(buttonPin, INPUT);
for (int thisPin = 2; thisPin <= numLeds+1; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
currentMillis = millis();
readButton();
ledSequence();
}
void readButton() {
buttonState = HIGH;
if (currentMillis - previousButtonMillis >= buttonInterval) {
if (digitalRead(buttonPin) == LOW) {
previousButtonMillis += buttonInterval;
buttonState = LOW;
}
}
}
void ledSequence(){
if (buttonState == LOW){
digitalWrite(ledOn, HIGH);
if (currentMillis - previousLedMillis >= ledInterval) {
previousLedMillis += ledInterval;
digitalWrite(ledOn, LOW);
ledOn++;
}
}
}
PaulRB:
2. Looking at the picture you posted, you seem to be using an Arduino simulator. In this forum, members generally use real Arduino and real circuits and many do not trust simulators. If at any time your code does not work as expected, they will probably blame the simulator and tell you to try the code on a real Arduino.
I have the Uno, Mega, Nano and Micro for testing purposes.
hannah_mackinlay:
I'd like to help, but I read that 5 times and still don't understand what it is you want to do, sorry!
Each time i press the pushbutton a led lit "animation" (like a one way larson scanner) should start on bottom led and end on the upmost led. If during that animation i click the pushbutton again, it should end the current animation and start a new one simultaneously.
Grumpy_Mike:
For how long?
How long i press the pushbutton? - could be instantly, but that's not what i'm searching to achieve.
How long is the sequence? till the upmost led is unlit unless we push the button again
Grumpy_Mike:
You seem to be wanting a limitless number of additional to the sequence but you are very specific about what these sequences should do.
The sequence should do exactly what it does. But i want to be able to add another sequence upon the push of a button even if there is a current sequence going on. "Add" means the example i gave: finish current sequence while adding a new one.
Grumpy_Mike:
Also what do you want to happen when you do not press the button? Should it cycle the length of sequence so far, should it stop on the last pattern or should it go blank?
Nothing happens if the button isn't pressed. Upon pressing the button for the 1st time, if we do not press the button, the sequence would end in the upper led.
Grumpy_Mike:
One simple way if you are not fussed about the pattern is to use the random number generator. Use the seed function with a constant at the start of the sequence, so that the sequence is always the same, until it comes to the new extended bit of the sequence.
I only want this pattern
PaulRB:
This is how I interpreted the OP's wish:
When a button is pressed, led #0 lights. Every 500ms, that single lit led moves on to the next position in line (#1, #2...). After led #9 has been lit for 500ms it goes out and no LEDs are lit until the next button press. This is what the sketch does now.
In the new version, the OP would like the button to be monitored continuously during the led movement. If the button is pressed again while the lit led is moving, led #0 is lit again and a second lit led follows the first down the line. And so on. Consequently, if the button is held down for ~5s, all LEDs would be lit, and if the button was released for ~5s, all LEDs would go out.
Excelent!
Grumpy_Mike:
As we both know one of the biggest problems with beginners is that they know what they want to happen but only put into words a very limited and incomplete sub set of what they want assuming that "everyone will know what I mean".
Really tried my best showcasing, tried to explain what i got, what i want, made an example... but you made some serious points on your questions wich i've made to myself in order to find a solution, so thanks!