Code not functioning as expected

I might have misunderstood the functionality of the switch/Case, but as far as I can read from my code below, this should work as expected, but all it does is light up all the LED's, and nothing happens when I press the buttons, either of them really.

Hardware is tested and working properly.

At the moment of posting I've pulled out the "Turn leds off at boot" function, but nothing has changed.

//Variables for LED selection
int led = 1;            //Set variable led to 1
int prevUp = HIGH;      //Set variable prevUp to High

//Variables for brightness setting
int prevUp2 = HIGH;  //set variable prevUp2 to High
int level = 1; //set variable level to 1
int brightness = 64; //set variable brightness to 64


const int buttonPin = 2;
const int modePin = 8;

const int led1pin = 3;
const int led2pin = 5;
const int led3pin = 6;
const int led4pin = 9;
const int led5pin = 10;
const int led6pin = 11;

void setup() {
  //set buttons to input
  pinMode (buttonPin, INPUT);
  pinMode (modePin, INPUT);
  /*turn leds off at boot
  analogWrite(led1pin, 0);
  analogWrite(led2pin, 0);
  analogWrite(led3pin, 0);
  analogWrite(led4pin, 0);
  analogWrite(led5pin, 0);
  analogWrite(led6pin, 0);
  */
}

void loop() {

  int currUp = digitalRead(buttonPin);
  if (currUp != prevUp)
  {
    if (currUp == LOW)
      led++;
    if (led > 7) led = 1;
  }
  prevUp = currUp;

  int currUp2 = digitalRead(modePin);
  if (currUp2 != prevUp2)
  {
    if (currUp2 == LOW)
      level++;
    if (level > 7) level = 1;
  }
  prevUp2 = currUp2;

  switch (led) {
    case 1:
      {
        analogWrite(led1pin, brightness);

      }
    case 2:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
      }
    case 3:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led2pin, brightness);
      }
    case 4:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
        analogWrite(led4pin, brightness);
      }
    case 5:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
        analogWrite(led4pin, brightness);
        analogWrite(led5pin, brightness);
      }
    case 6:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
        analogWrite(led4pin, brightness);
        analogWrite(led5pin, brightness);
        analogWrite(led6pin, brightness);
      }
      break;

  }

  switch (level) {
    case 1:
      {
        brightness = 0;
      }
    case 2:
      {
        brightness = 16;
      }
    case 3:
      {
        brightness = 32;
      }
    case 4:
      {
        brightness = 64;
      }
    case 5:
      {
        brightness = 128;
      }
    case 6:
      {
        brightness = 255;
      }
      break;

  }
}

Did you miss out a bunch of "break"s?

I think so

AWOL:
Did you miss out a bunch of "break"s?

I think so

And that's why I post on this forum. Behaviour is still not as wanted, but more close to it.

Thank you so much.

Hi,
Have you got pull_up or pull_down 10K resistors on the input pins that your buttons are connected to?

Tom.... :slight_smile:

Behaviour is still not as wanted, but more close to it.

Please post your revised code.

UKHeliBob:
Please post your revised code.

Tom: 10k pull up resistors

I was away for a bit here, but I got it working just before I shut down that day.

Here's the revised code, with a new question attached.

It appears the only reason it is now working, is because of the serial function I used to test the led/level counter slows things down just enough. Any suggestions how I can remove the serial bit and still maintain functionality?

//Variables for LED selection
int led = 1;            //Set variable led to 1
int prevUp = HIGH;      //Set variable prevUp to High

//Variables for brightness setting
int prevUp2 = HIGH;  //set variable prevUp2 to High
int level = 1; //set variable level to 1
int brightness = 64; //set variable brightness to 64


const int buttonPin = 8;
const int modePin = 2;

const int led1pin = 3;
const int led2pin = 5;
const int led3pin = 6;
const int led4pin = 9;
const int led5pin = 10;
const int led6pin = 11;

