Hey all
I'm doing a fairly simple RGB style lamp with several modes,
anyway I've managed to do most of the hard stuff and its working the way I want it to, but there is an issue with when I want to change modes.
so in the void loop there is code to change the mode from 1 to 0 depending on its previous state and whether I have pressed the momentary switch or not.
well the mode changes from mode 0 to mode 1 no problem but I cant change it back, I'm guessing the RGB pattern cycle programmed is interfering with reading the input of the switch. An interesting thing to note is when I remove this from the main loop:
if (mode==1){
crossFade(red);
crossFade(green);
crossFade(blue);
crossFade(yellow);
}
The button works perfectly but naturally the RGB pattern cycle code is broken. Any help would be appreciated, many thanks
Chris.
//RED ints
int REDpot = 0; // select the input pin for the potentiometer
int REDpin = 9; // select the pin for the RED LED (9)
int REDval = 0; // variable to store the value coming from the sensor;
int REDcount = 200;
//GREEN ints
int GREENpot = 1; // select the input pin for the potentiometer
int GREENpin = 10; // select the pin for the GREEN LED(10)
int GREENval = 0; // variable to store the value coming from the sensor
int GREENcount = 200;
//BLUE ints
int BLUEpot = 5; // select the input pin for the potentiometer
int BLUEpin = 11; // select the pin for the BLUE LED(11)
int BLUEval = 0; // variable to store the value coming from the sensor
int BLUEcount = 200;
//Encoder Pins these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int count = 200;
volatile int lastEncoded = 0;
volatile long brightness = 0;
long lastbrightness = 0;
int lastMSB = 0;
int lastLSB = 0;
//pattern ints
int mode=0;
const int modebutton=4;
int modestate=0;
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 100, 100, 100 };
int red[3] = { 100, 0, 0 };
int green[3] = { 0, 100, 0 };
int blue[3] = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.
// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];
int wait = 10; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
int calculateVal(int step, int val, int i) {
if (mode==1){
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
}
//end of pattern ints
void setup() {
pinMode(REDpin, OUTPUT);
pinMode(GREENpin, OUTPUT);
pinMode(BLUEpin, OUTPUT);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
pinMode(modebutton, INPUT);
digitalWrite(encoderPin1, HIGH);
digitalWrite(encoderPin2, HIGH);
digitalWrite(modebutton, HIGH);
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
Serial.begin(9600);//9600
Serial.flush();
}
//Encoder Update
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if (brightness < 99){
if(sum == 0b1101 && brightness < 100) brightness ++;
}
if (brightness > 1){
if(sum == 0b1110 && brightness > 0) brightness --;
}
lastEncoded = encoded; //store this value for next time
}
void loop() {
//mode state
modestate=digitalRead(modebutton);
if (modestate == HIGH && mode==0){
mode=1;
Serial.print("mode 1");
delay (500);
}
else if (modestate == HIGH && mode==1){
mode=0;
Serial.print("mode 0");
delay (500);
}
if (mode==1){
crossFade(red);
crossFade(green);
crossFade(blue);
crossFade(yellow);
}
else if (mode==0){
//RED pot loop
REDval = round((analogRead(REDpot))/4.05)-brightness*2.6; // read the value from the sensor
if (REDval<0){
REDval=0;
}
analogWrite(REDpin, REDval); // turn the ledPin on
if (REDcount != REDval){
Serial.print("Red pot value=");
Serial.println(REDval);
REDcount = REDval;
}
//GREEN pot loop
GREENval = round((analogRead(GREENpot))/10.23)-brightness*1.03; // read the value from the sensor
if (GREENval<0){
GREENval=0;
}
analogWrite(GREENpin, GREENval); // turn the ledPin on
if (GREENcount != GREENval){
Serial.print("Green pot value=");
Serial.println(GREENval);
GREENcount = GREENval;
}
//BLUE pot loop
BLUEval = round((analogRead(BLUEpot))/8.1)-brightness*1.3; // read the value from the sensor
if (BLUEval<0){
BLUEval=0;
}
analogWrite(BLUEpin, BLUEval); // turn the ledPin on
if (BLUEcount != BLUEval){
Serial.print("pot value=");
Serial.println(BLUEval);
BLUEcount = BLUEval;
}
//Encoder
if (count != brightness){
Serial.print("encoder possition=");
Serial.println(brightness);
count=brightness;
}
}
}
void crossFade(int color[3]) {
if (mode==1){
if (repeat) { // Do we loop a finite number of times?
j += 1;
if (j >= repeat) { // Are we there yet?
exit(j); // If so, stop.
}
}
// Convert to 0-255
int R = (color[0] * 255) / 100;
int G = (color[1] * 255) / 100;
int B = (color[2] * 255) / 100;
int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);
for (int i = 0; i <= 1020; i++) {
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);
analogWrite(REDpin, redVal); // Write current values to LED pins
analogWrite(GREENpin, grnVal);
analogWrite(BLUEpin, bluVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}
}