Pages: [1]   Go Down
Author Topic: Newb help with ending a task  (Read 958 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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();   
}
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not just add another case:

    switch(codevalue) {
   
   case -30601:  << some other value that you receive
// put the lamp in the 'off' state
break;
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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();   
}
}
Logged

Pages: [1]   Go Up
Jump to: