Okay, actually the task is a little more complicated, i want ot turn on/off RGB lamp which is changing colours in random sequence
when i press "on" button it starts changing colours, but doesn't turn off when i press the button again
#include <IRremote.h>
#include <Metro.h>
const int irReceivePin =2;
Metro ledMetro = Metro(10) ;
Metro irmetro = Metro(500);
IRrecv irrecv(irReceivePin);
decode_results results;
int prevKey, prevKey2;
float RGB1[3];
float RGB2[3];
float INC[3];
int sw;
int fadeAmount = 5;
int brightness = 0;
int red, green, blue;
int RedPin = 3;
int GreenPin = 5;
int BluePin = 6;
void setup()
{ Serial.begin(9600);
pinMode(irReceivePin, INPUT);
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(sw==1) lamp();
if(sw==0) zero();
if (irrecv.decode(&results))
{Serial.println(results.value);
if (results.value==52160) {
if(sw==1) sw=0;
else sw=1;
irrecv.resume();
}
}
}
void lamp() {
randomSeed(analogRead(0));
for (int x=0; x<3; x++) {
INC[x] = (RGB1[x] - RGB2[x]) / 256; }
for (int x=0; x<256; x++) {
if (ledMetro.check() == 1) {
red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
}}
label:
for (int x=0; x<3; x++) {
RGB2[x] = random(556)-300;
RGB2[x] = constrain(RGB2[x], 0, 255);
}
if(RGB2[0]==0 && RGB2[1]==0 && RGB2[2]==0) goto label;
}
void zero() {
analogWrite (RedPin, 0);
analogWrite (GreenPin, 0);
analogWrite (BluePin, 0);
}
Println results: first line is number of ir button, and then always "0", and nothing when i push the button