I am trying to create a code for a stepper motor that will stop for 10 seconds and then continue turning the stepper motor. i am using a external driver for a nema 17 stepper and an external power supply. please see a excerpt from my code below. Essentially I want to create a square wave
if (psi > 5099) { // initialize pressure, assuming pressure is high at the start of test
stepper.step(-100);
}
else if (psi == 5100) { // high pressure delay
delay(10000); // stop for 10 second
stepper.step(-100) // after delay continue to turn stepper
}
else if (psi == 3100) { // lower limit delay
delay(10000); // stop for 10 second
stepper.step(100); // reverse direction to bring pressure back up to 5100 psi
}
And.... what is it doing vs. what you expect it to do? Snippets of code are useless. Post the entire sketch as outlined in the sticky post at the top of the forum. It helps people help you.
i see, i have fixed that now. I want the program to stop once the pressure hits 5100 psi for 10 seconds and then continue to move the stepper. Right now with the code, it stops and moves the stepper every 10 seconds. is there a better way to make that stepper move constantly after the 10 seconds wait.
when i add that to my code i do not get the desired wait. Please let me know if i did this right.
include <Stepper.h> // include the stepper library.
#define STEPS 200 // number of steps for one rotation.
Stepper stepper(STEPS, 5, 3); // define the steps, pin 5 is direction, and pin 3 is the steps.
#define motorInterfaceType 1 // defines the case for the stepper library, this one is the external driver.
float psi; // define the variable for psi as a conversion.
int T1 = A0; // transducer is connected to the anolog 0 pin.
int volt; // initialize the voltage variable.
bool alreadyDelayed;
void setup() {
Serial.begin(9600); // setup the serial monitor read and write rate.
stepper.setSpeed(800); // set the speed for the rotation RPM.
pinMode(T1, INPUT); // sets T1 as in input.
}
void loop() {
volt = analogRead(T1); // set voltage to the value at T1.
Serial.print("T1: "); // Prints T1:
Serial.println(volt); // prints the value of T1
//delay(10); //wait for half a second.
psi = (9.77 * (analogRead(T1))) + (0); // converts the voltage value to PSI.
// y=mx+b where m is slope (transducer reading (0-10000)/voltage read (0-1023)) + b (offset)
Serial.print("PSI: "); // Prints PSI:
Serial.println(psi); // Prints the value of psi
//delay(10); // wait for half a second.
if (psi > 5109) { // initialize pressure, assuming pressure is high at the start of test
stepper.step(-5); // clockwise
}
else if (psi == 5100) { // high pressure delay
if (! alreadyDelayed){
delay(10000);
alreadyDelayed = true;
}
stepper.step(-5); // after delay continue to turn stepper
}
else if (psi == 3100) { // lower limit delay
delay(10000); // stop for 5 second
stepper.step(5); // reverse direction to bring pressure back up to 5100 psi
}
}
It might just be that you need to intialise "alreadyDelayed" to false to start. Either that or change the check to "alreadyDelayed != true". I'm fairly new to Arduino so take my guidance as broad strokes.
i do expect that to be true because the stepper will open a valve reliving pressure to bring the pressure down. so this will indeed change the state of psi.
When you are dealing with floating point numbers like psi, you will never hit exactly 5100 or 3100. You need to be testing for >= or <=. It was not clear to me if the stepper moves once to open/close something or if it should move multiple times to increase/decrease the opening. This code assumes it only moves once
#include <Stepper.h> // include the stepper library.
#define STEPS 200 // number of steps for one rotation.
Stepper stepper(STEPS, 5, 3); // define the steps, pin 5 is direction, and pin 3 is the steps.
#define motorInterfaceType 1 // defines the case for the stepper library, this one is the external driver.
const int T1 = A0; // transducer is connected to the anolog 0 pin.
const unsigned long waitTime = 10000;
const float maxPressure = 5100.0;
const float minPressure = 3100.0;
enum { GOING_UP, GOING_DOWN };
int state = GOING_UP;
unsigned long startTime;
void setup() {
Serial.begin(9600); // setup the serial monitor read and write rate.
stepper.setSpeed(800); // set the speed for the rotation RPM.
pinMode(T1, INPUT); // sets T1 as in input.
// figure out where to start
if ( readPressure() < maxPressure ) {
stepper.step(5);
state = GOING_UP;
}
else {
stepper.step(-5);
state = GOING_DOWN;
}
}
void loop() {
float psi = readPressure();
if ( state == GOING_UP ) {
// pressure is going up
if (psi >= maxPressure) {
// we have reached max, pressure, wait and go back down
delay(waitTime);
state = GOING_DOWN;
stepper.step(-5);
}
}
else {
// pressure is going down
if (psi <= minPressure) {
// lower limit delay
delay(waitTime);
state = GOING_UP;
stepper.step(5);
}
}
}
float readPressure() {
int volt = analogRead(T1); // set voltage to the value at T1.
Serial.print("T1: "); // Prints T1:
Serial.println(volt); // prints the value of T1
//delay(10); //wait for half a second.
float psi = (9.77 * volt) + (0); // converts the voltage value to PSI.
// y=mx+b where m is slope (transducer reading (0-10000)/voltage read (0-1023)) + b (offset)
Serial.print("PSI: "); // Prints PSI:
Serial.println(psi); // Prints the value of psi
//delay(10); // wait for half a second.
return psi;
}