Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. // Variables
  2. int clicks = 0;
  3. int clicked = 0;
  4. int lastClick = 0;
  5.  
  6. // Pins
  7. int switchPin = 8;
  8.  
  9. // Setup
  10. void setup() {
  11.  
  12.   // Pins
  13.   pinMode(switchPin, INPUT);
  14.  
  15.   // Serial
  16.   Serial.begin(9600);
  17.  
  18. }
  19.  
  20. // Loop
  21. int previousVal, startTime, changeTime;
  22. void loop() {
  23.  
  24.   // Variables
  25.   int switchVal = digitalRead(switchPin);
  26.  
  27.   // Value
  28.   if(switchVal != previousVal) {
  29.     changeTime = millis();
  30.   }
  31.  
  32.   // Reset
  33.   previousVal = switchVal;
  34.  
  35.   // Change
  36.   if((millis()-changeTime) > 50) {
  37.     if(switchVal) {
  38.  
  39.       // Inactive
  40.       if(!clicked) {
  41.         // Variables
  42.         clicks++;
  43.         clicked = 1;
  44.         startTime = millis();
  45.  
  46.         // Reset
  47.         if(clicks == 11) {
  48.           clicks = 1;
  49.         }
  50.        
  51.         // Debug
  52.         Serial.print("Count: ");
  53.         Serial.print(clicks);
  54.         Serial.print(" Time: ");
  55.         Serial.print(millis()-lastClick);
  56.         Serial.println(" ms;");
  57.  
  58.         // Reset
  59.         lastClick = millis();
  60.       }
  61.      
  62.     }else{
  63.      
  64.       // Debug
  65.       if(clicked) {
  66.         Serial.print("Hold time: ");
  67.         Serial.println(millis()-startTime);
  68.         Serial.println();
  69.       }
  70.  
  71.       // Variables
  72.       clicked = 0;
  73.      
  74.     }
  75.    
  76.   }
  77.  
  78.   // LED
  79.   if(clicks == 10) {
  80.     analogWrite(5, 255);
  81.   }else{
  82.     analogWrite(5, 0);
  83.   }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement