Yesss, after a little, I did the blink ![]()
int redPin =11;
int greenPin=10;
int bluPin=9;
int buttonPin=2;
int buttonState=0;
unsigned long previousMillis=0;
unsigned long interval1= 1000;
unsigned long interval2= 3000;
unsigned long interval3= 5000;
unsigned long interval4= 7000;
unsigned long interval5= 9000;
unsigned long interval6= 11000;
unsigned long vuelta=12000;
long int fade_goTime;
int fade_break = 100;
void setColor (int red,int green,int blu)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluPin, blu);
}
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluPin,OUTPUT);
pinMode(buttonPin,INPUT);
fade_goTime = millis();
}
void loop()
{
buttonState=digitalRead(buttonPin);
if (buttonState ==0)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis == interval1)
{
setColor( 255, 0, 0); //red
}
if (currentMillis - previousMillis == interval2)
{
setColor( 0, 255, 0); //green
}
if (currentMillis - previousMillis == interval3)
{
setColor( 0, 0, 255); //blue
}
if (currentMillis - previousMillis == interval4)
{
setColor( 255, 255, 0); //yellow
}
if (currentMillis - previousMillis == interval5)
{
setColor( 0, 255, 255); //aqua
}
if (currentMillis - previousMillis == interval6)
{
setColor( 255, 0, 255); //purple
}
if (currentMillis-previousMillis==vuelta)
{
previousMillis=currentMillis;
}
}
else
{
////////////////FADE
if( millis() >= fade_goTime)
{
static unsigned int rgbColour[3] = {255,0,0}; //Start on red
static int incColour = 1;
static int decColour = 0;
static int i = -1;
// cross-fade the two colours.
i++;
if(i > 255) {
i=0;
decColour ++;
if(decColour >2) decColour = 0;
if (decColour == 2) incColour = 0;
else incColour = decColour +1;
}
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColor(rgbColour[0], rgbColour[1], rgbColour[2]);
fade_goTime = millis() + fade_break;
}
}
}
The problem I'm facing now is with the fade, I just copied a fade code I found here and pasted it, did some tweaks and put it at work, the problem is when I push the button to change, it starts to fade but blinks once in a while :S What could be the problem now?
Thank you for the help ![]()
Also, they may be another way to do the blinking code shorter and easier, but I can't think how.