RGB Code not working.
hi, im wanting change colour of rgb led. using fsr (force sensitive resistor), have arduino reading fsr pressure not changing colour of rgb.
any appreciated.
thanks
moderator edit: please use[font=verdana] [[/font]code] [font=verdana][[/font]/code] tags when posting code.
any appreciated.
thanks
code: [select]
/*
* script following:
* 1. gets analog sensor input touch sensor
* 2. processes sensor data in 4 state machine
* 3. adjusts rgb led color based on toouch sensor pressure using pwm
*
* written by: jacek spiewla (jacekspi@umich.edu)
* date created: 2/23/2009
* date modified: 3/17/2009
*/
//setup pins
int senspin = 2; // set touch sensor (analog) input pin on arduino
int redpin = 11; // set pwm (analog) output pin on arduino controlling red anode
int grnpin = 9; // set pwm (analog) output pin on arduino controlling green anode
int blupin = 10; // set pwm (analog) output pin on arduino controlling blue anode
//setup initial values
int val = 0; // initial value touch sensor input
int state = 1; // initial machine state
//initialize variables
int redval; // pulse width variable red anode
int grnval; // pulse width variable green anode
int bluval; // pulse width variable blue anode
void setup() {
pinmode(redpin, output); // set led pins output
pinmode(grnpin, output);
pinmode(blupin, output);
serial.begin(9600);
}
void loop() {
val = analogread(senspin); // read touch sensor values
serial.println(val);
if (state == 1) {sleep();} // turn off led
else if (state == 2) {redin();} // fade in red color
else if (state == 3) {grnin();} // fade in green color
else if (state == 4) {bluin();} // fade in blue color
}
void sleep() {
alloff(); // turn off led
if (val > 20 && val <= 500) {state = 2;} // test low pressure
if (val > 500 && val <= 850) {state = 3;} // test medium pressure
if (val > 850) {state = 4;} // test high pressure
}
void redin() { // function fade in red color , transition other states
grnval = 0;
analogwrite(grnpin, grnval); // turn off green in case last state green
if (redval == 255) { // if red @ maximum, continue light up
analogwrite(redpin, redval);
} else { // else fade in red color
redval ++;
analogwrite(redpin, redval);
}
if (val < 20) {state = 1;} // turn off led if no pressure detected
if (val > 500) {state = 3;} // fade in green if pressure has increased
}
void grnin() { // function fade in green color , transition other states
redval = 0;
bluval = 0;
analogwrite(redpin, redval); // turn off red in case last state blue
analogwrite(blupin, bluval); // turn off blue in case last state blue
if (grnval == 255) { // if green @ maximum, continue light up
analogwrite(grnpin, grnval);
} else { // else fade in green color
grnval ++;
analogwrite(grnpin, grnval);
}
if (val <= 500) {state = 2;} // fade in red if pressure has decrease
if (val >= 850) {state = 4;} // fade in blue if pressure has increased
}
void bluin() { // function fade in blue color , transition other states
grnval = 0;
analogwrite(grnpin, grnval); // turn off green in case last state green
if (bluval == 255) { // if blue @ maximum, continue light up
analogwrite(blupin, bluval);
} else { // else fade in blue color
bluval ++;
analogwrite(blupin, bluval);
}
if (val <= 850) {state = 3;} // fade in green if pressure has decreased
}
void alloff() { // function turn off led
redval = 0;
grnval = 0;
bluval = 0;
analogwrite(redpin, redval);
analogwrite(grnpin, grnval);
analogwrite(blupin, bluval);
}
moderator edit: please use[font=verdana] [[/font]code] [font=verdana][[/font]/code] tags when posting code.
your setting of state inconsistent.
sleep() sets state 1, 2, or 3.
redin() might set 1 or 3.
grnin() might set 2 or 4.
bluin() might set 3.
serial.print() or state in each pass through loop necessary. not jerking state around in functions necessary, too.
sleep() sets state 1, 2, or 3.
redin() might set 1 or 3.
grnin() might set 2 or 4.
bluin() might set 3.
serial.print() or state in each pass through loop necessary. not jerking state around in functions necessary, too.
Arduino Forum > Using Arduino > Programming Questions > RGB Code not working.
arduino
Comments
Post a Comment