Go Down

Topic: A little help with digitalRead (Read 474 times) previous topic - next topic

OO00dougal00OO

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

Code: [Select]

//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

Code: [Select]

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

PaulS

Code: [Select]
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;}

Why are you declaring a new variable in the body of the if statements. The new variable goes out of scope at the }.

retrolefty

Your problem might also be not wiring up your switches properly and you are suffering from 'floating input pin' condition. You need to have proper pull-ups or pull-down resistors, the exact configuration depends on how you have wired up your switches. So show us how you have it now and we can show you how to fix it. This is a very common electronics beginners type mistake.

Lefty

holmes4

You have a scope problem

Code: [Select]

if (rot1 == HIGH && rot2 == HIGH && rot3 == HIGH && rot4 == LOW) {int prog_number = 1;}



prog_number only exists between the {} the next int prog_number creates a different variable, in this case just drop the "int".

Mark

billroy

As Holmes says, put:

Code: [Select]

   int prog_number;

before the giant if statement, and don't say "int prog_number=x" just "prog_number=x" in the if statement blocks.

It looks like you are setting prog_number to 1 in the fourth giant if statement.  Is that what you intended?

-br


OO00dougal00OO

Thanks all for your replies!

I have the pullup resistors in the circuit and have verified they are working correctly, i'm getting 2 low's coming in as i have split the ground for the switch into 2 and only just noticed one is connected :-s (it was easier to plug into the breadboard)

Looks like it was a noobie coding error. I have removed the int's in void loop and its working great!! also the 4th line with prog_number = 1 was a typo, should have been 4.

Thanks very much for your help!

Go Up