Go Down

Topic: switch case newb (Read 260 times) previous topic - next topic

LandonW

never used a switch case statement before   I basically want to increase a PWM value with a button press. however im getting a error.  the example code in the IDE wasn't helpful to me.

Code: [Select]

 if (dimmerPin==LOW);{
    delay(10);
    count++;
      if (count = 4);{
        count = 0;
      }
  }
   switch
    case count = 0
      dimmerVal = 64;
      break;
    case count = 1:   
      dimmerVal = 128;
      break;
    case count = 2:   
      dimmerVal = 192;
      break;
    case count = 3:
      dimmerVal = 255;

pert

When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

DKWatson

Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

cattledog

Once you get the switch case syntax correct you won't be doing much switching until you fix these two errors.

Code: [Select]
[code]if (count = 4);{

evanmars

Once you get the switch case syntax correct you won't be doing much switching until you fix these two errors.

Code: [Select]
[code]if (count = 4);{

And this one
Code: [Select]
if (dimmerPin==LOW);{

LandonW

The correct syntax for switch can be found here.


Thank you,  new code compiles with no errors


LandonW

Code: [Select]

int count;
int dimmerVal;
int ledPin = 0;    //analogWrite
int dimmerPin =3;  //digitalRead
int lightPin = 2;  //analogRead
int auxPin = 4;    // digitalWrite


void setup() {
  // put your setup code here, to run once:
  pinMode(dimmerPin,INPUT);
  pinMode(lightPin,INPUT);
  pinMode(ledPin,OUTPUT);
  pinMode(auxPin,OUTPUT);

  count = 3;
 
}

void loop() {
  // put your main code here, to run repeatedly:

  if (dimmerPin,LOW);{
    delay(25);
    count++;
      if (count == 4);{
        count = 0;
      }
  }
   switch (count){
    case 0:
      dimmerVal = 0;
      break;
    case 1:   
      dimmerVal = 85;
      break;
    case 2:   
      dimmerVal = 170;
      break;
    case 3:
      dimmerVal = 255;
      break; 
}
}

DrAzzy

if (dimmerPin,LOW)

No, the comma operator is not what you want there.

I think you meant

if (digitalRead(dimmerPin)==LOW)

You must read the pin, and then check the value you get.

Since LOW is just #defined to 0, you can also do

if(!digitalRead(dimmerPin))

But that's not best practice, as it makes the code a bit less readable for no particular benefit.
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

LandonW

if (dimmerPin,LOW)

No, the comma operator is not what you want there.

I think you meant

if (digitalRead(dimmerPin)==LOW)

You must read the pin, and then check the value you get.

Since LOW is just #defined to 0, you can also do

if(!digitalRead(dimmerPin))

But that's not best practice, as it makes the code a bit less readable for no particular benefit.
good call.

RayLivingston

#9
Sep 26, 2018, 01:06 am Last Edit: Sep 26, 2018, 01:07 am by RayLivingston
It may compile without errors, but this is still not going to do what you want it to...

Code: [Select]
if (dimmerPin,LOW);{

Regards,
Ray L.

LandonW

It may compile without errors, but this is still not going to do what you want it to...

Code: [Select]
if (dimmerPin,LOW);{

Regards,
Ray L.
yes thank you, it has been corrected. sometimes I get in a hurry and forget the little things.
I did grasp the idea of how to set up a switch case, which was my original predicament

PaulS

yes thank you, it has been corrected. sometimes I get in a hurry and forget the little things.
I did grasp the idea of how to set up a switch case, which was my original predicament
Did you also note that the ; does NOT belong in that statement?
The art of getting good answers lies in asking good questions.

Go Up