Go Down

Topic: Check if some of the buttons was pressed while executing a "for" loop. HELP! (Read 6907 times) previous topic - next topic

RogerioBoscolo



Quote

So, i just made a few adjustments to use the attachInterrupt but it still not working.  =( maybe the problem still in the "delay(WAIT);"

Not maybe. It is the problem.
Code: [Select]

unsigned long startTime;  //add this line before setup()

setup(){
startTime = millis();  //add this line
}
.
.
.
void writeLED1(){
if(millis() - startTime >= WAIT){
  analogWrite(red, r);
  analogWrite(green, g);
  analogWrite(blue, b);
  startTime = millis(); //reset startTime for next iteration
  }
}


Unfortunately i did that before and did that now, id makes the led to fade a lot faster but still stuck in the rgb loop once inside it. :smiley-eek:
But thank you anyway, any other suggestion would be appreciated

cypherrage

Read the last line of your rgb() function...

It's rgb(), hence, you call the function itself again right before the function ends, hence recursion, and thus resulting in an infinite "loop" (not a loop technically, but the effect is the same in your case since nothing changes from iteration to iteration.)

GoForSmoke


looks like you are calling rgb();
from within rgb()



There is the major problem. rgb() runs rgb() with no escape until the return addresses stack down to overwrite the heap (your variables) after a very long time.

The delay just slows it all down and keeps your code from noticing a pressed button.

Learning BlinkWithoutDelay from your IDE examples (can be reduced to 12 lines of code not counting the comments) is the first step in learning real-time code with Arduino. It's funny how hard less than 20 lines of code can be to follow.



1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

RogerioBoscolo



looks like you are calling rgb();
from within rgb()



There is the major problem. rgb() runs rgb() with no escape until the return addresses stack down to overwrite the heap (your variables) after a very long time.

The delay just slows it all down and keeps your code from noticing a pressed button.

Learning BlinkWithoutDelay from your IDE examples (can be reduced to 12 lines of code not counting the comments) is the first step in learning real-time code with Arduino. It's funny how hard less than 20 lines of code can be to follow.





Unfortunately if i dont write that down the rgb() will run only once after i press the button, i need it to keep running indefinitely until the button is pressed. The funny thing is that nothing solved it until now, may i give up? That can't be a impossible thing to achieve right?

cypherrage

Take out the rgb() line at the end of your rgb() function to solve the recursive problem, then put in a simple line such as this, and until you push the button, rgb() will continuously run.

Code: [Select]
while (!digitalRead(myButton)) {
  rgb();
}


Alternatively, if you want it to run while the button is pushed, you can just invert the logic above (by taking out the ! ).

Also, if you want the program to stall until you release the button (which I like to do, prevents multiple things from happening due to one button press), you can do a simple modification to the above code:

Code: [Select]
while (digitalRead(myButton)) {delay(2);}

RogerioBoscolo


Take out the rgb() line at the end of your rgb() function to solve the recursive problem, then put in a simple line such as this, and until you push the button, rgb() will continuously run.

Code: [Select]
while (!digitalRead(myButton)) {
  rgb();
}


Alternatively, if you want it to run while the button is pushed, you can just invert the logic above (by taking out the ! ).

Also, if you want the program to stall until you release the button (which I like to do, prevents multiple things from happening due to one button press), you can do a simple modification to the above code:

Code: [Select]
while (digitalRead(myButton)) {delay(2);}

It solved the problem of getting out of the rgb() loop. YAY!! :D :)

BUT....

Now i can't execute the rgb() loop again, if  press that "bt_1" button the rgb() code isn't called :(

GoForSmoke




looks like you are calling rgb();
from within rgb()



There is the major problem. rgb() runs rgb() with no escape until the return addresses stack down to overwrite the heap (your variables) after a very long time.

The delay just slows it all down and keeps your code from noticing a pressed button.

Learning BlinkWithoutDelay from your IDE examples (can be reduced to 12 lines of code not counting the comments) is the first step in learning real-time code with Arduino. It's funny how hard less than 20 lines of code can be to follow.





Unfortunately if i dont write that down the rgb() will run only once after i press the button, i need it to keep running indefinitely until the button is pressed. The funny thing is that nothing solved it until now, may i give up? That can't be a impossible thing to achieve right?


It is very possible to run rgb() over and over but that leaves you with the next big problem which is button (control) response. Do you want to have to wait for rgb() to finish and have the button pressed for the tiny fraction of a second before rgb() runs again? Or do you want to be able to press a button and get 'instant gratification' ?
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

RogerioBoscolo

Thank you all! Topic solved!  8)

Thanks Thanks Thanks!!
Follow me on instagram to see further projects @rogeriolbf

jack wp


Henry_Best



Code: [Select]
int ledLevel = 25.5;
Novel take on an integer.

I think it is not the problem because the code was working fine until i put the rgb() functions.

Look up the definition of an integer.

GoForSmoke

If you want to make a program, write it twice and throw the first copy away.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

boguz

i think a good simple way you could use to achieve this would be to use Button States.
Take a look here:
http://arduino.cc/en/Tutorial/ButtonStateChange

Basically you could have something like
Code: [Select]

.
.
.
If (buttonState == 0) {   //this means your rgb would be "off"
   if you press your button {
      buttonState = 1;
   }
}
If (buttonState == 1) {   //this means your rgb would be "on"
   if you press your button {
      buttonState = 0;
   }
}

//now you just need to define what you want to be happening on state 0 and state 1
if (buttonState == 0) {
   //what do you want here while the rgb is not playing?
   //maybe a serial print just to see if things are working?
   Serial.println("Waiting for button press");
   //you could use the "blink without delay" idea to make it print only every second or two...   ;)
}
if (buttonState == 1) {
   //whatever you want to happen on state 1
   rgb();   //this will call your rgb function, so it will make it run UNTIL your button state isn't 1 anymore!
}

void rgb() {
   //here you can have the code for your rgbLED thingy...   ;)
   //remember not to use delay() in your rgb function.
}



If you have solved the problem, it would be nice if you post once the full code.
Maybe someday other people will have the same problem and this way they will be able to see how to work it out!
;)

Go Up