Pages: [1]   Go Down
Author Topic: Moving in and out of a do while loop?  (Read 644 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an IR remote (0 - 9, up, down, left, and right) and I've got a receiver wired up to a few RGB LEDs. The on / off stuff works but the custom sequences I've made only do one round and then stop.

Is there any examples on going into a do while loop and then breaking it when another event happens?

I tried posting all of the code but it exceeded the maximum characters.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I tried posting all of the code but it exceeded the maximum characters.

You can attach it to the post. Click on "Additional Options..." below the text entry box.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See it now. Here you go.

* arduinorgbled.txt (14.59 KB - downloaded 20 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any examples on going into a do while loop and then breaking it when another event happens?
First, explain why you want to use a do/while construct. I've been writing C and C++ code for 25 years, and never used one. Plenty of while loops, but no do/while loops.

A while loop keeps executing until some condition is true or until something in the while loop causes a break.

So, yes, you could use a while loop to execute functions over and over, exiting when a new IR event occurred. Simple call irrecv.decode() in the while loop, and break if it returns true.

You could change your loop function, too. Separate the test for, and decoding of, the IR event from the use of the decoded value.

In the cases where you want to do something only once, set results.value to 0. In the other cases, don't change results.value.

Then, each pass through loop, the action triggered by the last IR event will be repeated, unless the event value was changed to 0.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


So, yes, you could use a while loop to execute functions over and over, exiting when a new IR event occurred. Simple call irrecv.decode() in the while loop, and break if it returns true.

You could change your loop function, too. Separate the test for, and decoding of, the IR event from the use of the decoded value.

In the cases where you want to do something only once, set results.value to 0. In the other cases, don't change results.value.

Then, each pass through loop, the action triggered by the last IR event will be repeated, unless the event value was changed to 0.

Honestly I didn't know what to use. If I separated the decoding and the if/else how would I know what pattern to do? Either I'd have a decoded IR signal with nothing to test it against or I'd have an if/else chain that wouldn't ever get activated.

(reference the irrecv.decode() in the loop) you mean like this?

Code:
while(results.value == 1)
{
    pattern1();
        if( irrecv.decode())
        {
            break;
        }
}
Quote
You could change your loop function, too. Separate the test for, and decoding of, the IR event from the use of the decoded value.

I think I know what you mean.
Code:
void loop()
{
        switch(irrecv.decode(&results))
            {
                case power:
                    power();
                    break;
            }
}

Or am I completely missing something?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I separated the decoding and the if/else how would I know what pattern to do? Either I'd have a decoded IR signal with nothing to test it against or I'd have an if/else chain that wouldn't ever get activated.
I don't understand why.

Code:
unsigned long whatToDo = 0;

void loop()
{
   if(irrecv.decode(&results))
   {
      whatToDo = results.value;
   }

   switch(whatToDo)
   {
      case 0:
         // Don't do anything
         break;
      case up:
         Serial.write(" up ");
         dspeed = dspeed + 100;
         whatToDo = 0;
         break;
      // Other cases
   }
}
Here, deciding whether there is a change in what to do, and doing what needs to (still) be done are separate.

You could press the right button and
Code:
      Serial.write("right");
      whitemedium();
would be executed over and over until you pressed another button.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow that makes sense now. Thanks a lot for your help.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the code so far. Just need to make some awesome patterns now. Thanks again for your help.

It listens for an IR code, does the pattern, and then listens again. There is a bit of a "delay" but it's not bad. The "delay" is due to it not reading IR signals while it does the pattern. Once the pattern is done though, it will automatically go to what button you pressed next.

I'll make a video tomorrow just to show it working.


* arduinorgbled.txt (17.88 KB - downloaded 17 times.)
Logged

Pages: [1]   Go Up
Jump to: