Hiya, I am working on a project to basically run as a light chaser for a top hat. I am running into issues reading a set of 4 digital pins and setting a variable from that. These pins are connected to a rotary switch which will in turn select which light chase to run on the hat.
What i am seeing is that even though the pin states (rot1, rot2, rot3 and rot4) are changing correctly the "prog_number" variable does not change as i expect it to. Please see code and output below
//ROTARY SWITCH PINS
int rotpin1 = 7;
int rotpin2 = 8;
int rotpin3 = 12;
int rotpin4 = 13;
//VALUES
int prog_number = 0;
void setup ()
{
//setup serial
Serial.begin(9600);
//Setup Rotary Switch Inputs
pinMode (rotpin1, INPUT);
pinMode (rotpin2, INPUT);
pinMode (rotpin3, INPUT);
pinMode (rotpin4, INPUT);
}
void loop ()
{
//debug delay - Remove for live
delay(3000);
//Program Selection
//Read the Pins and set the variables
int rot1 = digitalRead(rotpin1);
int rot2 = digitalRead(rotpin2);
int rot3 = digitalRead(rotpin3);
int rot4 = digitalRead(rotpin4);
//Run If statements to set the prog_number variable which in turn will select the chase.
if (rot1 == LOW && rot2 == HIGH && rot3 == HIGH && rot4 == LOW) {int prog_number = 1;}
if (rot1 == HIGH && rot2 == LOW && rot3 == HIGH && rot4 == LOW) {int prog_number = 2;}
if (rot1 == HIGH && rot2 == HIGH && rot3 == LOW && rot4 == LOW) {int prog_number = 3;}
if (rot1 == HIGH && rot2 == HIGH && rot3 == HIGH && rot4 == LOW) {int prog_number = 1;}
//The other way i was doing it, gave the same result
//if (digitalRead(rotpin1) == HIGH && digitalRead(rotpin2) == LOW && digitalRead(rotpin3) == HIGH && digitalRead(rotpin4) == LOW) {int prog_number=2;}
//if (digitalRead(rotpin1) == HIGH && digitalRead(rotpin2) == HIGH && digitalRead(rotpin3) == LOW && digitalRead(rotpin4) == LOW) {int prog_number=3;}
//if (digitalRead(rotpin1) == HIGH && digitalRead(rotpin2) == HIGH && digitalRead(rotpin3) == HIGH && digitalRead(rotpin4) == LOW) {int prog_number=4;}
//Serial output to see what the thing is doing
Serial.print(rot1);
Serial.print(rot2);
Serial.print(rot3);
Serial.print(rot4);
Serial.print(" ");
Serial.println(prog_number);
}
and here is the output
1100 0
1100 0
1010 0
1010 0
0110 0
0110 0
1110 0
1110 0
I have been playing with this for about a day now and its doing my head in, any help would be greatly appreciated.
Thanks
Dougal