Pages: [1]   Go Down
Author Topic: Variables in switch statements  (Read 376 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey. Is it possible to use a variable in a switch statement? I'm doing a project based on TV remote IR codes and using variables to concisely store the long codes. Also, using the codes themselves in switch statements would be impractical, since if I started using a different remote, I would only have to change the codes in one place at the top. So, is it possible?

Code:
//import IRremote Library
#include <IRremote.h>

//define remote button data codes
int power = 31377463;
int play = 31352983;
int stp = 31385623;
int rewind = 31365223;
int forward = 31332583;
int menu = 31329013;
int enter = 31367773;
int no1 = 31359103;
int no2 = 31342783;
int no3 = 31375423;
int no4 = 31334623;
int no5 = 31367263;

//LED status variables (0 = off; 1 = on)
int a,b,c,d,e,x = 0;

//pin definitions
int ledPin1 = 12;
int ledPin2 = 11;
int ledPin3 = 10;
int ledPin4 = 9;
int ledPin5 = 8;
int RECV_PIN = 2;

//variable for storing recieved data code
int CODE;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {

  Serial.begin(9600);       //enable serial console for debugging
  irrecv.enableIRIn();      //enable IR recieving
  pinMode(ledPin1, OUTPUT); //LED pin output enabling
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);

}

void loop() {

  if (irrecv.decode(&results)) {

    Serial.println(results.value);
    CODE = (results.value);

    switch (CODE) {

    case no1:
      if (a == 0) {
        digitalWrite(ledPin1, HIGH);
        a = 1;
      }

      else {
        digitalWrite(ledPin1, LOW);
        a = 0;
      }

      break;

    case no2:
      if (b == 0) {
        digitalWrite(ledPin2, HIGH);
        b = 1;
      }

      else {
        digitalWrite(ledPin2, LOW);
        b = 0;
      }

      break;

    case no3:
      if (c == 0) {
        digitalWrite(ledPin3, HIGH);
        c = 1;
      }

      else {
        digitalWrite(ledPin3, LOW);
        c = 0;
      }

      break;

    case no4:
      if (d == 0){
        digitalWrite(ledPin4, HIGH);
        d = 1;
      }

      else {
        digitalWrite(ledPin4, LOW);
        d = 0;
      }

      break;

    case no5:
      if (e == 0){
        digitalWrite(ledPin5, HIGH);
        e = 1;
      }

      else {
        digitalWrite(ledPin5, LOW);
        e = 0;
      }

      break;

    case power:
      if (x == 0) {
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, HIGH);
        digitalWrite(ledPin4, HIGH);
        digitalWrite(ledPin5, HIGH);
        x = 1;
        a = 1;
        b = 1;
        c = 1;
        d = 1;
        e = 1;
      }

      else {
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        digitalWrite(ledPin4, LOW);
        digitalWrite(ledPin5, LOW);
        x = 0;
        a = 0;
        b = 0;
        c = 0;
        d = 0;
        e = 0;
      }

      if (a == 1 && b == 1 && c == 1 && d == 1 && e == 1) {
       
        x = 1;
       
      }
     
      if (a,b,c,d,e == 0) {
       
        x = 1;
       
      }
     
      Serial.println(x);
      delay(500);
      irrecv.resume();
     
    }

  }

}

^^^That gives me "'no1' cannot appear in a constant expression."
Logged

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

You should only use a variable in a switch.
What would be the point of using a constant?
You'd always know which case it would select, and so would be pretty pointless.
A case must be a compile time constant.

Your case could use a lookup to change which code it selected.
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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13855
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


int power = 31377463;

The Arduino compiler cannot put a var this size in an int. you need to use long for that and append an L to the numbers

long power = 31377463L;
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick replies guys.

Hmm, I've never had a problem with not being able to store the codes in an int, unless of course the wraparound would be the same.
Anyway, I've solved the problem by making the variables that store the codes for each button constants.

Code:
const int power = XXXXXXXXXXX;

AWOL, what's a lookup and how do I use it?
Logged

Pages: [1]   Go Up
Jump to: