Hello. First off all I was wondering why the program is reacting to the buttonpushes slow, because I have to push the button many times before it "detects" it, like there is a delay before the UNO is doing a next reading.
The second problem is that I want the program to count the amount of buttonpresses, wait for maybe half a second, then do the case after how many buttonpresses that have been count within the half second period. Anyone that could help me programming that?
Here's my code:
int led1 = 1;
int switch1 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int led9 = 9;
int led10 = 10;
int led11 = 11;
int led12 = 12;
int led13 = 13;
int val = 0; // variable for reading the pin status
int buttonState; // variable to hold the button state
int buttonPresses = 0; // 10 presses to go!
void setup() {
// put your setup code here, to run once:
pinMode(switch1, INPUT); // Set the switch pin as input
pinMode(led1, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
pinMode(led10, OUTPUT);
pinMode(led11, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
buttonState = digitalRead(switch1); // read the initial state
}
void loop() {
buttonState = digitalRead(switch1); // read the initial state
val = digitalRead(switch1); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is now pressed
buttonPresses++;
switch (buttonPresses) {
case 1:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(led10, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(50);
}
break;
case 2:
digitalWrite(led7, HIGH);
break;
case 3:
digitalWrite(led4, HIGH);
break;
case 4:
digitalWrite(led13, HIGH);
break;
default:
buttonPresses = 0;
digitalWrite(led4, LOW);
digitalWrite(led7, LOW);
digitalWrite(led10, LOW);
digitalWrite(led13, LOW);
}
}
}
}