Switch Case practice for class

I am trying to work on a switch case using a potentiometer and 4 LED lights. I have been struggling with this project for weeks and I am at my wits end. If anyone has any idea on what I am doing incorrectly please say so. I am attempting to have the potentiometer go through five different cases where different combinations of the lights are active and at certain points; beginning and end they are both off.



const int Red = 11;
const int Blue = 10;
const int Green = 9;
const int Yellow = 5;

const int potpin = A1; 
int val; // variable needed to read from Analog Pin 1.
int newvariable = A1;

void setup() {

  // put your setup code here, to run once:
Serial.begin(9600);
  
pinMode(Red, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
  
  
}

void loop() {

val = analogRead(potpin);
val = map(val, 0, 1023, 1, 4);
  
Serial.println(val);
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180); 
 
if(analogRead(potpin); newvariable!=0)
   {
  newvariable++;}

  else if(newvariable>=200 && analogRead(potpin)==HIGH)
 {
  newvariable == 1;}
  
  else if(newvariable<=275 && analogRead(potpin)==HIGH)
  {
    newvariable == 2;}
 
  
switch (val) {
  
  case 1:   
    // LED Bulbs light up in order according to value from the potentiometer 
  // Bulbs light up as listed when variables equal case 1.
    val>=5 && val<=34;  
  
    digitalWrite(Red, HIGH);
    digitalWrite(Blue, LOW);
    digitalWrite(Green, HIGH);
    digitalWrite(Yellow, LOW);
break;
    case 2:     
   // LED Bulbs light up in order according to value from the potentiometer 
  // Bulbs light up as listed when variables equal case 2. 
  val>=35 && val<=68;
  
  	digitalWrite(Red, LOW);
    digitalWrite(Blue, HIGH);
    digitalWrite(Green, LOW);
    digitalWrite(Yellow, HIGH);
break; 
    case 3:  
     // LED Bulbs light up in order according to value from the potentiometer 
  // Bulbs light up as listed when variables equal case 3. 
    val>=69 && val<=102;
  
    digitalWrite(Red, LOW);
    digitalWrite(Blue, HIGH);
    digitalWrite(Green, HIGH);
    digitalWrite(Yellow, LOW);
break;
    case 4: 
    // LED Bulbs light up in order according to value from the potentiometer 
  // Bulbs light up as listed when variables equal case 4. 
    val>=103 && val<=136;
  
  	digitalWrite(Red, HIGH);
    digitalWrite(Blue, LOW);
    digitalWrite(Green, LOW);
    digitalWrite(Yellow, HIGH);
    
break;
     case 5:      
 // LED Bulbs light up in order according to value from the potentiometer 
  // Bulbs light up as listed when variables equal case 5.
    
  val>=137 && val<=170;
  
  	digitalWrite(Red, HIGH);
    digitalWrite(Blue, HIGH);
    digitalWrite(Green, HIGH);
    digitalWrite(Yellow, HIGH);
  
default:   // if no condition is met, then LED Lights will revert to being off.
    digitalWrite(Red, LOW);
    digitalWrite(Blue, LOW);
    digitalWrite(Green, LOW);
    digitalWrite(Yellow, LOW); 

}
}

What do you think this does?
if(analogRead(potpin)..... ; ..... newvariable!=0)

1 Like

What is this for?

What value " val " should be ?

Your code has a number of errors that are not strictly errors.

All the statements like this are equal to doing exactly nothing. C/C++ lets you do things like that:

  val>=35 && val<=68;

This

val = analogRead(potpin);
val = map(val, 0, 1023, 1, 4);
  
Serial.println(val);
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180); 

ends you up with val being between 0 and 180.

Review the syntax of the switch/case statement. Typically cases are controlled with small integers, or you could use a case range which may be what you were going for with those do nothing statements

   case 35 ... 68 :

// switch value is between including 35 and 68 here

   break;

You use analogRead() and compare it to HIGH, which it will be the moment you crack open the potentiometer all the way to the full on twist.

I don't know what you might have wanted this to do

if(analogRead(potpin); newvariable!=0)

I don't even know if or why that compiles, and if it does I wouldn't dare predict what it means.

In short you are way over your skis. I suggest you work through some examples from the IDE and start read read reading as much code as you can, where you won't see things you've written. At this point there is very little room for creativity, and rather than pound your head against the wall trying to get an actual sketch to function to specification, your time would be better spent studying and learning.

Less fun? Hard to say, but def more efficient. Every minute learning will pay off for the rest of your life.

a7

in the first bit of potentiometer range i am aiming to just leave the 4 LED lights off

And.... What s... is that " val>=5 && val<=34; `

got any suggestions on where to look for the IDE examples you talk about?

@jmm2022
Try this simple code.

