Help with Switch Case method and Array, getting error on Switch

Hi! I am new to arduino and have a small sketch to help me learn. My issue is my Switch Statement gives me an error. What could I be missing? Thanks!

I am using the switch case method, because I need to learn this for another program I am working on. IF / ELSE won't work for that. And the button array needs to be a byte array as well, I can't change that.

The EEPROM portion of my code is NOT in here. I don't want to go any further until I get switch working.

#include <LiquidCrystal.h>
#include <EEPROM.h>

/// VERY SIMPLE PROGRAM 
/// 
/// PRESS A BUTTON IN THE BUTTON ARRAY
///
/// LCD PRINTS OUT WHAT BUTTON YOU PRESSED   HOPEFULLY! =) 
///
/// LCD PRINTS OUT THE ADDRESS OF THE BUTTON 0 - 7    HOPEFULLY =)
///
/// 

byte buttons[] = {22, 26, 30, 34, 38, 42, 46, 50};  /// ARRAY OF BUTTONS  , PINS OF ARDUINO 2560

byte leds[] = {20, 19, 18, 17, 16, 15, 14, 2};      /// ARRAY OF LEDS , PINS OF ARDUINO 2560

int buttonCount = 8;     /// AMOUNT OF ITEMS IN ARRAY << --- MIGHT NOT BE NEEDED CODE ????
int ledsCount = 8;       /// AMOUNT OF LEDS IN ARRAY << --- MIGHT NOT BE NEEDED CODE ???

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);  /// LCD PINS 
void setup()
{

 	lcd.begin(16,2);
 	lcd.print("OOMPA LOOMPA");
 	
 	delay(2000);
 	lcd.clear();

	for ( int b=0; b<buttonCount; b++)
	{
		pinMode(buttons[b], INPUT);        /// SETUP ARRAY OF BUTTONS AS INPUTS
		digitalWrite(buttons[b], LOW);   ///  SETUP ARRAY OF BUTTONS AS LOW ---- NO LEDS ON AT START
	}

	for (int l=0; l<ledsCount; l++)
	{
		pinMode(leds[l], OUTPUT);  /// SETUP ARRAY OF LED PINS, OUTPUT
		digitalWrite(leds[l], LOW);   /// PINS LOW, OFF UNTIL BUTTONS PRESSED
	} 
}

void loop()
{

	///  SELECTING EVENTS WITH BUTTON PRESSES
	switch byte(buttons[])
	{
		case 0:
		if (buttons[0] == HIGH) 
		{
			digitalWrite(leds[0], HIGH);
			lcd.print("Button: ", buttons[0]); /// PRINT THE BUTTON NUMBER THAT IS PRESSED
			lcd.print("EEPROM: ");             /// BIT TO BE WRITTEN TO ADDRESS
			break;
		}

 		case 1:
 		if (buttons[1] == HIGH)
 		{
 			digitalWrite(leds[1], HIGH)
 			lcd.print("Button: ", buttons[1])  /// PRINT THE BUTTON NUMBER THAT WAS PRESSED 
 			lcd.print("EEPROM: ");             /// BIT TO BE WRITTEN TO ADDRESS
 			break;
 		}
 		
 		case 2:
 		if (buttons[2] == HIGH)
 		{
            digitalWrite(leds[2], HIGH);
            lcd.print("Button: ", buttons[2]);
            lcd.print("EEPROM: ");
            break;
 		}

 		case 3;
 		if (buttons[3] == HIGH)
 		{
            digitalWrite(leds[3], HIGH);
            lcd.print("Button: ", buttons[3]);
 			lcd.print("EEPROM: ");
 			break;
 		}

 		case 4:
 		if (buttons[4] == HIGH)
 		{
 			digitalWrite(leds[4], HIGH);
 			lcd.print("Button: ", buttons[4]);
 			lcd.print("EEPROM: ");
 			break;
 		}

 		case 5:
 		if (buttons[5] == HIGH)
 		{
 			digitalWrite(leds[5], HIGH)
 			lcd.print("Button: ", buttons[5]);
 			lcd.print("EEPROM: ");
 			break;
 		}

 		case 6:
 		if buttons[6] = HIGH
 		{
 			digitalWrite(leds[6], HIGH);
 			lcd.print("Butons: ", buttons[6]);
 			lcd.print("EEPROM: ");
 			break;
 		}

 		case 7:
 		if (buttons[7] == HIGH)
 		{
            digitalWrite(leds[7], HIGH);
 			lcd.print("Button: ", buttons[7]);
 			lcd.print("EEPROM: ");
 			break;
 		}
                return -1;  /// MULTIPLE BUTTON PRESSED RETURN ERROR. <<--- IS THIS WRONG ?
	}

}

You don't want a switch statement in this situation, replace the entire switch with this:

for( int i=0; i<sizeof(buttons); i++ )
{
	if (buttons[i] == HIGH) 
	{
		digitalWrite(leds[i], HIGH);
		lcd.print("Button: ", buttons[i]); /// PRINT THE BUTTON NUMBER THAT IS PRESSED
		lcd.print("EEPROM: ");             /// BIT TO BE WRITTEN TO ADDRESS		}
	}
}

But for future reference, the reason it didn't compile is this:

switch byte(buttons[])

taking out the byte and giving an index inbetween [] would probably of removed the error.

  switch byte(buttons[])

Can you explain what this is supposed to do ?
(a) you are declaring the buttons array as part of the switch, so it's value will be undefined
(b) you don't declare how many elements there are in the array
(c) you don't even specify which element of the array should be used as the switch variable even if it did work

