Hi everyone, I am trying to complete my first arduino project. But now I have a problem with led. I would like my led to fade in when remote control button pressed. When it stays on, you can rotate the rotary encoder to change the brightness. Then press the button again to fade it out.
Now the issue is, when I hit the button, the brightness goes from 0 to 50. It stays at 50 forever even if i press the button again and again. When rotating encoder, it jumps to 49 then quickly go back to 50 again. I reckon there must be some conflict commands over there.
I'd really appreciate it if someone would help me out. Thanks in advance.
//Include the library
#include <IRremote.h>
int RECV_PIN = 2;
int toggleState = 0;
int itsON[] = {0, 0, 0, 0};
IRrecv irrecv(RECV_PIN);
decode_results results;
#include <Encoder.h>
Encoder myEnc(6, 7); //(outA, outB)
//define led pin
const int ledPin = 5;
//brightness adjustment
long oldPosition = -999;
long newPosition;
int positionDifference = 0;
bool ledState = 0;
int ledBrightness = 0;
//fading led
boolean fadeInOut = true;
int
fadeMin = 0, // >= 0
fadeMax = 50, // <= 255
fadeValue = fadeMin,
fadeInStep = 5,
fadeOutStep = 5
;
//without delay
unsigned long previousMillis = 0;
const int led_In_Interval = 300;
const int led_Out_Interval = 400;
unsigned long currentMillis = 0;
unsigned long previousLed_In_Millis = 0;
unsigned long previousLed_Out_Millis = 0;
//setup
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); //12500
irrecv.enableIRIn();
} //end of setup()
//loop
void loop() {
//ir receiver
if (irrecv.decode(&results)) {
switch(results.value){
//other task1
case 0xE318261B: //buttonCH-
if (itsON[1] == 1){
//task
itsON[1]=0;
} else {
//task
itsON[1]=1;
}
break;
//other task2
case 0xEE886D7F: //button CH+
if (itsON[2] == 1){
//task
itsON[2]=0;
} else {
//task
itsON[2]=1;
}
break;
//toggle led
case 0x52A3D41F: //button <
if (itsON[3] == 1){
ledState = 1;
itsON[3]=0;
} else {
ledState = 0;
itsON[3]=1;
}
break;
} //end of switch
irrecv.resume();
}
currentMillis = millis ();
//ledState
if(ledState == 1){
//fadein
if ( currentMillis - previousLed_In_Millis >= led_In_Interval ){
if ( fadeInOut ){
fadeValue += fadeInStep;
if ( fadeValue > fadeMax ){
fadeValue = fadeMax;
fadeInOut = false;
}
analogWrite ( ledPin, fadeValue );
}
previousLed_In_Millis += led_In_Interval;
} //end of fadein
//use encoder to brightness
if (fadeInOut == false){
ledBrightness = fadeValue; //bring fadeValue to ledBrightness
newPosition = myEnc.read();
if (newPosition != oldPosition) {
positionDifference = newPosition - oldPosition;
oldPosition = newPosition;
}
ledBrightness = ledBrightness - positionDifference;
ledBrightness = constrain(ledBrightness, 0, 50);
positionDifference = 0;
analogWrite(ledPin, ledBrightness);
}
}else{ // if ! (ledState == 1)
//fadeout
fadeValue = ledBrightness; //bring ledBrightness to fadeValue
if ( currentMillis - previousLed_Out_Millis >= led_Out_Interval){
if (fadeInOut ==false){
fadeValue -= fadeOutStep;
if ( fadeValue < fadeMin ){
fadeValue = fadeMin;
fadeInOut = true;
}
} //end of fadeInOut
previousLed_Out_Millis += led_Out_Interval;
analogWrite ( ledPin, fadeValue );
} //end of fadeout
} //end of ledState
Serial.println(ledBrightness);
Serial.print("\t");
Serial.println(fadeValue);
} //end of loop()