Hello to all. I really need your help here.
I have this code that runs smoothly and the stepper motor works.
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define motorPin1 3 // IN1 on the ULN2003 driver 1
#define motorPin2 4 // IN2 on the ULN2003 driver 1
#define motorPin3 5 // IN3 on the ULN2003 driver 1
#define motorPin4 6 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
stepper1.moveTo(20000);
}//--(end setup )---
void loop() {
//Change direction when the stepper reaches the target position
if (stepper1.distanceToGo() == 0) {
stepper1.moveTo(-stepper1.currentPosition());
}
stepper1.run();
}
And on this code doesn't run. The motor doesn't run at all.
#include <AccelStepper.h>
#include <LiquidCrystal.h>
#define HALFSTEP 8
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
int adc_key_val[5] ={50, 200, 400, 600, 800 };
// Motor pin definitions
#define motorPin1 3 // IN1 on the ULN2003 driver 1
#define motorPin2 4 // IN2 on the ULN2003 driver 1
#define motorPin3 5 // IN3 on the ULN2003 driver 1
#define motorPin4 6 // IN4 on the ULN2003 driver 1
int NUM_KEYS = 5;
int adc_key_in;
int key = -1;
int isRun;
double speeds = 271.6;
int maxspeed = 1245;
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void setup()
{
lcd.clear();
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("**STAR-TRACKER**");
lcd.setCursor(0,1);
lcd.print("**BY RC & MC**");
delay(2000);
lcd.clear();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" Stopped ");
lcd.setCursor(0, 1);
lcd.print("Speed ");
lcd.print(speeds);
lcd.print(" ");
isRun = 0;
stepper1.setMaxSpeed(maxspeed);
stepper1.setSpeed(speeds);
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key >= 0) // if keypress is detected
{
if (key == 1)
{
speeds += 0.1;
delay(50);
}
if (key == 2 && speeds > 0)
{
speeds -= 0.1;
delay(50);
}
if (key == 0)
{
speeds += 10;
}
if (key == 3)
{
speeds -= 10;
}
if (speeds > maxspeed)
{
speeds = maxspeed;
}
if (speeds < -maxspeed)
{
speeds = -maxspeed;
}
if (key == 4)
{
isRun = 1 - isRun;
lcd.setCursor(0, 0);
if (isRun == 1)
{
lcd.print("+++ Running +++ ");
}
else
{
lcd.print(" Stopped ");
}
delay(250);
}
lcd.setCursor(0, 1);
lcd.print("Speed ");
lcd.print(speeds);
lcd.print(" ");
stepper1.setSpeed(speeds);
delay(50);
}
if (isRun == 1)
{
stepper1.runSpeed();
}
}
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
return k;
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
thanks in advance