I have a bipolar stepper motor running on a battery that needs to switch off when the joystick in in the neutral position (to stop over heating and battery life). A DC motor won't do as I need the small increments to control a laser over large distances (building site) The shield is for a Uno from DF robots
The shield does not have a sleep option
I've tried switching the phases LOW and HIGH in the loop but no joy
Here is the basic script
#include <Stepper.h>
const int num_step = 200; // motor steps per revolution
const int pwm_cha = 6;
const int dir_cha = 7;
const int pwm_chb = 4;
const int dir_chb = 5;
int Pot1 = A1;
int val;
Stepper stepper(num_step, dir_cha, dir_chb);
void setup()
{
Serial.begin(9600);
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
}
void loop()
{
int PVal=analogRead(Pot1);
if (PVal >= 507 || PVal <= 517)
{
// This is the neutral bit that needs to turn off the 2 Phases
}
if (PVal > 517 )
{
int K= map (PVal, 518,1023,0,150);
int S=map (K, 0,150,0,num_step);
stepper.setSpeed(K); //this sets the speed of the stepper motor
stepper.step(S); //this sets the direction
Serial.print("Forward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
if (PVal < 507 )
{
int K= map (PVal, 0,506,150,0);
int S=map (K, 0,150,0,num_step);
stepper.setSpeed(K); //this sets the speed of the stepper motor
stepper.step(-S); //this sets the direction
Serial.print("Backward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
}
// End of file
I've tried to slip this bit in but still no joy
// declaration here!
int val = 0;
int prev;
void loop() {
// keep the previous value
prev = val;
// just set the value
val = analogRead(A1); // read the input on analog pin 0:
// compare the previous value
if (val > prev) {
Serial.println("up");
} else if (val < prev) {
Serial.println("down");
}
// and no need to leave empty an else
block
}
Thanks in advance
Laser.ino (1.29 KB)