Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ledPin = 9;
- int buttonPin = 2;
- int potPin = A0;
- int readValue;
- int writeValue;
- int buttonState; // the current reading from the input pin
- int lastButtonState = HIGH; // the previous reading from the input pin
- // 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 = 50; // the debounce time; increase if the output flickers
- int ButtonPress = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(ledPin, OUTPUT);
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(potPin, INPUT);
- }
- 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 == LOW) {
- if (ButtonPress == 1)
- ButtonPress = 0;
- else
- ButtonPress = 1;
- }
- }
- }
- lastButtonState = reading;
- Serial.println(ButtonPress);
- if (ButtonPress == 1)
- {
- readValue = analogRead(potPin);
- writeValue = (255./1023.) * readValue;
- analogWrite(ledPin, writeValue);
- }
- else
- analogWrite(ledPin, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement