Advertisement
Guest User

Hellvis Help

a guest
Jan 27th, 2013
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. //pins
  2. const int pushButton = 12;
  3. const int stepPin = 13;
  4. const int dirPin = 2;
  5.  
  6. //delays
  7. float siderealDelay = 47.875;
  8. float slewDelay = 10;
  9. float slewDelay2 = 1;
  10.  
  11. //time variables
  12. int time1 = 0;
  13. int time2 = 0;
  14. int buttonHold = 2000;
  15.  
  16. //States
  17. int currState = LOW;
  18. int prevState = LOW;
  19. int buttonState = -1;
  20.  
  21. void setup()
  22. {
  23.   //inputs
  24.   pinMode(pushButton, INPUT);
  25.  
  26.   //outputs
  27.   pinMode(stepPin, OUTPUT);
  28.   pinMode(dirPin, OUTPUT);
  29.  
  30.   //initial
  31.   digitalWrite(stepPin, LOW); // set up pins for initial state
  32.   digitalWrite(dirPin, LOW);
  33.  
  34.   //for testing
  35.   Serial.begin(9600);
  36. }
  37.  
  38. void loop()
  39.  {
  40.    currState = digitalRead(pushButton);
  41.    
  42.    /*
  43.    This part of the code will check the time and state
  44.    Using that information it assigns the buttonState
  45.    to 0, 1, or -1 for tap, hold, no press respectivley.
  46.    */
  47.    if(currState)
  48.    {
  49.      if(prevState && time1-time2 > buttonHold)  
  50.      {
  51.        time2 = time1;
  52.        buttonState = 1;
  53.      }
  54.      else if(prevState)
  55.        time1 = millis();
  56.      else        
  57.        buttonState = 0;
  58.    }
  59.  
  60.    else  
  61.    {
  62.      buttonState = -1;
  63.      time2 = time1;
  64.    }
  65.  
  66.  
  67.    /*
  68.    This part of the code looks at the state and acts on it
  69.    These if else ladder represents the three possible states
  70.    that the button should have been in.
  71.    */
  72.    if(buttonState == 1)       //hold
  73.    {
  74.      Serial.println("hold");
  75.      
  76.      digitalWrite(stepPin, HIGH);
  77.      delay(slewDelay2);
  78.      digitalWrite(stepPin, LOW);
  79.      delay(slewDelay2);
  80.    }
  81.    else if(!buttonState)    //tap
  82.    {
  83.     Serial.println("tap");
  84.      
  85.      digitalWrite(stepPin, HIGH);
  86.      delay(slewDelay);
  87.      digitalWrite(stepPin, LOW);
  88.      delay(slewDelay);
  89.    }
  90.    else                  //no push
  91.    {
  92.      Serial.println("no push");
  93.      
  94.      time2 = time1;
  95.      digitalWrite(stepPin, HIGH);
  96.      delay(siderealDelay);
  97.      digitalWrite(stepPin, LOW);
  98.      delay(siderealDelay);    
  99.    }
  100.    
  101.    prevState = currState;
  102.    //delay(50);    //just so I could read the output easier
  103.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement