Help star tracker

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

You appear to have stepper and LCD sharing pins 3 4 5 6. Move the stepper to different pins

Nick_Pyner:
You appear to have stepper and LCD sharing pins 3 4 5 6. Move the stepper to different pins

Are you suggesting moving the stepper pins to something like this?

#define motorPin1  1     // IN1 on the ULN2003 driver 1
#define motorPin2  2     // IN2 on the ULN2003 driver 1
#define motorPin3  3     // IN3 on the ULN2003 driver 1
#define motorPin4  10     // IN4 on the ULN2003 driver 1

Pick pins that aren't being used by something else. Pin 1 is for the serial port.

#define motorPin1  1     // IN1 on the ULN2003 driver 1

jremington: Pick pins that aren't being used by something else. Pin 1 is for the serial port.

#define motorPin1  1     // IN1 on the ULN2003 driver 1

Can you help me with the ones that i can use?

Sure, if you will tell us (a) exactly which Arduino you have and (b) exactly which pins you are using, and for what.

For helpful advice on posting questions to the forum, see the "How to use this forum" post.

These are some photos. |373x500 |373x500 |373x500 |500x375

I have tried the pins 3,4,5,6 without the screen and the stepper rotates. When I add the screen it doesn't work.

I notice that in the second sketch you don't use 'moveTo()' to set a destination. It could be that is why the motor does not move.

johnwasser: I notice that in the second sketch you don't use 'moveTo()' to set a destination. It could be that is why the motor does not move.

I have to add

stepper1.moveTo(speeds);

Faulk_08: Are you suggesting moving the stepper pins to something like this?

Anything that isn't used by LCD BUT pins 0,1 which are used for serial. Don't forget that you can use the analogue pins as digital, and they make a useul group...

Nick_Pyner:
Anything that isn’t used by LCD BUT pins 0,1 which are used for serial. Don’t forget that you can use the analogue pins as digital, and they make a useul group…

thanks for the help. Right now i’m on this

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  A1     // IN1 on the ULN2003 driver 1
#define motorPin2  A2     // IN2 on the ULN2003 driver 1
#define motorPin3  A3     // IN3 on the ULN2003 driver 1
#define motorPin4  A4     // 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(271.6);
  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 it runs ok.

Then I upgrade the code for the use of the LCD and doesn’t work 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  A1     // IN1 on the ULN2003 driver 1
#define motorPin2  A2     // IN2 on the ULN2003 driver 1
#define motorPin3  A3     // IN3 on the ULN2003 driver 1
#define motorPin4  A4     // 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); 
        stepper1.moveTo(speeds); //add 
}

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 +++ ");
                stepper1.run(); //add
            }
            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;
}

I add the stepper1.run(); and stepper1.moveTo(speeds); and makes no difference.