switch byte(buttons[])

Doesn't work...
Even when removing the byte(), there is no primary expression in the brackets.
This is a case of using if/else functions, what you want can't be done with a switch case.

What I am trying to do isn't difficult, looks like I went the wrong way about it. :slight_smile:

Basically, you press a button in the array, and a corresponding LED lights up and the LCD prints out the button pressed.

I am also working on saving the button as a value in bit to eeprom. button 0 = bit 0 ( inside a selected byte address )

So, the IF statement will work okay for such a task? I also want only single button presses to work. If you press multiple buttons at the same time, I want to return an error....this is why I thought I would need a switch case.

Thanks!

The switch() case is great if ONE parameter has multiple possible values. If there are MORE parameters then you need to put it into one or use the if/else statements.

What you could try is:

byte Var1;
bitWrite(Var1, 0, digitalRead(22));
bitWrite(Var1, 1, digitalRead(26));

And so on. (Or write a nice for() loop) Now all buttons are in one variable and can be read by a switch() case. But if 2 or more buttons are pressed at the same time, it won't work because after the first correct case: statement, it'll exit the switch() loop.

You could also try:

for(int ii = 0; ii <= 7; ii++)
{
if(digitalRead(buttons[ii])==HIGH)
{
//do your stuff here
}
}

C-F-K, I think that's what I am looking for. I want to return an error if more than one button is pressed.

Lets say, print to LCD " Please wait"...or.."Please only press one button." -- something like that.

Almost forgot:
When writing to the EEPROM, take into account that it'll only survives 100.000 write times. So if you press the button and the program writes to it without a delay() or something like the BlinkWithoutDelay, your screwed...

Create your program that it only writes to EEPROM when the button is pressed/released the FIRST time, not every time!

I am going to work on the eeprom once I get my silly switch solved. The If statement looks the better choice with everybody's replies. :wink:

But can an if statement make sure only one button is pressed at any given time? I guess that was my main concern.

Not really, you need some other magic for that part...

So, am I back then, to a switch statement in that case? Thats my kicker, making sure, that only one button is read.

It's important later on in the program. Because I don't want to write multiple bits in the same byte..meaning I don't want to override something by accident.

int count = 0;
for(int ii = 0; ii <=7; ii++)
{
if(bitRead(Var1, ii) == 1)
  count++
}

if(count > 1)
   multipleButtonsPressed();

Thats what I can think of, in between debugging my own work right now :stuck_out_tongue:
Anybody a better idea how to detect that only 1 bit of a byte is set?

Thank you kindly! I will test these ideas out and see what I find. :slight_smile:

I was never this determined with Objective-C!!

Hi!

Can I use Switch Case to run a method depending on a selected value in an array?

No code yet. But If I have an array of 4 numbers, 1 - 4.
And say I have 4 buttons, so you press a button lets say #3 and it goes to case 3: /// do this method

Can I use switch case where each case is relative to a value in the array?

I know about If / Else, but I'd rather do a switch method. I've seen it done in code, but I forgot the exact grammar. I forget what the grammar for the switch statement should look like. Google is not helping.

Sure just like your index pointer is relative to the array you could use the same value in a switch. I don't see much reason to do that but you could.

You should clearly describe, in plain language, what you really want to do. Then you should make some code and post it here.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Sorry about that!

You need to look at each of the buttons, one at a time, to see if it is pressed. You cannot look at them all at the same time. But, the computer is very fast, it will seem like you have looked at them all at the same time.

You can also count up how many are pressed, as you are reading them. If you count more than one pressed, you can ignore that cycle and start again.

When you have read all the buttons, and found that one, and only one, has been pressed, then you can take some action based on that.

Ignore all the overcomplicated advice about trying to pack information into separate bits.

Hey michinyon, thats exactly what I want to do.

My main issue is I just haven't learned enough about such switching yet. I have seen code where people are using shift registers, but I do not want to do that, because then I'm just copy/pasting code, and I don't learn by doing that. And now I have to add more parts to the circuit board and cost.

So, Can I have an array for the buttons? Or do I need to declare each button ( pin ) individually?

Then, make maybe in IF statement to see what button is pressed and compare it to other buttons to see if they're pressed? This is where I maybe want an array to cut down on code....if I can.

And then, use a switch / case statement, to create the actions for the buttons? Case 1 would be button 1, so on and so on.

The reason for the switch case is the break; command.

I have written very basic code that I mostly got from here. And just set it up for my needs, at least for learning.

int relayLed[] = {2, 3, 4, 5, 6, 7, 8, 9};
int relayPin = 8;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int out = 0; out < relayPin; out++)
  {
    pinMode(relayLed[out], OUTPUT);
  }
  
  

}

void loop() {
  // put your main code here, to run repeatedly:
  
if (Serial.available() > 0)
{
int readInput = Serial.read();

switch(readInput)
  {
  case '0':
  digitalWrite(2, HIGH);
  break;
  
  case '1':
  digitalWrite(3, HIGH);
  break;
  
  return;
  }
}



}

PLEASE NOTE: This is all I have written so far. Now, this is the output code...The lighting of buttons and relays. I am using Serial.read as my buttons right now.

But I should be able to make an array of buttons, and then do maybe a for loop and an IF statement, and then have the switch case you see in the code to be the actions.

It just hit me: Do I make another switch case to turn the relays/leds off?