Making a Auto Blast Gate for dust collection in my wood shop. Arduino Nano using ACS712's to detect when tool is in use. Using DRV8825 to drive a single Stepper motor that will spin a disk to line up blast gate for the tool in use. Code works as expected with one sensor coded but I can't figure out how to make it work with additional sensor coded in. Not sure if this is the way the question shows up, Thanks
/*This one is atarting to work. When tool is turn on the ACS712 detects it and the stepper moves 90 deg/steps.
When the tool is turned off it remains in position which is desireable as I would use the same tool multiable times in a row. Need to add sensor A1 and assign different steps to it. Can I declare the values of each sensor 1 time to be used whenever that analog port read > current Threshold? Can I have a statement that will check all the analogs and act on the one that is >current Threshold? Need for the stepper to only move if a different sensor (than the previous analog#) is > current Threshold? IF/Else */
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "DRV8825.h"
#define MOTOR_STEPS 360 // Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define RPM 200
#define MICROSTEPS 1 // Since microstepping is set externally, make sure this matches the selected mode
// If it doesn't, the motor will move at a different RPM than chosen
// 1=full step, 2=half step etc.
#define DIR 2 // All the wires needed for full functionality
#define STEP 3
#define ENABLE 4 // 2-wire basic config, microstepping is hardwired on the driver
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, ENABLE);
#define CURRENT_SENSOR_PIN A0 // Current sensor is connected to analog pin A0
#define CURRENT_THRESHOLD 570 // The analog value above which the relay shall be triggered
#define CURRENT_SAMPLE_PERIOD 500 // The number of milliseconds to sample the current reading
int analogValue = 0; // Stores ADC values read in from the current sensor
unsigned long stopwatch = 0; // Used to keep track of elapsed time
void setup() {
Serial.begin(9600);
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(ENABLE, OUTPUT);
// pinMode (A0, INPUT);
stepper.begin(RPM, MICROSTEPS);
stepper.setEnableActiveState(LOW);
}
void loop() {
analogValue = 0; // Initialize analogValue to a known state
stopwatch = millis(); // Store a snapshot of the current time since the program started executing
// Collect the max ADC value from the current sensor for the predetermined sample period
while (millis() - stopwatch < CURRENT_SAMPLE_PERIOD) {
analogValue = max(analogValue, analogRead(CURRENT_SENSOR_PIN));
Serial.println(analogValue);
}
// If the max ADC value from the current sensor exceeds the threshold, set the state to LOW
if (analogValue > CURRENT_THRESHOLD) {
stepper.enable(); // energize coils - the motor will hold position
stepper.rotate(90); //Moving motor using the degree notation in ()
}
while(analogValue > CURRENT_THRESHOLD)
stepper.disable(); // pause and allow the motor to be moved by hand
}
// need to return to top of loop

loop()function.analogValuedoesn't change in the while loop, so the condition will always be true. – chrisl Apr 14 '20 at 03:48