Go Down

### Topic: Newb help with ending a task (Read 1 time)previous topic - next topic

#### Sparkinman

##### Jan 29, 2011, 10:20 pm
Okay, I've been reading all the books, and doing the exercises, and am now a little stumped!  In the book Beginning Arduino project 8 (which is the mood lamp) I thought to myself "wouldn't it be cool to turn this on and off with a remote control".  So I understand the remote part, and I can activate the mood lamp with it, but how would I stop the mood lamp from running with another ir command?  I've tried a few different ways, and I'm sure it's probably about putting the IR receive in the right place in the mood lamp code to make it stop..I just can't figure it out.  I could post code..but it's such a mess from my "trials" I doubt it would be very easy to follow..any thoughts?

#### Sparkinman

#1
##### Jan 29, 2011, 10:53 pm
It's not that the code is that bad, it's just that the moodlamp portion is basically a "forever" loop of changing colors, how or where should I interrupt this?
Code: [Select]
`#include <IRremote.h>int RECV_PIN = 12;float RGB1[3];float RGB2[3];float INC[3];int red, green, blue;int RedPin = 5;int GreenPin = 10;int BluePin = 9;int codevalue;IRrecv irrecv(RECV_PIN);decode_results results;void setup(){ irrecv.enableIRIn(); randomSeed(analogRead(0));RGB1[0] = 0;RGB1[1] = 0;RGB1[2] = 0;RGB2[0] = random(256);RGB2[1] = random(256);RGB2[2] = random(256);}void loop(){  if (irrecv.decode(&results)){  codevalue=results.value;    switch(codevalue) {       case -30601:      randomSeed(analogRead(0));for (int x=0; x<3; x++) {INC[x] = (RGB1[x] - RGB2[x]) / 256; }for (int x=0; x<256; x++) {red = int(RGB1[0]);green = int(RGB1[1]);blue = int(RGB1[2]);analogWrite (RedPin, red);analogWrite (GreenPin, green);analogWrite (BluePin, blue);delay(100);RGB1[0] -= INC[0];RGB1[1] -= INC[1];RGB1[2] -= INC[2];}for (int x=0; x<3; x++) {RGB2[x] = random(556)-300;RGB2[x] = constrain(RGB2[x], 0, 255);delay(1000);}break;    }    irrecv.resume();   }}`

#2
##### Jan 29, 2011, 11:00 pm
Why not just add another case:

switch(codevalue) {

case -30601:  << some other value that you receive
// put the lamp in the 'off' state
break;
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Sparkinman

#3
##### Jan 29, 2011, 11:55 pm
I tried that, but once in the loop of color change, all IR becomes unresponsive (the ir resume comes after the case)..any other thoughts?

#4
##### Jan 30, 2011, 12:09 am
You've got all those delays in there.
Does this
irrecv.resume();
act like the Serial port, or is it only read at the time it is executed?
If the latter, sprinkle some more in & act on the data (i.e. set a flag and then break out if the flag is set).
Or, rewrite the code to be loop that checks the elapsed time and does the writes & variable updates every 10mS or something so you can read the IR data in a more timely manner (i.e. the Blink Without Delay example).
I do the same for a scoring machine - I have a loop that runs in about 100microseconds - each pass thru, it checks for Serial port activity, wireless RF input, user inputs, if 1 second has gone by the time display is updated, etc.
You could do similar here. You have stuff that happens every 100mS, and stuff that happens every 1000mS - plenty of time to look for IR inputs in between.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Sparkinman

#5
##### Jan 30, 2011, 12:20 am
I'm not sure, I added a few more to no avail throughout the runtime, and it still isn't affecting it.  I tried repeating my original irrec.decode again with a different case in the middle, and it still isn't catching it..any other thoughts?
Code: [Select]
`#include <IRremote.h>int RECV_PIN = 12;float RGB1[3];float RGB2[3];float INC[3];int red, green, blue;int RedPin = 5;int GreenPin = 10;int BluePin = 9;int codevalue;IRrecv irrecv(RECV_PIN);decode_results results;void setup(){ irrecv.enableIRIn(); randomSeed(analogRead(0));RGB1[0] = 0;RGB1[1] = 0;RGB1[2] = 0;RGB2[0] = random(256);RGB2[1] = random(256);RGB2[2] = random(256);}void loop(){  if (irrecv.decode(&results)){  codevalue=results.value;     switch(codevalue) {       case -30601:      randomSeed(analogRead(0));for (int x=0; x<3; x++) {INC[x] = (RGB1[x] - RGB2[x]) / 256; }for (int x=0; x<256; x++) {red = int(RGB1[0]);green = int(RGB1[1]);blue = int(RGB1[2]);analogWrite (RedPin, red);analogWrite (GreenPin, green);analogWrite (BluePin, blue); irrecv.resume(); if (irrecv.decode(&results)){  codevalue=results.value;   switch(codevalue) {case 18615:analogWrite (RedPin,0);analogWrite (GreenPin, 0);analogWrite (BluePin, 0);break; } }delay(100);RGB1[0] -= INC[0];RGB1[1] -= INC[1];RGB1[2] -= INC[2];}for (int x=0; x<3; x++) {RGB2[x] = random(556)-300;RGB2[x] = constrain(RGB2[x], 0, 255);delay(1000);}break;case 18615:analogWrite (RedPin,0);analogWrite (GreenPin, 0);analogWrite (BluePin, 0);break;    }    irrecv.resume();   }}`

Go Up