Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int ledPin = 9;
  2. int buttonPin = 2;
  3. int potPin = A0;
  4. int readValue;
  5. int writeValue;
  6.  
  7. int buttonState;             // the current reading from the input pin
  8. int lastButtonState = HIGH;   // the previous reading from the input pin
  9.  
  10. // the following variables are unsigned longs because the time, measured in
  11. // milliseconds, will quickly become a bigger number than can be stored in an int.
  12. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  13. unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
  14.  
  15. int ButtonPress = 0;
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19.   pinMode(ledPin, OUTPUT);
  20.   pinMode(buttonPin, INPUT_PULLUP);
  21.   pinMode(potPin, INPUT);
  22. }
  23.  
  24. void loop() {
  25.   // read the state of the switch into a local variable:
  26.   int reading = digitalRead(buttonPin);
  27.  
  28.   // check to see if you just pressed the button
  29.   // (i.e. the input went from LOW to HIGH), and you've waited long enough
  30.   // since the last press to ignore any noise:
  31.  
  32.   // If the switch changed, due to noise or pressing:
  33.   if (reading != lastButtonState) {
  34.     // reset the debouncing timer
  35.     lastDebounceTime = millis();
  36.   }
  37.  
  38.   if ((millis() - lastDebounceTime) > debounceDelay) {
  39.     // whatever the reading is at, it's been there for longer than the debounce
  40.     // delay, so take it as the actual current state:
  41.  
  42.     // if the button state has changed:
  43.     if (reading != buttonState) {
  44.       buttonState = reading;
  45.  
  46.       // only toggle the LED if the new button state is HIGH
  47.       if (buttonState == LOW) {
  48.         if (ButtonPress == 1)
  49.           ButtonPress = 0;
  50.         else
  51.            ButtonPress = 1;
  52.       }
  53.     }
  54.   }
  55.  
  56.   lastButtonState = reading;
  57.  
  58.   Serial.println(ButtonPress);
  59.  
  60.   if (ButtonPress == 1)
  61.   {
  62.     readValue = analogRead(potPin);
  63.     writeValue = (255./1023.) * readValue;
  64.     analogWrite(ledPin, writeValue);
  65.   }
  66.   else
  67.     analogWrite(ledPin, 0);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement