Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Variables
- int clicks = 0;
- int clicked = 0;
- int lastClick = 0;
- // Pins
- int switchPin = 8;
- // Setup
- void setup() {
- // Pins
- pinMode(switchPin, INPUT);
- // Serial
- Serial.begin(9600);
- }
- // Loop
- int previousVal, startTime, changeTime;
- void loop() {
- // Variables
- int switchVal = digitalRead(switchPin);
- // Value
- if(switchVal != previousVal) {
- changeTime = millis();
- }
- // Reset
- previousVal = switchVal;
- // Change
- if((millis()-changeTime) > 50) {
- if(switchVal) {
- // Inactive
- if(!clicked) {
- // Variables
- clicks++;
- clicked = 1;
- startTime = millis();
- // Reset
- if(clicks == 11) {
- clicks = 1;
- }
- // Debug
- Serial.print("Count: ");
- Serial.print(clicks);
- Serial.print(" Time: ");
- Serial.print(millis()-lastClick);
- Serial.println(" ms;");
- // Reset
- lastClick = millis();
- }
- }else{
- // Debug
- if(clicked) {
- Serial.print("Hold time: ");
- Serial.println(millis()-startTime);
- Serial.println();
- }
- // Variables
- clicked = 0;
- }
- }
- // LED
- if(clicks == 10) {
- analogWrite(5, 255);
- }else{
- analogWrite(5, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement