Hi,
Recently I made a code to work with one simple led where it interacts with a reed switch. So when the magnet is nearby, a led produces continuous light, and when the magnet is taken away a led slowly fades out. However, for my final idea, I need a led to fade from yellow to red while fading out completely at the same time. So far I managed to only make yellow and fade it out. Does anyone know how I could adjust the colours while still having this smooth fading out effect?
int ledPinR = 11;
int ledPinG = 10;
int ledPinB = 9;
int reedPin = 2;
int brightness = 0;
int fadeAmount = 5;
unsigned long timestamp = 0;
void setup(){
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(reedPin, INPUT_PULLUP);
}
void loop(){
if(millis()-timestamp > 30){
brightness = brightness - fadeAmount;
if(brightness < 0) brightness = 0;
timestamp = millis();
}
if(!digitalRead(reedPin)){
brightness = 255;
}
analogWrite(ledPinR, brightness);
analogWrite(ledPinG, brightness);
}