const int pwmPin = 1; // pwnPin number conencted to MCU
const int buttonPin = 4; // the number of the pushbutton pin
const int analogPin = A3; // battery voltage monitor resistor connected to analog pin 3
int var = 4; //default
int lastButtonState = LOW; // the previous reading from the input pin
int buttonState; // the current reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 150; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(2, OUTPUT); // sets the digital pin 2 as output
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from HIGH to LOW), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == LOW) {
var = var + 1;
if (var >= 5) {
var = 1;
}
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
switch (var) {
case 1:
analogWrite(pwmPin, 0);
digitalWrite(0, HIGH);
break;
case 2:
analogWrite(pwmPin, 255);
digitalWrite(0, LOW);
break;
case 3:
analogWrite(pwmPin, 10);
digitalWrite(0, HIGH);
break;
case 4:
analogWrite(pwmPin, 0);
digitalWrite(0, LOW);
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
}