Pages: [1]   Go Down
Author Topic: switch / case not switching as expected...need second pair of eyes  (Read 656 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I am using the switch/case command but it isn't triggering. Variable -suffix- is always printing "0". Variable -iVal- prints correct values as the pot is changed. Please see sketch and see what I am missing.
The code is really just the pertinent parts of a *much* longer sketch. Not really necessary to post for this problem. I am using the MEGA2560R3. The pot is a 10K connected to MEGA 5V and GND with the wiper to analog pin 8. I omitted 7 of the 10 cases just to shorten this post. They are repetitive.
The button scanner, as I call it, also uses switch/case which is functioning perfectly. I'm stumped.
Thank you,
1oldfox
"If it ain't broke, fix it 'til it is."


Code:
int irisValuePin = 6;
int analogPin = 8;
int suffix;
String baseString;
int iVal;
int i;

void setup()
{
  Serial.begin(9600, SERIAL_8E1); 
  pinMode(irisValuePin, INPUT_PULLUP); 
}
 
void loop()
{
  IrisValue();
}

void IrisValue()    //button 5 -- Dpin 6
{
  iVal = analogRead(analogPin);          //changes with pot position... verified GOOD
  Serial.print("iVal = ");
  Serial.println(iVal);
  delay(1);
  switch(iVal)
  {
    case 1: //F1.6
    if (iVal > 50 & iVal < 150)
    { suffix = 0; }
    break;
   
    case 2: //F2.2
    if (iVal > 150 & iVal < 250)
    { suffix = 1; }
    break;

    //
    //
    //

    case 10: //F34
    if (iVal > 950 & iVal < 1023)
    { suffix = 9; }
    break;   
  }
  baseString = "W123456";
  Serial.println(baseString);
  Serial.println(suffix);
  baseString += suffix;
  Serial.println(baseString);
  Serial.print("\n");
  delay(1000);
  while (digitalRead(6) == HIGH)
  {
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (iVal > 150 & iVal < 250)
&& would be more usual.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

switch/case depends on an exact match for the case values to work.  What values do you see for iVal ?
As it comes from an analogRead() I would be surprised if they were exactly 1, 2 ,3 etc
In fact, from your code I can see that you are not expecting them to be any such value.  Take out the switch/case and just use the ifs
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

switch/case depends on an exact match for the case values to work.

While true in the sense of a compiler locked to the C/C++ standard we are after all using GCC which allows us to do -

Code:
switch ( /* an integer value here */ )
{
    case  0 ...  9:
        // ... some perhaps useful code here ...
        break;

    case 10 ... 19:
        // ... some perhaps useful code here ...
        break;

    case 20 ... 29:
        // ... some perhaps useful code here ...
        break;

    default:
        // ... some perhaps useful code here ...
        break;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

>UKHeliBob
I took your advice and went with the "if" statements only. Works like a charm now. Thanks a lot. I have used switch/case many times in the past with various languages (but I'm just a newbie) so that was the first thing I tried. This is the first time I couldn't get it to work. The sketch works now but I'm still wanting to know why switch didn't work. "Inquiring minds want to know."

"If it ain't broke, fix it 'til it is."
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 627
Posts: 34234
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The sketch works now but I'm still wanting to know why switch didn't work.

UKHeliBob told you:-
Quote
switch/case depends on an exact match for the case values to work.  What values do you see for iVal ?
As it comes from an analogRead() I would be surprised if they were exactly 1, 2 ,3 etc
You only get those case to work for iVal values of 1 to 10. Then the first thing you do when the are called is check incorrectly if they are in another range altogether. Therefore nothing can ever be executed.
Logged

London
Offline Offline
Edison Member
*
Karma: 47
Posts: 1442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

>UKHeliBob
I took your advice and went with the "if" statements only. Works like a charm now. Thanks a lot. I have used switch/case many times in the past with various languages (but I'm just a newbie) so that was the first thing I tried. This is the first time I couldn't get it to work. The sketch works now but I'm still wanting to know why switch didn't work. "Inquiring minds want to know."

"If it ain't broke, fix it 'til it is."

Check your if statements. What happens if iVal ==150?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You only get those case to work for iVal values of 1 to 10. Then the first thing you do when the are called is check incorrectly if they are in another range altogether. Therefore nothing can ever be executed.

AHA! Grumpy_Mike turned on the light for me and it's my bad. I failed to explain that I will be using the pot as a multi-position "switch". The pot will be set thusly... case 1: pot will be set to analog 100... case 2: pot will be set to analog 200... etc. etc. (And therein lies my answer) The pot will never be set to 150 or any other value other than the midpoint of each range. (e.g. 100, 200, 300, 400, 500 etc.) I apologize for not explaining this earlier. The function won't even be called until a pushbutton is pressed. That button is scanned by the other switch/case I mentioned earlier, and is working flawlessly.
Looking back at the code, I see my problem. I would have to first declare another var, (e.g. range) and set that to an integer between 1 and 10 using analogRead(). Then the switch/case var would become range, not iVal. To determine the value of "range", I would have to use the "if" statements first. Therefore switch/case becomes redundant. DUH! Oh well, like I said, I'm really a newbie at software. I'm a hardware tech.
Thanks, everyone, for the input. It did make me see what I was doing wrong. Hope this may help someone in the future. I learned something new from all of this.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you considered using map() to convert the 0 - 1023 input into 0 - 9 output ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Have you considered using map() to convert the 0 - 1023 input into 0 - 9 output ?
No. I have never used "map()". I'll have to learn that one. I'll give it a try. I will finally use the function that gives me the smallest code.  smiley-mr-green
Thanks for the tip.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Have you considered using map() to convert the 0 - 1023 input into 0 - 9 output ?
*That's it!* map() took a 41 line function down to 21 lines. 50% tightening. I *like* it. Thanks for the lead. This project is my first time using analog inputs. So the learning curve was kinda double steep.  smiley-grin
Logged

Pages: [1]   Go Up
Jump to: