luni64 / TeensyStep

Fast Stepper Motor Library for Teensy boards
https://luni64.github.io/TeensyStep/
MIT License
272 stars 56 forks source link

Help with sensor based run/stop #149

Open parrotcam opened 1 year ago

parrotcam commented 1 year ago

I have included notes in the following code describing what I am trying to accomplish and some of the many attempts I've made to come up with logic to make it work. I'm not sophisticated at programming, though I have made a variety of successful projects with non-Arduino systems. For some (probably obvious) reason, this one eludes me.

/*  There are 12 magnets on a rotational perimeter with a hall effect sensor used to determine when one has arrived.
 *  The desire is to pass up 11 magnets and stop at the 12th. I can get the system to respond and stop 
 *  at every sensor with the While routine below. The accompanying If routine will not respond when a 
 *  magnet passes. The other two routines simply stop at eveRy magnet. 
 *  The system uses a Teensy 3.5 and a TB67S279FTG driver.
 */

#include "Arduino.h" 
#include "TeensyStep.h"

Stepper motor(19,18);                          // STEP pin: 19, DIR pin: 18
RotateControl controller;                     // use for indefinite moves

const byte ENABLE = 14;                     // to enable the driver (or save current drain)
const byte EXPOSURE = 8;                  // button to initiate exposure  
const byte HOUR_PIN = 4;                  // hour magnets interacting with sensor
byte hour_count = 0;                           // last known hour count : for tracking exposure progress

void setup() {
  pinMode(ENABLE, OUTPUT);             // ENABLE is output pin
  digitalWrite(ENABLE, HIGH);              // disable driver
  pinMode(EXPOSURE, INPUT_PULLUP);            // exposure switch
  pinMode(HOUR_PIN, INPUT_PULLUP);            // sensor magnet locations
  motor
    .setMaxSpeed(10000)                      // steps/s
    .setAcceleration(50000);                  // steps/s^2  
}

void exposure() {
   hour_count= 0;                                 // reset hour_count
  digitalWrite(ENABLE, LOW);              // enable driver
  controller.rotateAsync(motor);        

/* THIS DOES NOT RESPOND TO THE MAGNETS
  if (digitalRead(HOUR_PIN) == LOW){
    controller.stop();                             // stop motor and return immediately     
  }
*/

/* THIS WILL RESPOND TO EACH MAGNET AND STOP AT EACH IN TURN
    while (digitalRead(HOUR_PIN) == HIGH){}   // wait until magnet is sensed
    controller.stop();
    delay(5000);
*/

/* THIS STOPS AT EVERY MAGNET
  while (hour_count < 12){
    if (digitalRead(HOUR_PIN) == LOW){
      hour_count = hour_count + 1; 
    }
  }
  controller.stop();                                   // stop motor and return immediately
}
*/

/* THIS STOPS AT EVERY MAGNET
  while (hour_count < 12){
      if (digitalRead(HOUR_PIN) == LOW){
        hour_count = hour_count + 1; 
      }
  }
  controller.stop();                                // stop motor
}
*/

void loop() {                                                   // look for button actuation
    if (digitalRead(EXPOSURE) == LOW){        // has exposure button been pressed
      exposure();
    }
}