Go Down

Topic: Sequencing through sketches with a button press (Read 288 times) previous topic - next topic

I'm working on LED flash patterns with multiple LED's. I have 4 different flash patterns programed in a single sketch. I want to be able to switch from the first pattern to the next pattern and on through the patterns to the last using a button press.
I hope I'm explaining that right.
I found a tutorial in Hindi on Youtube and tried to copy his example from the comments and it works, sort of, but I need to hold the button for several seconds before it will change to the next sketch pattern.
I can paste the code here if that's okay. I just don't want to yet until I get permission.
Any and all help and/or suggestions are welcome.
Thank you in advance.
Doug 

PaulS

Most likely, the code you found uses for loops and delay() to run through a complete "pattern" before reading the switch again.

You can read the switch state and break out of the loop, but you can't do anything about the delay()s, without completely rewriting the sketch to be a state machine.
The art of getting good answers lies in asking good questions.

It doesn't use for loops. it uses an "int buttonState" command and an "int p=1". Then each flash pattern is changed using an "if (p==1)" "if (p==2)" "if (p==3)" etc.
There are delays in each of the flash patterns to get the LED's to behave properly though.

PaulS

It doesn't use for loops. it uses an "int buttonState" command and an "int p=1". Then each flash pattern is changed using an "if (p==1)" "if (p==2)" "if (p==3)" etc.
There are delays in each of the flash patterns to get the LED's to behave properly though.
If you don't want to show us the code, that's fine. But, you can't expect us to tell you how to fix it without showing it to us.
The art of getting good answers lies in asking good questions.

I'm not sure how attachments work here. I attached a .ino file for the code I'm working on.

Robin2

I'm not sure how attachments work here. I attached a .ino file for the code I'm working on.
That code is an awfully bad starting point. It is full of delay()s which is why it is unresponsive. IMHO it would take so long to change them to non-blocking code that it would not be worth trying.

Better to make a detailed note of the behaviour that you want and write a completely new program to put it into effect.


The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R
Planning and Implementing a Program
Two or three hours spent thinking and reading documentation solves most programming problems.

#6
Mar 29, 2019, 12:14 am Last Edit: Mar 29, 2019, 03:30 am by Arduino_Cowboy
My level of programing is no where near as advanced as yours and although this sketch looks like it would work, I just don't understand most of it.
What I'm attempting to do is blink 7 segments of LED's (40 LED's in each segment) to mimic the rear Amber section of a service truck light bar.
I've already figured out the wiring for the 40 LED matrix and how to hook them to the Arduino and each sketch works just like I want it to.
The 1st sketch would blink the segments from Left to Right. Progressively lighting each segment.
The 2nd sketch blinks from Right to Left.
The 3rd sketch blinks from the center outward to both ends.
I just need to figure out how to switch from one sketch to the next by pressing a button.
Once I have that figured out I can add different flash patterns.
The sketch that I uploaded was copied from a youtube page that was in Hindi and I couldn't understand what he was saying. However, his solution was working. It just took a very long button press to switch between sketches.
What I need is help understanding how to write a sketch that will work with a button press so I can
keep working on this project.
Thanks to anyone willing to help.

Robin2

I don't think I can add anything beyond what I said in Reply #5

If you want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge

#8
Mar 29, 2019, 09:20 am Last Edit: Mar 29, 2019, 09:25 am by TomGeorge
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

I have edited the code to use   switch.. case function.
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

I have added some comments which may be of help.
This will make sequence selection easier.

How have you got your button wired to the Arduino?
What model Arduino are you using?

Because you are using delay(), the only time the button pin will be read is when a sequence has finished a cycle, so you may have to hold the button down until you see a sequence change.
Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up