i trying add 1 button multi function built aquerium light control string of 1w rgb led's.each channel of rgb controlled pot , works except want use 1 button click exit loop set defined pwm value led's.my problem think sketch counting button clicks when 4 turns off led's pulsing on/off at button count 1 - 3.any guidance appreciated.incidentally code running on attiny44.the code messy works fine except switch count function.
code: [select]
[ const int analoginpin_red = 1;
const int analoginpin_green = 2;
const int analoginpin_blue = 3;
const int analogoutpin_red = 6;
const int analogoutpin_green = 7;
const int analogoutpin_blue = 8;
const int threshold = 120;
const int switchpin = 10;
int sensorvalue_red = 0;
int sensorvalue_green = 0;
int sensorvalue_blue = 0;
int outputvalue_red = 0;
int outputvalue_green = 0;
int outputvalue_blue = 0;
int red_on_led = 0;
int green_on_led = 4;
int blue_on_led = 5;
int switchstate = 0;
int lastswitchstate = 0;
int switchcounter = 0;
void setup() {
pinmode(analoginpin_red, input);
pinmode(analoginpin_green, input);
pinmode(analoginpin_blue, input);
pinmode(analogoutpin_red, output);
pinmode(analogoutpin_green, output);
pinmode(analogoutpin_blue, output);
pinmode(red_on_led, output);
pinmode(green_on_led, output);
pinmode(blue_on_led, output);
pinmode(switchpin, input);
}
void option_1 (){
analogwrite( analogoutpin_red, 85 );
analogwrite( analogoutpin_green, 85 );
analogwrite( analogoutpin_blue, 85 );
}
void option_2 (){
analogwrite( analogoutpin_red, 127 );
analogwrite( analogoutpin_green, 127 );
analogwrite( analogoutpin_blue, 127 );
}
void option_3 (){
analogwrite( analogoutpin_red, 255 );
analogwrite( analogoutpin_green, 255 );
analogwrite( analogoutpin_blue, 255 );
}
void loop() {
switchstate = digitalread(switchpin);
if (switchstate != lastswitchstate) {
if (switchstate == low) {
switchcounter++;
}
}
lastswitchstate = switchstate;
while (switchcounter == 3 ) {
option_3 ();
}
while (switchcounter == 2 ){
option_2 ();
}
while (switchcounter == 1 ){
option_1 ();
}
if(switchcounter >3) switchcounter ==0;
sensorvalue_red = analogread(analoginpin_red);
sensorvalue_green = analogread(analoginpin_green);
sensorvalue_blue = analogread(analoginpin_blue);
outputvalue_red = map(sensorvalue_red, 0, 1023, 0, 255);
outputvalue_green = map(sensorvalue_green, 0, 1023, 0, 255);
outputvalue_blue = map(sensorvalue_blue, 0, 1023, 0, 255);
if (sensorvalue_red > 0 && sensorvalue_red <= threshold ){
analogwrite( analogoutpin_red, 0 );
digitalwrite(red_on_led,high);
delay(100);
digitalwrite(red_on_led,low);
delay(100);
}
else
{
analogwrite( analogoutpin_red,outputvalue_red );
digitalwrite(red_on_led,low);
}
//************************************************
if (sensorvalue_green > 0 && sensorvalue_green <= threshold){
analogwrite( analogoutpin_green, 0 );
digitalwrite(green_on_led,high);
delay(100);
digitalwrite(green_on_led,low);
delay(100);
}
else
{
analogwrite( analogoutpin_green,outputvalue_green );
digitalwrite(green_on_led,low);
}
//************************************************
if (sensorvalue_blue > 0 && sensorvalue_blue <= threshold ){
analogwrite( analogoutpin_blue, 0 );
digitalwrite(blue_on_led,high);
delay(100);
digitalwrite(blue_on_led,low);
delay(100);
}
else
{
analogwrite( analogoutpin_blue,outputvalue_blue );
digitalwrite(blue_on_led,low);
}
} ]
[/code]
code: [select]
while (switchcounter == 3 ) {
option_3 ();
}
if switchcounter equals 3 how ever change value , exit while loop ?
Arduino Forum > Using Arduino > Programming Questions > One button for multi function
arduino
Comments
Post a Comment