hi,
I am using a rain sensor (A0)analog output to drive a nema stepper,,,working but part of the code is wrong.
I have got it to home using a limit switch but then want it to stay at that home position untill A0 changes but what it does is move to -800 position then it senses A0 and moves accordingly......
In the loop section I am obviously telling "stableVal" to equal -800;;;thats why it moves there as the value of the sensor is 1023 ,no water ,,,so how would I tell it to stay at "0" then move when water changes...my logic is wrong somewhere...
would appreciate some help....please...
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepperX(1, 5, 4); // 1 = Easy Driver interface
// dm320 Pin 5 connected to STEP pin of Easy Driver
// dm320 Pin 4 connected to DIR pin of Easy Driver
// Define the Pins used
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
int previous = 0;
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
pinMode(home_switch, INPUT_PULLUP);
delay(5); // Wait for EasyDriver wake up
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepperX.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperX.run(); // Start moving the stepper
delay(5);
}
stepperX.setCurrentPosition(0); // Set the current position as zero for now
stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepperX.moveTo(initial_homing);
stepperX.run();
initial_homing++;
delay(5);
}
stepperX.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepperX.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepperX.setAcceleration(1000.0); // Set Acceleration of Stepper
// Print out Instructions on the Serial Monitor at Start
//Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
}
void loop() {
int sensorRead = analogRead(A0); //read the input on analog pin 0:
Serial.println(sensorRead); //print out the value you read:
delay(1000); // delay in between reads for stability
int val = analogRead(A0);
int stableVal;
if ((val > previous + 20 ) || (val < previous - 20 )) {
stableVal = map (val, 0, 1023, 0 , -800);
stepperX.runToNewPosition(stableVal);
stableVal = previous; // remember the previous value of the sensor
}
delay(1000); // Wait 1 seconds before moving the Stepper
}