Go Down

Topic: Can't get this switch / case sketch to work. Any ideas? (Read 1 time) previous topic - next topic

PlasticOne

The program was suppose to run an RGB led with a lot of different functions off of one button but it was to many to cycle throw so i thought i could add a second button to brake down the functions making it easier to use so one button cycle the colours whilst the other dealt with the colours functions but iv made a but of a mess any help would be greatly appreciated. A small sample of the code
Code: [Select]
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
const int buttonPin2 = 4;
int RGB=1;
int B2=2;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  }

void loop ()
{
int currUp = digitalRead(buttonPin2);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin2);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            B2++;
      }
   }
 
   switch (B2)
     case 1:{
  {
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
 
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
   
   }
    prevUp = currUp;
}
break;

  case 2:{
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
 
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
   
   }
    prevUp = currUp;
  }break;
   }
}

Arrch

Start by putting any curly brace on its own line and using the auto format tool (In the Arduino IDE, under Tools) to make the code more readable. The seemingly random indentation is distracting.

MaJiG

You have a curly brace out of place, methinks

Code: [Select]
   
   switch (B2)
     case 1:{
  {
int currUp = digitalRead(buttonPin);


should be

Code: [Select]

   switch (B2)
   {
     case 1:
     {
         int currUp = digitalRead(buttonPin);


you may need to match closing brackets.

The curly brackets after the "case 1:" isn't necessary, btw.

UKHeliBob

Code: [Select]
  switch (B2)
  case 1:
  {
    {

should be
Code: [Select]
  switch (B2)
  {
  case 1:
    {
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Grumpy_Mike

You can't define a variable inside a switch statement. Define that int outside first and then assign it a value inside the switch.

Go Up