Go Down

Topic: Variables in switch statements (Read 400 times) previous topic - next topic

cubemike99

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: [Select]
//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."

AWOL

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

robtillaart


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;
Rob Tillaart

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

cubemike99

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: [Select]
const int power = XXXXXXXXXXX;

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

Go Up