Go Down

Topic: Swich Case with nested if statement problems (Read 565 times) previous topic - next topic

AgentNoise

Oct 24, 2013, 02:47 am Last Edit: Oct 24, 2013, 03:00 am by AgentNoise Reason: 1
I have a sketch set up to run switch case statement base on voltage of a potentiometer.  I have it set so case 0 has an if statement to light up LED's if the button is pressed.  For some reason  the LED flicker on every 6 seconds or so and then turn back off even when nothing is attached to A5(the pin going to the button).  If I change the potentiometer to the highest voltage the LEDS don't light up.   I am not sure how it is getting an analog reading even when no cable is plugged into the A5 pin. Also, when the LEDs are off I can push the button and they turn on, as soon as I let got they turn off again.  This is very odd, any ideas?

Here is the code

Code: [Select]
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 1023;    // sensor maximum, discovered through experiment
const int pentometerPin = A0;// pin that the pentometer is attached to
const int buttonApin = A5; // pin that the button is attached to
const int ledPin = 8;
const int ledPin2 = 9;

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(buttonApin, INPUT);
 // initialize serial communication:
 Serial.begin(9600);  
}

void loop() {
 // read the sensor:
 int sensorReading = analogRead(A0);
 int buttonstate = analogRead(buttonApin);
 // map the sensor range to a range of four options:
 int range = map(sensorReading, sensorMin, sensorMax, 0, 6);

 // do something different depending on the
 // range value:
 switch (range) {
 case 0:
   if(buttonstate == LOW)
   {
     digitalWrite(ledPin, HIGH);
     digitalWrite(ledPin2, HIGH);
     Serial.println("Button Pressed");
   }
   else
   {
     digitalWrite(ledPin, LOW);
     digitalWrite(ledPin2, LOW);
   }
     // your hand is on the sensor
   Serial.println("lowest");
   break;
 case 1:    // your hand is close to the sensor
   Serial.println("lower");
   break;
 case 2:    // your hand is a few inches from the sensor
   Serial.println("low");
   break;
 case 3:    // your hand is nowhere near the sensor
   Serial.println("medium");
   break;
 case 4:
   Serial.println("high");
    break;
 case 5:
    Serial.println("higher");
    break;
  case 6:
  Serial.println("highest");
  break;
 }
 delay(1);        // delay in between reads for stability
}


Here is the layout:  https://www.dropbox.com/sc/tqohro3z936ss9e/iVcYHNqg-j

mrburnette

#1
Oct 24, 2013, 02:50 am Last Edit: Oct 24, 2013, 02:52 am by mrburnette Reason: 1
OMG ... Quick before one of the old-guard fusses... Edit to include CODE TAGS before-after your code :smiley-eek-blue:

Like thus:

Quote

Code: [Select]


// Hint: it us the # symbol on the toolbar.
...
...


AgentNoise

Ohh, didn't know that was a thing.  Fixed it

ehrja

So, which is the desired behavior?

I guess you miss a pull up resistor, it needs to bee in the button:

http://www.tinkerhobby.com/pull-up-resistors/
28.11.2014  &#8224Roberto Gómez Bolaños, Chespirito.   Q.P.D.
¿Y ahora quien podra defendernos?

AgentNoise

It is supposed to only light the LEDs when I push the button.  How ever the LEDS light up even when there is nothing on the A5 pin. I can literally remove the cable from the arduino and it still lights the LEDs up randomly.  I have changed the A5 from INPUT to INTPUT_PULLUP but then nothing happens.  Wont light up period

ehrja

you need to put a 10k resistor from vcc to the A5, the same pin which has the button, check this picture (3):
28.11.2014  &#8224Roberto Gómez Bolaños, Chespirito.   Q.P.D.
¿Y ahora quien podra defendernos?

CrossRoads

Change this line
pinMode(buttonApin, INPUT);

pinMode(buttonApin, INPUT_PULLUP); // input with internal pullup enabled

Wire the button to connect to Gnd when pressed.
Then:

const int buttonApin = 19; // pin that the button is attached to - A5 = D19
int buttonstate = digitalRead(buttonApin);
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AgentNoise


Change this line
pinMode(buttonApin, INPUT);

pinMode(buttonApin, INPUT_PULLUP); // input with internal pullup enabled

Wire the button to connect to Gnd when pressed.
Then:

const int buttonApin = 19; // pin that the button is attached to - A5 = D19
int buttonstate = digitalRead(buttonApin);


That works!  I know its a bit much to ask but can you explain why it works?  Does digital read not float or something?   Does INPUT_PULLUP only work with digital read?

Also,  if you happen to know a good tutorial on pull up resistors that would be awesome.  They seem to be important, and I can't find a decent guide(or haven't yet anyways)

CrossRoads

"tutorial on pull up resistors"  Huh? Just enable them to hold the pin at a steady HIGH when not grounded.

I think it works because this
int buttonstate = analogRead(buttonApin);
returns a value of 0-1023, and you were checking for
buttonstate == LOW,
which doesn't really make sense.
digitalRead looks for HIGH, LOW, so that makes more sense.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up