Toggle Switch Erratic behavior

Dear All,
I'm working on a project where, where based on combination of switch selected a set of actions are to be taken. For my project I need total of 12 Switches.
I'm trying to connect multiple pushbutton as toggleswitch. When I load the code for more than two inputs, the Arduino behaves erratically.
Here is my finding
S1, S2 -> combinations are detected perfectly no problem.
Once I add code for S3 and load, the S3 State is never detected. Moreover, S3 state erratically changes when I press S1.
I'm really struggling to solve this, as project I'm planning can be realized only using toggle state.
I've checked the connections with multiple pins and even changed different boards. The problem is similar when I try to load the code for more than two switch.
You can try loading the code and check the state too.

int del=100;
int S1=2;
int S1new;
int S1old=1;
int S1State=0;

int S2=3;
int S2new;
int S2old=1;
int S2State=0;

int S3=4;
int S3new;
int S3old=1;
int S3State=0;

int S4=5;
int S4new;
int S4old=1;
int S4State=0;

int S5=6;
int S5new;
int S5old=1;
int S5State=0;

void setup() {
  // put your setup code here, to run once:

pinMode (S1, INPUT);
pinMode (S2, INPUT);
pinMode (S3, INPUT);
pinMode (S4, INPUT);
pinMode (S5, INPUT);
Serial.begin (9600);
}

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

S1new=digitalRead(S1);
if (S1old==0 && S1new==1) {
  if(S1State==0){
    S1State=1;
      }
  else {
    S1State=0;
  }
  }
S1old=S1new;

S2new=digitalRead(S2);
if (S2old==0 && S2new==1) {
  if(S2State==0){
    S2State=1;
      }
  else {
    S2State=0;
  }
  }
S2old=S2new;   
  

S3new=digitalRead(S3);
if (S3old==0 && S3new==1) {
  if(S3State==0){
    S3State=1;
      }
  else {
    S3State=0;
  }
  }
S3old=S3new;   





Serial.print("S3 State");
Serial.println(S3State);


delay(del);   
 
}

It is very difficult to see the value of the pull down resistors on those switches. Have you measured them?
They should be about 10K, if they are too low then you could be exceeding the switch's current rating.

Also that is not the best way to wire switches. The switches should be wired between the input and ground. No connection to 5V, no resistors.

Then change your setup function to be:-

pinMode (S1, INPUT_PULLUP);
pinMode (S2, INPUT_PULLUP);
pinMode (S3, INPUT_PULLUP);
pinMode (S4, INPUT_PULLUP);
pinMode (S5, INPUT_PULLUP);

This will invert the state you read but this will not matter as you can easily change that in software.

If you problem persists then it is something mechanical.

Place a 220R resistor in series with a LED then put across the power rails.

This will act as a power LED to prove visually that power is there.


As stated:


IMO

S3old=S3new;

Might be better worded:

LastS3 = CurrentS3;

Or
S3Previous = S3Current;

Not quite sure what you intend here.

Trouble is, we would have to replicate your circuit!

Much nicer if we can find the problem by examination of the code, or guess the problem with the hardware, but I cannot spot it. The three instances of the switch code appear to be identical except for the numeral and you have not made the mistake of using pins 0 or 1. :thinking:

I will guess you are using a UNO and the fact that you are using the serial monitor indicates it is connected to and powered by a PC. Presumably there are no other parts connected. So the problem is certainly not obvious.

Thank you Mike, I hope my question didnt make you Grumpy :P, but your tip helped. Now it works!

1 Like

Cheers! How did you draw this any software? looks cool.

The pinMode change to INPUT_PULLUP worked for me.

That would suggest - not surprisingly - that your wiring was faulty. :thinking:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.