Checking a previously printed value

is their a method to recheck a previously sent value from the log? I am receiving a value from an agent, and it sees the variable, goes through the loop, and stops there. i’ve tried switch cases, returns, but not having the best of luck.

code:

`//////////////////F I N A L C O D E //////////////////////////////////

imp.configure(“RGB LED 1”,[],[]);
hardware.pin9.configure(PWM_OUT, 0.02, 0.0);
hardware.pin7.configure(PWM_OUT, 0.02, 0.0);
hardware.pin8.configure(PWM_OUT, 0.02, 0.0)
led <- hardware.pin2;
led.configure(DIGITAL_OUT);
led.write(0);

function setLed(value) {
led.write(value);
server.log(value);

switch(value){

case 1:
for(local fade=0; fade<=256.0; fade++){
hardware.pin9.write(fade/256.0);
imp.sleep(0.01);
}
for(local fade=256.0; fade>=0; fade–){
hardware.pin9.write(fade/256.0);
imp.sleep(0.01);
}
break;

  case 0:

server.log(value);
for(local fade=0; fade<=256.0; fade++){
hardware.pin7.write(fade/256.0);
imp.sleep(0.01);
}
for(local fade=256.0; fade>=0; fade–){
hardware.pin7.write(fade/256.0);
imp.sleep(0.01);
}

}
}

agent.on(“setLed”, setLed);`

What do you mean by “recheck a value sent from the log”?

Your code looks like it’s doing some weird things:

In the SetLed function, you turn the LED on or off, but then immediately go into the fade sequence. This is going to happen so fast you won’t notice that initial state change for the LED.

Also, both of your switch statements are identical, I’m not sure if that’s intentional or not, but if it is - you don’t need the switch statement, you can just have the code.

Finally, it’s going to take just over 5 seconds (~5.12 to b exact) for the LEDs to fade in, and then back out again. If you’re sending “setLed” more frequently than that… weird things might start happening.

Can you explain what you’re trying to do with your RGB led? How do you want it to behave? We might be able to help out more if you can explain in a bit more detail what you’re trying to accomplish :slight_smile:

Thanks for the feedback. I revised the code and I’ll paste/explain below:

`
//////////////////F I N A L C O D E //////////////////////////////////
values <- 0;
changes <- 1;

imp.configure(“RGB LED 1”,[],[]);
hardware.pin9.configure(PWM_OUT, 0.02, 0.0);
hardware.pin7.configure(PWM_OUT, 0.02, 0.0);
hardware.pin8.configure(PWM_OUT, 0.02, 0.0)
led <- hardware.pin2;
led.configure(DIGITAL_OUT);
led.write(0);

function poll()
{

values += changes;
if (values == 255) {
   changes = changes-1;

} else if (values == 0) {
changes = changes+1;
}

hardware.pin9.write(values/256.0);
imp.wakeup(0.01, poll);

}
// value - 1 or 0
function setLed(value) {
led.write(value);

if(value==1 ){

   hardware.pin8.write(0);

hardware.pin9.write(128.0);
hardware.pin7.write(39.0);

}else{
hardware.pin8.write(0);
hardware.pin9.write(0);
hardware.pin7.write(0);

}
}

agent.on(“setLed”, setLed);
poll();`

So this is almost behaving the way I want. I have two imps communicating with each other. one has a button, the other an RGB led. (using your agent example and everything works fine). right now, the code has a continuos loop of the function “poll” which is fading an LED. my function below, " setLed" is receiving the agent value, and i am applying conditionals like so.

This is not what I want to happen. I want to trigger different fade events (exactly like the fade event from the “set” function) when the imp receives a “1” or “0” from the agent. specifically, I have a default fade in/out of green, then when button is pressed (agent sending 1), same pattern of fade changes to fade in/out of red, and when released, defaults to green.

im not sure if you are confusing the LED on pin 2 with anything… thats just an indicator for me when i had pressed button.

I have been working at this for hours, reading the API and am still coming up dry. if anyone can point me in the right direction it would be a great help. here is a somewhat strippd version of the code with comments.

`
values <- 0;
changes <- 1;

imp.configure(“RGB LED 1”,[],[]);
hardware.pin9.configure(PWM_OUT, 0.02, 0.0);
hardware.pin7.configure(PWM_OUT, 0.02, 0.0);
hardware.pin8.configure(PWM_OUT, 0.02, 0.0)
led <- hardware.pin2;
led.configure(DIGITAL_OUT);
led.write(0);

function poll()
{

values += changes;
if (values == 255) {
   changes = changes-1;

} else if (values == 0) {
changes = changes+1;
}

hardware.pin9.write(values/256.0);

imp.wakeup(0.01, poll);

}

// value - 1 or 0
function setLed(value) {
led.write(value); //simply a test LED

server.log(value);

if(value==1){

//THIS IS WHERE I WANT TO TAKE THE EVENTS OF poll(); AND SIMPLY CHANGE THE COLOR.CALLING poll() ITSELF JUST SPEEDS UP THE SKETCH/OVERLOADS.

}

}

poll(); //THIS IS LOOPING FINE.
agent.on(“setLed”, setLed);
`