Go Down

Topic: Run Void *** when button is pressed (Read 1 time) previous topic - next topic

Arrch

#10
Dec 17, 2012, 07:46 pm Last Edit: Dec 18, 2012, 05:02 pm by Arrch Reason: 1

This seems to be one of those Give me the code for it to do what I want, I don't want to actually how to code type of posts.

In some ways it may appear like that. I've been working on this project for 8 weeks and have built a fine matrix with over 620 led's, had to learn to solder correctly, design circuit boards after learning the software to do that, had to learn about transistors, IC's, power management, resistors, diodes, you name it. I'm studying as fast as I can and have come a long way in those 8 weeks.

My back is against the way, I have to get this finished in the next day or two as it's a CHRISTMAS DISPLAY and a present for someone. There's no point in me finishing it in February.


Ok, so it's Give me the code for it to do what I want, I don't have time to learn how to do it myself.

Quote

I understand the reasons for "guiding" people and helping them to learn, and I will, but as for this project.......  I NEED HELP TODAY

And you're getting help TODAY.

In order to have a responsive momentary switch that changes states you need to have those "states" to run non-blocking code. That typically means no non-trivial delays(), and very limited usage of for/while loops. In the flashSlow() and flashFast() code that you posted, that would mean you would need to redo those functions to not use delays. The blink without delay example shows you how to do just that. Another observation is that in the original code you posted, you run pattern() only during the rising edge once, and and pong all the other times. Assuming pattern() and pong() are non-blocking, you would be better off setting a state variable when you detect the transition. Then outside of the if statements, have your code check the state and run either pattern() or pong() based on its value.

PMKimpton


In order to have a responsive momentary switch that changes states you need to have those "states" to run non-blocking code. That typically means no non-trivial delays(), and very limited usage of for/while loops. In the flashSlow() and flashFast() code that you posted, that would mean you would need to redo those functions to not use delays. The blink without delay example shows you how to do just that. Another observation is that in the original code you posted, you run pattern() only during the rising edge once, and and pong all the other times. Assuming pattern() and pong() are non-blocking, you would be better off setting a state variable when you detect the transition. Then outside of the if statements, have your code check the state and run either pattern() or pong() based on its value.


Thank you for your understanding Arrch, I was on the verge of binning all my Arduino kit, giving up and buying my mum a kindle!
I'm not a scrounger, I am reading everything I can but I'm really struggling with the code concept.
Whilst your comments above sort of make sense to me, I haven't a clue where to begin to implement them. Straight over my head mate :-)

I posted up the simple LED flash code to better understand what I'm doing wrong in my main project code.

Would you kindly re-write the code for the example LED flash code so that it would switch back and forth when a button is pressed? then I'll have some code to study so as to make the changes to my main code.

Thanks Arrch,

Arrch

#12
Dec 17, 2012, 08:11 pm Last Edit: Dec 17, 2012, 08:13 pm by Arrch Reason: 1

Would you kindly re-write the code for the example LED flash code so that it would switch back and forth when a button is pressed? then I'll have some code to study so as to make the changes to my main code.


The main code should look something like this

Code: [Select]
set state to 0

get the current button state
if the current button state high and the previous one low?
  is the state 0?
     set the state to 1
  otherwise
     set the state to 0

is the state 0?
 run non-blocking long blink code
otherwise
 run non-blocking short blink code

set last button state to current one


In order to get the long blink and short blink code to be non-blocking, they will need to be re-written using using the concepts in the Blink Without Delay example.

PMKimpton

Thanks Arrch, I will study this all night because I'm not under any pressure.

I appreciate your time & comments, but you may as well written them in Spanish, I don't understand code...

Seeing as I don't understand how to resolve this simple button issue using code, I going to solve it by doubling up my hardware and then just turn off one arduino and turn another one on.

Solved.

Go Up