If you want to stop cycling, you will have to set the value variable to another value. If you press another button on the remote, that will be done for you in this part of the code (note that I have the resume back where it belongs).
// if remote keypress received
if (irrecv.decode(&results))
{
value = results.value;
irrecv.resume();
}
Because your fading (case code1) ends with an intensity of 0, your led should be off when a different key on the remote is pressed; your code will however not react immediately due to the use of delays but that's another story. So pressing another button should basically work. Does it?
If you want the same button to be fading/off, you need to check what your code is doing now and act based on that.
If the received keypress (results.value) equals code1 and the current value also equals code1, you can set the variable value to 0. If the current value is 0, you can set the variable value to the received keypress (results.value).
In code
// if remote keypress received
if (irrecv.decode(&results))
{
// if currently not fading
if(value == 0)
{
value = results.value;
}
else
{
value = 0;
}
irrecv.resume();
}
No other key turns the Led off or interrupts anything. With the else value = 0 it goes 1 bright to dim and stops. If I change else value to > 0 it will loop as I had planned but no other key press does anything. Thanks for all the help. Current code as follows.
[code#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int ledPin = 5; // Led connected to digital pin 11
int cycle = 75;
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1 41565 // code received from Power Button
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
pinMode(ledPin, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
// variable to remember the last value from the remote
static int value = 0;
// if remote keypress received
if (irrecv.decode(&results))
{
// if currently not fading
if (value == 0)
{
value = results.value;
}
else
{
value > 0;
}
irrecv.resume();
}
// take action
switch (value)
{
case code1:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
analogWrite(ledPin, 255);
delay(strobe);
analogWrite(ledPin, maxFade);
for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
}
}]
I will have to make a test setup to see what is happening; it might be the delay that you use. It might take till the weekend before I will be able to find the time.
I dusted off my demo board; the only thing that I changed in your code in reply #21 is to match the pin numbers to my demo board, used an IR code to match my remote and corrected the value > 0;. I've also added some debug via serial
#include <IRremote.h>
int RECV_PIN = 8; // the pin where you connect the output pin of IR Receiver
int cycle = 75;
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1 0x10EFEB14 // code received from Power Button
int ledPin = 10; // Led connected to digital pin 11
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(57600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
// variable to remember the last value from the remote
static unsigned long value = 0;
// if remote keypress received
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
// if currently not fading
if (value == 0)
{
value = results.value;
}
else
{
value = 0;
}
irrecv.resume();
}
// take action
switch (value)
{
case code1:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
analogWrite(ledPin, fadeValue);
delay(strobe);
analogWrite(ledPin, fadeValue);
for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
}
}
This code works as expected. It cycles when I press and release the button and stops when I press the same button again or when I press any other button.
So time to debug.
1)
Add a Serial.begin in setup()
2)
Add a Serial.print to print the IR code that you receive
Both were already implemented in the above code. Next check (using serial monitor) what you receive when you press the button. Note that I use hex print, you can change HEX to DEC.
The remote that I use sends a repeat code 0xFFFFFFFF if a button is pressed long; if yours also does something like that, that will result in only cycling once.
Note
1)
I used a baudrate of 57600.
2)
Don't forget to change the pin numbers and the code back to what matches your setup.
Yes I did as you said. It just was very sporadic. Didn’t seem to turn on and off consistently. I’ve been doing some more research and it seem the delay is possibly causing problems. May have to switch to millis?? Fade without delay. I’ll keep trying. Thanks again
Well I’m shouting. I’ve still had no luck. I found a fade using using millis() but can’t get it to work with remote. Any thoughts? In my research I came across a guy who used 2 Arduinos. 1 for remote and the other to run LED’s. Do you think that’s the route to take? Thank you
Just trying to get a fade to come on and go off by remote. here's my basic On Off Sketch
[code #include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of TSOP4838
int ledPin = 5;
int itsONled[] = {0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */ #define code1 41565 // code received from pwr button #define code2 57885 // code received from mute button
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(ledPin, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(ledPin, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}][/code]
Here's the fade I'd like to turn on and off
// define directions for LED fade
#define UP 0
#define DOWN 1
int pwmLED=5;
// constants for min and max PWM
const int minPWM = 2;
const int maxPWM = 255;
// State Variable for Fade Direction
byte fadeDirection = UP;
// Global Fade Value
// but be bigger than byte and signed, for rollover
int fadeValue = 0;
// How smooth to fade?
byte fadeIncrement = 4;
// millis() timing Variable, just for fading
unsigned long previousFadeMillis;
// How fast to increment?
int fadeInterval = 130;
void setup() {
// put pwmLED into known state (off)
analogWrite(pwmLED, fadeValue);
}
void doTheFade(unsigned long thisMillis) {
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(pwmLED, fadeValue);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void loop() {
// get the current time, for this time around loop
// all millis() timer checks will use this time stamp
unsigned long currentMillis = millis();
doTheFade(currentMillis);
}
Fix your code tags in reply #36. Post your attempt of the integration. Describe the problem that you have; what do you expect the code to do and what does it do (and not do).