My program is set up to run a stepper motor at a speed determined by a pot. Basically the stepper moves a carriage back and forth between two optical switches. Each optical switch flips the direction to positive or negative.
The issue is when the input switch is pressed, the stepper moves really fast until it hits one of the optical switch, once the switch goes high then it goes the speed determined by the pot. Everything works as intended after. Not sure what is going on.
Thanks
big
//#include <Wire.h>
//#include <LiquidCrystal_I2C.h> #include <AccelStepper.h>
//LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2);
AccelStepper stepper1(AccelStepper::FULL2WIRE, 26, 25);
int motspd = A4;
int switchon = 0;
int spd = 0;
int spd2 = 20;
int spd3 = 0;
int leftsensor = 31;
int rightsensor = 24;
int switch1 = 53;
int lsensor = 0;
int rsensor = 0;
int gowinder = 32;
Also please use the AutoFormat tool to indent your code consistently so it is easy to see where the different parts begin and end. Every } should be on its own line
Sorry, this looks better. A few other notes. I turned all references to the I2C LCD into comments. I found updating the LCD slowed the program way too much. This is the current code running in the machine. Runs very well but the initial speed is much faster than the set speed as noted before. It’s a head scratcher for me.
big
//#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
//LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2);
AccelStepper stepper1(AccelStepper::FULL2WIRE, 26, 25);
int motspd = A4;
int switchon = 0;
int spd = 0;
int spd2 = 20;
int spd3 = 0;
int spd4 = 0;
int leftsensor = 31;
int rightsensor = 24;
int switch1 = 53;
int lsensor = 0;
int rsensor = 0;
int gowinder = 32;
void setup()
{
stepper1.setMaxSpeed(2200);
stepper1.setAcceleration(500);
// lcd.init();
//lcd.backlight();
pinMode(motspd, INPUT);
pinMode(switch1, INPUT);
pinMode(leftsensor, INPUT);
pinMode(rightsensor, INPUT);
pinMode(gowinder, OUTPUT);
stepper1.setSpeed(300);
}
void loop(){
//lcd.setCursor(0, 0);
//lcd.print("TRAVEL/REV");
//lcd.setCursor(0, 1);
//lcd.print(spd2);
switchon = digitalRead(switch1);
spd = analogRead(motspd);
spd2 = map(spd,0,1000,2,300);
spd3 = spd2 * -1;
if(switchon == HIGH){
digitalWrite(gowinder,HIGH);
lsensor = digitalRead(leftsensor);
rsensor = digitalRead(rightsensor);
if(lsensor == HIGH){
stepper1.setSpeed(spd2);}
if(rsensor == HIGH){
stepper1.setSpeed(spd3);}
stepper1.runSpeed();}
else if(switchon == LOW){
digitalWrite(gowinder,LOW);
stepper1.stop();}
}