I have 5 buttons when I push to send 1,3,5,10 impulses to the output,but when I push button sometimes work sometimes not (one of five or six tryes work).
Any help?Thanks!
int ledPin = 13; // LED is connected to digital pin 13
int switchPin1 = 2;// switch connected to digital pin 2
int switchPin2 = 3;
int switchPin3 = 4;
int switchPin4 = 5;
int switchPin5 = 6;
int switchValue; // a variable to keep track of when switch is pressed
int counter = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
pinMode(switchPin1, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin1, HIGH); // sets the default (unpressed) state of switchPin to HIGH
pinMode(switchPin2,INPUT); // sets the switchPin to be an input
digitalWrite(switchPin2, HIGH); // sets the default (unpressed) state of switchPin to HIGH
pinMode(switchPin3, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin3, HIGH); // sets the default (unpressed) state of switchPin to HIGH
pinMode(switchPin4,INPUT); // sets the switchPin to be an input
digitalWrite(switchPin4, HIGH); // sets the default (unpressed) state of switchPin to HIGH
pinMode(switchPin5,INPUT); // sets the switchPin to be an input
digitalWrite(switchPin5, HIGH); // sets the default (unpressed) state of switchPin to HIGH
}
void loop() // run over and over again
{
switchValue = digitalRead(switchPin1); // check to see if the switch is pressed
if ((switchValue == LOW) && (counter <= 1)) { // if the switch is pressed then,
for(int ii = 0; ii <= 1; ii++)
{
digitalWrite(ledPin, !digitalRead(ledPin));
delay(200);
counter++;
}
}
if (switchValue == HIGH)
//if ((switchValue == HIGH) && (counter > 2))
{
counter = 0;
delay(200);
}
{
switchValue = digitalRead(switchPin2); // check to see if the switch is pressed
if ((switchValue == LOW) && (counter <= 1)) { // if the switch is pressed then,
for(int ii = 0; ii <= 3; ii++)
{
digitalWrite(ledPin, !digitalRead(ledPin));
delay(200);
counter++;
}
}
if (switchValue == HIGH)
if ((switchValue == HIGH) && (counter > 2))
{
counter = 0;
delay(200);
}
{
switchValue = digitalRead(switchPin3); // check to see if the switch is pressed
if ((switchValue == LOW) && (counter <= 1)) { // if the switch is pressed then,
for(int ii = 0; ii <= 9; ii++)
{
digitalWrite(ledPin, !digitalRead(ledPin));
delay(200);
counter++;
}
}
if (switchValue == HIGH)
//if ((switchValue == HIGH) && (counter > 2))
{
counter = 0;
delay(200);
}
{
switchValue = digitalRead(switchPin4); // check to see if the switch is pressed
if ((switchValue == LOW) && (counter <= 1)) { // if the switch is pressed then,
for(int ii = 0; ii <= 19; ii++)
{
digitalWrite(ledPin, !digitalRead(ledPin));
delay(200);
counter++;
}
}
if (switchValue == HIGH)
//if ((switchValue == HIGH) && (counter > 2))
{
counter = 0;
delay(200);
}
{
switchValue = digitalRead(switchPin4); // check to see if the switch is pressed
if ((switchValue == LOW) && (counter <= 1)) { // if the switch is pressed then,
for(int ii = 0; ii <= 19; ii++)
{
digitalWrite(ledPin, !digitalRead(ledPin));
delay(200);
counter++;
}
}
if (switchValue == HIGH)
//if ((switchValue == HIGH) && (counter > 2))
{
counter = 0;
delay(200);
}
}
}
}
}
}