Go Down

Topic: Moving in and out of a do while loop? (Read 723 times) previous topic - next topic

Dustin

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.

dxw00d

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.

Dustin

See it now. Here you go.

PaulS

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.

Dustin



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: [Select]

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: [Select]

void loop()
{
        switch(irrecv.decode(&results))
            {
                case power:
                    power();
                    break;
            }
}


Or am I completely missing something?

PaulS

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: [Select]
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: [Select]
      Serial.write("right");
      whitemedium();

would be executed over and over until you pressed another button.

Dustin

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

Dustin

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.


Go Up