// constants won't change. They're used here to set pin numbers:
const int buttonPin = 5; // the pin that the pushbutton is attached to
const int ledPin1 = A0; // the pin that the LED is attached to
const int ledPin2 = A1; // the pin that the LED is attached to
const int ledPin3 = A2; // the pin that the LED is attached to
float brightness = 10; // the pin that the LED is attached to
// Variables will change:
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int buttonPushCounter = 40;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 10; // the debounce time; increase if the output flickers
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// set initial LED state
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
Serial.begin(9600);
}
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 LOW to HIGH), 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 == HIGH) {
ledState = !ledState;
buttonPushCounter+=10;
}
}
}
brightness = map(buttonPushCounter, 0, 90, 100, 255);
Serial.print("brightness: ");
Serial.println((255-brightness));
analogWrite(ledPin1, (255-brightness));
analogWrite(ledPin2, (255-brightness));
analogWrite(ledPin3, (255-brightness));
// Serial.println("on");
// Serial.print("number of button pushes: ");
// Serial.println(buttonPushCounter);
// set the LED:
if (buttonPushCounter > 90) {
buttonPushCounter = 0;
analogWrite(ledPin1, LOW);
analogWrite(ledPin2, LOW);
analogWrite(ledPin3, LOW);
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState=reading;
}