Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //pins
- const int pushButton = 12;
- const int stepPin = 13;
- const int dirPin = 2;
- //delays
- float siderealDelay = 47.875;
- float slewDelay = 10;
- float slewDelay2 = 1;
- //time variables
- int time1 = 0;
- int time2 = 0;
- int buttonHold = 2000;
- //States
- int currState = LOW;
- int prevState = LOW;
- int buttonState = -1;
- void setup()
- {
- //inputs
- pinMode(pushButton, INPUT);
- //outputs
- pinMode(stepPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- //initial
- digitalWrite(stepPin, LOW); // set up pins for initial state
- digitalWrite(dirPin, LOW);
- //for testing
- Serial.begin(9600);
- }
- void loop()
- {
- currState = digitalRead(pushButton);
- /*
- This part of the code will check the time and state
- Using that information it assigns the buttonState
- to 0, 1, or -1 for tap, hold, no press respectivley.
- */
- if(currState)
- {
- if(prevState && time1-time2 > buttonHold)
- {
- time2 = time1;
- buttonState = 1;
- }
- else if(prevState)
- time1 = millis();
- else
- buttonState = 0;
- }
- else
- {
- buttonState = -1;
- time2 = time1;
- }
- /*
- This part of the code looks at the state and acts on it
- These if else ladder represents the three possible states
- that the button should have been in.
- */
- if(buttonState == 1) //hold
- {
- Serial.println("hold");
- digitalWrite(stepPin, HIGH);
- delay(slewDelay2);
- digitalWrite(stepPin, LOW);
- delay(slewDelay2);
- }
- else if(!buttonState) //tap
- {
- Serial.println("tap");
- digitalWrite(stepPin, HIGH);
- delay(slewDelay);
- digitalWrite(stepPin, LOW);
- delay(slewDelay);
- }
- else //no push
- {
- Serial.println("no push");
- time2 = time1;
- digitalWrite(stepPin, HIGH);
- delay(siderealDelay);
- digitalWrite(stepPin, LOW);
- delay(siderealDelay);
- }
- prevState = currState;
- //delay(50); //just so I could read the output easier
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement