Pages: [1]   Go Down
Author Topic: Swich Case with nested if statement problems  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: October 23, 2013, 08:00:11 pm by AgentNoise » Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1777
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

// Hint: it us the # symbol on the toolbar.
...
...
« Last Edit: October 23, 2013, 07:52:48 pm by mrburnette » Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 12
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 12
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you need to put a 10k resistor from vcc to the A5, the same pin which has the button, check this picture (3):
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26570
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26570
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: