Adding switches

Hi,

I need help making program that will run specifed function when switch is on. So, when switch1 is on(button1 pressed) case 1 will run. Please look down at my program. I was using variable var to specify if switche on some port is low or high and that putted var into switch..case
Hardware setup is: all middle pins of switch are up to ground, and other switch pin is thought 10k resistor on Vin(power supply) and same pin is on digitalpins 0-4.

Problem is that program does not work every time. What am I doing wrong?
I am also interested what will happen if two switches at same time are on?

(ignore rest of program)

const int ledPin1 = 11;
const int ledPin2 = 10;
const int ledPin3 = 9;
const int ledPin4 = 6;
const int ledPin5 = 5;

const int button1 = 0;
const int button2 = 1;
const int button3 = 2;
const int button4 = 3;
const int button5 = 4;

int state1;
int state2;
int state3;
int state4;
int state5;

int brightness1 = 10;
int brightness2 = 10;
int brightness3 = 250;
int brightness4 = 10;
int brightness5 = 250;

int fadeAmount1 = 5;    
int fadeAmount2 = 5;  
int fadeAmount3 = 5;  
int fadeAmount4 = 5;  
int fadeAmount5 = 5;  

int brightness6 = 10;
int brightness7 = 10;
int brightness8 = 10;
int brightness9 = 10;
int brightness10 = 10;

int fadeAmount6 = 5;    
int fadeAmount7 = 5;  
int fadeAmount8 = 5;  
int fadeAmount9 = 5;  
int fadeAmount10 = 5;  

const int potPin1 = 1;
const int potPin2 = 2;

int var;

int pot1;
int pot2;

void setup(){
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
     
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
}

void loop(){

state1 = digitalRead(button1);
state2 = digitalRead(button2);
state3 = digitalRead(button3);
state4 = digitalRead(button4);
state5 = digitalRead(button5);

pot1 = analogRead(potPin1);
pot2 = analogRead(potPin2);
pot2 = map(pot2, 0, 1023, 1, 255);

if (state1 == LOW){
  var = 1;
}
else{
  var = 0;
}

if (state2 == LOW){
  var = 2;
}
else{
  var = 0;
}

if (state3 == LOW){
  var = 3;
}
else{
  var = 0;
}

if (state4 == LOW){
  var = 4;
}
else{
  var = 0;
}

if (state5 == LOW){
  var = 5;
}
else{
  var = 0;
}

switch (var) {
case 1:
analogWrite(ledPin1, pot2);
analogWrite(ledPin2, pot2);
analogWrite(ledPin3, pot2);
analogWrite(ledPin4, pot2);
analogWrite(ledPin5, pot2);
delay(pot1);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
delay(pot1);

;
break;

case 2:
analogWrite(ledPin1, pot2);
analogWrite(ledPin2, pot2);
analogWrite(ledPin3, pot2);
analogWrite(ledPin4, pot2);
analogWrite(ledPin5, pot2);

;
break;

case 3:
analogWrite(ledPin1, brightness1);
analogWrite(ledPin2, brightness2);
analogWrite(ledPin3, brightness3);
analogWrite(ledPin4, brightness4);
analogWrite(ledPin5, brightness5);

brightness1 = brightness1 + fadeAmount1;
brightness2 = brightness2 + fadeAmount2;
brightness3 = brightness3 + fadeAmount3;
brightness4 = brightness4 + fadeAmount4;
brightness5 = brightness5 + fadeAmount5;

  if (brightness1 == 10 || brightness1 == 255) {
        fadeAmount1 = -fadeAmount1 ; }
  if (brightness2 == 10 || brightness2 == 255) {
        fadeAmount2 = -fadeAmount2 ; }
  if (brightness3 == 10 || brightness3 == 255) {
        fadeAmount3 = -fadeAmount3 ; }
  if (brightness4 == 10 || brightness4 == 255) {
        fadeAmount4 = -fadeAmount4 ; }
  if (brightness5 == 10 || brightness5 == 255) {
        fadeAmount5 = -fadeAmount5 ; }
        
        delay(pot1); 
;
break;

case 4:
analogWrite(ledPin1, brightness6);
analogWrite(ledPin2, brightness7);
analogWrite(ledPin3, brightness8);
analogWrite(ledPin4, brightness9);
analogWrite(ledPin5, brightness10);

brightness6 = brightness6 + fadeAmount6;
brightness7 = brightness7 + fadeAmount7;
brightness8 = brightness8 + fadeAmount8;
brightness9 = brightness9 + fadeAmount9;
brightness10 = brightness10 + fadeAmount10;

  if (brightness6 == 10 || brightness6 == 255) {
        fadeAmount6 = -fadeAmount6 ; }
  if (brightness7 == 10 || brightness7 == 255) {
        fadeAmount7 = -fadeAmount7 ; }
  if (brightness8 == 10 || brightness8 == 255) {
        fadeAmount8 = -fadeAmount8 ; }
  if (brightness9 == 10 || brightness9 == 255) {
        fadeAmount9 = -fadeAmount9 ; }
  if (brightness10 == 10 || brightness10 == 255) {
        fadeAmount10 = -fadeAmount10 ; }
        
        delay(pot1); 
;
break;
}
}

Problem is that program does not work every time. What am I doing wrong?

The problem is that you are writing a bunch of code assuming that the switches work perfectly, without having tested that that is a valid assumption.

const int button1 = 0;
const int button2 = 1;
const int button3 = 2;
const int button4 = 3;
const int button5 = 4;

It's really not a good idea to use pins 0 and 1. Doing so completely removes your ability to debug the code.

I am also interested what will happen if two switches at same time are on?

Look at the code. It is perfectly obvious what will happen.

I have tested it before separately, and always only first one wasn't working. I though meabe I should test in live, but nothing changed...

Will change to other digital pins, no problem.

Both of programs will be on, right?

Any suggestions?

Any suggestions?

I suggest we have a drink, and celebrate the end of the work day. I'll have a rum and Pepsi.

What kind of suggestions did you have in mind?

I have tested it before separately, and always only first one wasn't working.

I didn't follow this. Only the first what? If you are referring to the first switch, the one that is attached to the serial port pin, I'm not too surprised it didn't work.

I though meabe I should test in live, but nothing changed...

How else can you test it?

Both of programs will be on, right?

Will be on what?

The primary reason for getting the switches off of pins 0 and 1 is so that you can add Serial.begin() to setup() and Serial.print() and Serial.println() statements to loop() to see what is happening.

Not sure what time zone are you into, but here it has just started :stuck_out_tongue:

So, actually, I thought about suggestions how to write that part of program. Is my method good?
About the first switch, I've connected it to digital pin. Have I wrote it wrong?

And, once again, sorry for such a topic and answers, I'm beginner, remember yourself at these days :wink:

Look at your code. Imagine that switch 3 is pressed.

if (state1 == LOW){
  var = 1;
}
else{
  var = 0;
}

The value in state1 is HIGH, so var is set to 0. That's fine.

if (state == LOW){
  var = 2;
}
else{
  var = 0;
}

The value in state2 is HIGH, so var is set to 0. That's fine.

if (state3 == LOW){
  var = 3;
}
else{
  var = 0;
}

The value in state3 is LOW, so var is set to 3. That's good. That's what we want.

if (state4 == LOW){
  var = 4;
}
else{
  var = 0;
}

The value in state4 is HIGH, so var is set to 0. That is most definitely not a good thing.

if (state5 == LOW){
  var = 5;
}
else{
  var = 0;
}

The value in state5 is HIGH, so var is set to 0. Well, it was trashed anyway, so it doesn't matter that we just trashed it again.

switch (var) {
case 1:
// Stuff
case 2:
// Stuff
case 3:
// Stuff
case 4:
//Stuff
}

Nothing to do if switch 5 is pressed...

The way the code is currently written, the only possible values in var are 0 or 5. You don't have cases for either of these values.

Hopefully, a clue-by-4 has whacked you, and you see how to fix the program.

Yeah, it's obvious now. Thanks. I had debugged it using Serial Monitor