const int Red = 11;
const int Blue = 10;
const int Green = 9;
const int Yellow = 5;
const int potpin = A1;
int val; // variable needed to read from Analog Pin 1.
//----------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(Red, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
}
//----------------------------------------------------------------------
void loop() {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180);
  Serial.println(val);

  // LED Bulbs light up in order according to value from the potentiometer
  // Bulbs light up as listed when variables equal case 1.
  if (val >= 5 && val <= 34) {
    digitalWrite(Red, HIGH);
    digitalWrite(Blue, LOW);
    digitalWrite(Green, HIGH);
    digitalWrite(Yellow, LOW);
  }
  // LED Bulbs light up in order according to value from the potentiometer
  // Bulbs light up as listed when variables equal case 2.
  if (val >= 35 && val <= 68) {
    digitalWrite(Red, LOW);
    digitalWrite(Blue, HIGH);
    digitalWrite(Green, LOW);
    digitalWrite(Yellow, HIGH);
  }
  // LED Bulbs light up in order according to value from the potentiometer
  // Bulbs light up as listed when variables equal case 3.
  if (val >= 69 && val <= 102) {
    digitalWrite(Red, LOW);
    digitalWrite(Blue, HIGH);
    digitalWrite(Green, HIGH);
    digitalWrite(Yellow, LOW);
  }
  // LED Bulbs light up in order according to value from the potentiometer
  // Bulbs light up as listed when variables equal case 4.
  if (val >= 103 && val <= 136) {
    digitalWrite(Red, HIGH);
    digitalWrite(Blue, LOW);
    digitalWrite(Green, LOW);
    digitalWrite(Yellow, HIGH);
  }
  // LED Bulbs light up in order according to value from the potentiometer
  // Bulbs light up as listed when variables equal case 5.
  if (val >= 137 && val <= 170) {
    digitalWrite(Red, HIGH);
    digitalWrite(Blue, HIGH);
    digitalWrite(Green, HIGH);
    digitalWrite(Yellow, HIGH);
  }
  // if no condition is met, then LED Lights will revert to being off.
  if (val < 5 || val > 170) {
    digitalWrite(Red, LOW);
    digitalWrite(Blue, LOW);
    digitalWrite(Green, LOW);
    digitalWrite(Yellow, LOW);
  }
}

It works, but you don't think it will work as a switch case huh?

And don't forget to ask your teacher to send the best grade to me. :flushed: :flushed: :flushed: :flushed:

const int Red = 11;
const int Blue = 10;
const int Green = 9;
const int Yellow = 5;
const int potpin = A1;
int val; // variable needed to read from Analog Pin 1.
byte myCase = 6;
//----------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(Red, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
}
//----------------------------------------------------------------------
void loop() {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180);

  if (val >= 5 && val <= 34) myCase = 1;
  if (val >= 35 && val <= 68)  myCase = 2;
  if (val >= 69 && val <= 102)  myCase = 3;
  if (val >= 103 && val <= 136)  myCase = 4;
  if (val >= 137 && val <= 170)  myCase = 5;
  if (val < 5 || val > 170)  myCase = 6;
  //Serial.println(myCase);

  // LED Bulbs light up in order according to value from the potentiometer
  // Bulbs light up as listed when variables equal case 1.
  switch (myCase) {
    case 1:
      digitalWrite(Red, HIGH);
      digitalWrite(Blue, LOW);
      digitalWrite(Green, HIGH);
      digitalWrite(Yellow, LOW);
      break;
    // LED Bulbs light up in order according to value from the potentiometer
    // Bulbs light up as listed when variables equal case 2.
    case 2:
      digitalWrite(Red, LOW);
      digitalWrite(Blue, HIGH);
      digitalWrite(Green, LOW);
      digitalWrite(Yellow, HIGH);
      break;
    // LED Bulbs light up in order according to value from the potentiometer
    // Bulbs light up as listed when variables equal case 3.
    case 3:
      digitalWrite(Red, LOW);
      digitalWrite(Blue, HIGH);
      digitalWrite(Green, HIGH);
      digitalWrite(Yellow, LOW);
      break;
    // LED Bulbs light up in order according to value from the potentiometer
    // Bulbs light up as listed when variables equal case 4.
    case 4:
      digitalWrite(Red, HIGH);
      digitalWrite(Blue, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Yellow, HIGH);
      break;
    // LED Bulbs light up in order according to value from the potentiometer
    // Bulbs light up as listed when variables equal case 5.
    case 5:
      digitalWrite(Red, HIGH);
      digitalWrite(Blue, HIGH);
      digitalWrite(Green, HIGH);
      digitalWrite(Yellow, HIGH);
      break;
    // if no condition is met, then LED Lights will revert to being off.
    default:
      digitalWrite(Red, LOW);
      digitalWrite(Blue, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Yellow, LOW);
      break;
  }
}
1 Like

You could use case ranges.

a7

Open the Arduino IDE. File menu ->examples

Google "Arduino examples"

Two questions: what was the point of byte? why all the ifs and not if/ else if?

I am using C++ Arduino uno will that be very different?

Different from what? Thatsthe most common setup.

Why don't you just try looking and explore a little. You might have to wade through a little reading to find the nugget you want. You might learn something.

Google what I said Google. Take some time with some basic tutorials. There's no reason to make these people retype all that out for you here.

I understand what you mean on the last bit. In a way I am just frustrated about having to go in a certain manner without knowing how to navigate for proofreading material on projects. To me, this kind of field, its like trying to learn a different language because you have all the english symbols, letters and numbers, but they are all arranged differently and without any way to proofread it for what is required, one would never know how far off their own attempts are.

"byte" is a smaller data type compared to "int" so memory is not wasted.

  • one byte = 8 bits.
  • one int = 16 bits.

"else" expects "if" to be false, but in this list of conditions you want only the true condition.

This link is a small subset of "Arduino C++" but probably everything you will use... have a read.

That's why people have put together tutorials and examples and lessons. Avail yourself of some and then you'll know this stuff. Nobody starts out already understanding it. We all started at the basics.

how long did it take you to get to this point? Like how long have you been working on arduino?