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.
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.
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.