Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <Stepper.h>
  2.  
  3.   // A 200-step motor connected on pins 2-5
  4.   Stepper stepper(200,3,4,5,6);
  5.   const int buttonPin = 2;
  6.   const int limitBottom = 8;
  7.   const int limitTop = 7;
  8.   const int LED1 = 11;  // ready
  9.   const int LED2 = 12;  //brewing
  10.   const int readyButton = 13;
  11.   const int touchSensor = 10;
  12.   const int limitSwitchActivated = HIGH;
  13.  
  14.   //variable
  15.   volatile int buttonState = 0;
  16.  
  17.  
  18.   //variables to keep track of the timing of recent interrupts
  19.     unsigned long button_time = 0;  
  20.     unsigned long last_button_time = 0;
  21.  
  22.  
  23. void setup()
  24. {
  25.  
  26.   pinMode(limitBottom, INPUT);  
  27.   pinMode(limitTop, INPUT);
  28.   pinMode(LED1, OUTPUT);   //ignore for now
  29.   pinMode(LED2, OUTPUT);   // ignore for now
  30.   pinMode(buttonPin, INPUT);
  31.   pinMode(readyButton, INPUT);
  32.     // Attach an interrupt to the ISR vector
  33.   attachInterrupt(digitalPinToInterrupt(2),pin_ISR,RISING);
  34.  
  35.   stepper.setSpeed(60);
  36.  
  37. }
  38.  
  39. void loop() {
  40.  
  41.   /*          ******* STEP 1 *******
  42.    *  on start check bottom limit, reach it if not active */
  43.    
  44.   while (digitalRead(limitBottom) != limitSwitchActivated) {
  45.     stepper.step(-1);   //check if bottom limit is hit  
  46.                         //if not, lower until it's hit.
  47.     }
  48.  
  49.  
  50.   delay(50);
  51.  
  52.   /*          ******* STEP 2 *******
  53.    *   check ready button after bottom limit is active.
  54.    *   if touch detected, Step CW until top limit
  55.    *   activated
  56.    */
  57.  
  58.    while ((digitalRead(readyButton) == LOW)) {
  59.     //do nothing while no touch detected
  60.    }
  61.      
  62.    
  63.     while(digitalRead(limitTop) != limitSwitchActivated) {
  64.         stepper.step(1);
  65.       }
  66.    
  67.     delay(5000);
  68.    }
  69.  
  70.    
  71.  
  72.   /*         ******* STEP 3 *******
  73.    *          Return to bottom position
  74.    *       Searching for either the top limit switch to be active
  75.    *       or interrupt program via button.
  76.    */
  77.    
  78.  
  79.  
  80.     // Interrupt service routine for interrupt 0
  81.     void pin_ISR() {
  82.       button_time = millis();
  83.       if (button_time - last_button_time > 250) {
  84.       //check to see if increment() was called in the last 250 milliseconds
  85.     // this is to avoid the button sensing more than one push accidentally, in case it screws with the code
  86.      
  87.       (digitalRead(limitBottom) != limitSwitchActivated);
  88.        stepper.step(-1);
  89.       }
  90.       else {
  91.         //nada
  92.         }
  93.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement