hello all, i just made this project by using switch function, all three leds turn on one by one, that worked perfect, but when i add two more buttons for color mixing it stop working, i,m not familiar with millis function and i hope that can fix this problem. any help will be appreciated, thanks in advance, here is the code link,
int GREEN = 4; //Led's and pins
int RED = 2;
int BLUE = 5;
const byte button = 12;
const byte button2 = 3;
const byte button3 = 13;
int button2State;
int button3State;
int newcount;
int count = 0;
int dimmer = 0;
void setup() {
// put your setup code here, to run once:
pinMode(GREEN, OUTPUT); //Pinmodes of the leds
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(button,INPUT);
pinMode(button2,INPUT_PULLUP);
pinMode(button3,INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
button2State = digitalRead(button2);
button3State = digitalRead(button3);
if (button2State == HIGH){
dimmer=dimmer+1;
}
if (dimmer<2){
dimmer=2;
}
if (button3State == HIGH){
dimmer=dimmer-1;
}
if (dimmer>1023){
dimmer=1023;
}
if (digitalRead(button) == LOW)
{
newcount=count+1;
if (newcount!=count)
{
Serial.println(newcount);
switch (newcount)
{
case 1 : analogWrite(GREEN, dimmer);
break;
case 2 : analogWrite(BLUE, dimmer);
break;
case 3 : analogWrite(RED, dimmer);
break;
default : analogWrite(GREEN, 255);
analogWrite(BLUE, 255);
analogWrite(RED, 255);
newcount=0;
break;
}
count=newcount;
}
delay(300);
}
}
The buttons act while pressed, not when pressed/or released,
this does not allow a free flow of loop, the increments for example would be much too fast.
(Added: they probably are, the delay is active only under some conditions,
which is hard to see, with that bad formatting. Try Ctrl-T in the IDE.)
Which Arduino do you use?
Does it have a ten bit PWM or DAC to be used by analogWrite?
Here is a version using StateChangeDetection for the buttons. I always use a 10ms delay for debounce and have never had a problem. For most applications a 10ms delay when a button is pressed should not be a problem. I couldn't test it but if there is any problem it will be which button state corresponds to "pushed".
const byte GREEN = 4; //Led's and pins
const byte RED = 2;
const byte BLUE = 5;
const byte button1 = 12;
const byte button2 = 3;
const byte button3 = 13;
int newcount = 0;
int count = 0;
int dimmer = 0;
void setup()
{
pinMode(GREEN, OUTPUT); //Pinmodes of the leds
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
}
void loop()
{
static int lastButton1State = digitalRead(button1);
static int lastButton2State = digitalRead(button2);
static int lastButton3State = digitalRead(button3);
int button1State = digitalRead(button1);
int button2State = digitalRead(button2);
int button3State = digitalRead(button3);
if (button2State != lastButton2State)
{
delay(10); //debounce
lastButton2State = button2State;
if (button2State == LOW)
{
dimmer++;
if (dimmer > 1023) dimmer = 1023;
}
}
if (button3State != lastButton3State)
{
delay(10); //debounce
lastButton3State = button3State;
if (button3State == LOW)
{
dimmer--;
if (dimmer < 2) dimmer = 2;
}
}
if (button1State != lastButton1State)
{
delay(10); //debounce
lastButton1State = button1State;
if (button1State == LOW)
{
newcount++;
}
}
if (newcount != count)
{
Serial.println(newcount);
switch (newcount)
{
case 1 :
analogWrite(GREEN, dimmer);
break;
case 2 :
analogWrite(BLUE, dimmer);
break;
case 3 :
analogWrite(RED, dimmer);
break;
default :
analogWrite(GREEN, 255);
analogWrite(BLUE, 255);
analogWrite(RED, 255);
newcount = 0;
break;
}
count = newcount;
}
}