void setup() {
  
  //set buttons to input
  pinMode (buttonPin, INPUT);
  pinMode (modePin, INPUT);
  
  //set LED's to output
  pinMode (led1pin, OUTPUT);
  pinMode (led2pin, OUTPUT);
  pinMode (led3pin, OUTPUT);
  pinMode (led4pin, OUTPUT);
  pinMode (led5pin, OUTPUT);
  pinMode (led6pin, OUTPUT);

  //turn leds off at boot
  analogWrite(led1pin, 0);
  analogWrite(led2pin, 0);
  analogWrite(led3pin, 0);
  analogWrite(led4pin, 0);
  analogWrite(led5pin, 0);
  analogWrite(led6pin, 0);

  // start serial port at 9600 bps:
  Serial.begin(9600);

}

void loop() {

  int currUp = digitalRead(buttonPin);
  if (currUp != prevUp)
  {
    if (currUp == LOW)
      led++;
    if (led > 7) led = 1;
  }
  prevUp = currUp;

  int currUp2 = digitalRead(modePin);
  if (currUp2 != prevUp2)
  {
    if (currUp2 == LOW)
      level++;
    if (level > 6) level = 1;
  }
  prevUp2 = currUp2;

  switch (led) {
    case 1:
      {
        analogWrite(led1pin, brightness);

      }
      break;
    case 2:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
      }
      break;
    case 3:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
      }
      break;
    case 4:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
        analogWrite(led4pin, brightness);
      }
      break;
    case 5:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
        analogWrite(led4pin, brightness);
        analogWrite(led5pin, brightness);
      }
      break;
    case 6:
      {
        analogWrite(led1pin, brightness);
        analogWrite(led2pin, brightness);
        analogWrite(led3pin, brightness);
        analogWrite(led4pin, brightness);
        analogWrite(led5pin, brightness);
        analogWrite(led6pin, brightness);
      }
      break;
    case 7:
      {
        analogWrite(led1pin, 0);
        analogWrite(led2pin, 0);
        analogWrite(led3pin, 0);
        analogWrite(led4pin, 0);
        analogWrite(led5pin, 0);
        analogWrite(led6pin, 0);
      }
      break;
  }

  switch (level) {
    case 1:
      {
        brightness = 16;
      }
      break;
    case 2:
      {
        brightness = 32;
      }
      break;
    case 3:
      {
        brightness = 64;
      }
      break;
    case 4:
      {
        brightness = 128;
      }
      break;
    case 5:
      {
        brightness = 255;
      }
      break;
    case 6:
      {
        brightness = 0;
      }
      break;
    
  }
  Serial.println(led);
  Serial.println(level);
}
  switch (level) {
    case 1:
      {
        brightness = 16;
      }
      break;
    case 2:
      {
        brightness = 32;
      }
      break;
    case 3:
      {
        brightness = 64;
      }
      break;
    case 4:
      {
        brightness = 128;
      }
      break;
    case 5:
      {
        brightness = 255;
      }
      break;
    case 6:
      {
        brightness = 0;
      }
      break;
    
  }

Simplify this by putting the brightness values in an array and using the value of level as an index to the array.

It appears the only reason it is now working, is because of the serial function I used to test the led/level counter slows things down just enough. Any suggestions how I can remove the serial bit and still maintain functionality?

I'm not sure what the program does when it is "too fast" but I believe that you could have a problem with switch bounce, and I suggest that you debounce your buttons with either software or hardware.

There is a debounce example in the ide 02digital examples and Robin2 presents a different version in his sticky post at the top of the project guidance section.

Hi,
try 115200 as your baud rate.

Do you need the serial function?

Or fit a switch input that causes the code to jump around it.

if (switch)
{
  Serial.println(led);
  Serial.println(level);
}

Tom.. :slight_smile:

Problem is button bounce that will appear if I remove the delay caused by the serial function.

I got some tips on my post in the project guidance forums, so I'll try it out later on. For now I'll leave the serial function there, just to keep it working while I build up the hardware.

Thanks for help.

TomGeorge:
Hi,
try 115200 as your baud rate.

Do you need the serial function?

Or fit a switch input that causes the code to jump around it.

if (switch)

{
  Serial.println(led);
  Serial.println(level);
}




Tom.. :)

Hi,

Problem is button bounce that will appear if I remove the delay caused by the serial function.

Sorry but how will it cause button bounce?? ?? ?? ?? ??
Did you try increasing the baud rate?

Tom.... :slight_smile: