Hi guys, enjoying my new arduino and brushing up on my programming. I'm basically just trying to incremental a variable to control the switch case loop structure.
I can't for the life of me to get buttonpresses == 0; :
also, when the fading function is running, the speed of my serialprint and buttonvalue functions slow down, is there a way around that?
int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int fadeval = 0;
int buttonpresses = 0;void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
Serial.begin( 9600 ); // set the serial communication rate
}void loop(){
serialprint();
buttonvalue();switch (buttonpresses) {
case 1:
if (buttonpresses == 1){
fading();
}case 2:
if (buttonpresses == 2) {
blinking();
}
case 3:
if (buttonpresses >= 3) {
buttonpresses == 0;
}
}
}// to see what the buttonpress variable is
void serialprint() {
Serial.print(buttonpresses); // print the value
Serial.println(); // print a linefeed character
}// checks and increments buttonpresses variable
void buttonvalue(){val = digitalRead(inputPin); // read input value
if (val == LOW) {
buttonpresses++;
}
delay(25);
}//------Blinking mode
void blinking(){
digitalWrite(ledPin, LOW); // turn LED OFF
delay(50);
digitalWrite(ledPin, HIGH); // turn LED ON
delay(50);
}//------Fading mode
void fading() {for(fadeval = 0 ; fadeval <= 255; fadeval+=5) { // fade in (from min to max)
analogWrite(ledPin, fadeval); // sets the value (range from 0-255)
delay(10);
}for(fadeval = 255; fadeval >=0; fadeval-=5) { // fade out (from max to min)
analogWrite(ledPin, fadeval);
delay(10);
}
}