Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Arduino "CASE " Pro...
 
Share:
Notifications
Clear all

[Solved] Arduino "CASE " Problems

3 Posts
2 Users
1 Reactions
996 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

Hi there from Arduino nerd needing help. I have posted a similar problem on Arduino forum but cannot get any help ( Probably my fault and not their's), with what seems like a very simple "Case statement".

int aaa = 0;

int z1 = 0;

int z2 = 0;

void setup() {

  Serial.begin(9600);

  Serial.println(" Begin");

}

void CasE() {

  switch (aaa) {

  case 1:

    //   /*

    Serial.print(" START CASE 1");

    z1 = 12 * 12 / 2 + 45 - 31;

    Serial.print(" Z1 =");
    Serial.println(z1);

    //    */

    break;

  case 2:

    Serial.print(" START CASE 2");

    int z2 = 12 * 12 / 2 + 45 - 31;

    Serial.print(" Z2 =");

    Serial.println(z2);
    break;

  }

}

void loop() {

  aaa = 2;

  CasE();

}

With this code I cannot get to "case2" unless I comment all the code in "case1"'

This is my first attempt at using "switch case" and I am obviously missing something.

Can anyone please assist me.

Thanks

Edit: Code clean up, keywords added.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
 

Hi Farticus,

I did run this code and it works as expected for aaa=1 and aaa=2 on WokWiki (did not have an Arduino handy).
(references: Arduino Documentation, chapter of my mini-course)

So I'm not sure what is happening on your setup 😞 

 

Code that I tested (cleaned it up a little):

int aaa = 0;
int z1 = 0;
int z2 = 0;

void setup() {
  Serial.begin(9600);
  Serial.println(" Begin");
}

void CasE() {

  switch (aaa) {

  case 1:

    Serial.print(" START CASE 1");
    z1 = 12 * 12 / 2 + 45 - 31;
    Serial.print(" Z1 =");
    Serial.println(z1);
    break;

  case 2:

    Serial.print(" START CASE 2");
    z2 = 12 * 12 / 2 + 45 - 31;
    Serial.print(" Z2 =");
    Serial.println(z2);
    break;

  }

}

void loop() {

  aaa = 1;
  CasE();

}

 

A tip, of no critical importance for you code: Use code "blocks" when in doubt.

In C (and most other languages) you can create code blocks, where a group of code could be seen as one. In C like languages (C, C++, Obj. C, Java, javaScript, etc) this is typically done with enclosing accolades. The advantage can be that potentially breaking characters or code is now enclosed as a "block". This also improves code readability - but that is just my personal opinion 😉 

For example:

void CasE() {

  switch (aaa) {

    case 1:
      {
        Serial.print(" START CASE 1");
        z1 = 12 * 12 / 2 + 45 - 31;
        Serial.print(" Z1 =");
        Serial.println(z1);
        break;
      }
      
    case 2:
      {
        Serial.print(" START CASE 2");
        int z2 = 12 * 12 / 2 + 45 - 31;
        Serial.print(" Z2 =");
        Serial.println(z2);
        break;
      }
      
  }

}

 

 

Another tip: avoid the use of global variables.

Global variables make your code inflexible, not reusable in other projects, and ... can cause unexpected behavior (bugs).

For example:
In your example code the globally defined "z2" is NOT the same variable as the "z2" used in the "Case()" function. 

You've declared z1 and z2 as two global integers at the beginning of your code.
However ... in de CasE function you work "z1" without declaring it (so it uses the global variable).
"z2" on the other hand, you redefine in the "CasE()" function by saying "int z2 = ..." in the "case 2:" section.
So you declare "z2" now as a new and local variable and the global defined "z2" can no longer be accessed in the "CasE()" function.


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

Thank you for your prompt and very helpful response. I am new to coding and as yet do not fully understand the concept of Global and Local variable but your explanation certainly helped. 

Thank you 😀 


   
Hans reacted
ReplyQuote
Share: