Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Stepper Project not working
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Stepper Project not working

by sadams260 on Sat Apr 17, 2021 3:47 pm

This sketch was operating the stepper perfectly until I added some lines to monitor A3 and print results to LCD. Now the stepper barely moves and doesnt respond to joystick clicks at all. I am supplying 11 v to the driver board as well as Vin. Another interesting thing is the LCD (A3) reading fluctuates with joystick movement.
Code: Select all | TOGGLE FULL SIZE
/* this sketch will control a stepper motor proportionally via a push
button joystick, pressing the joystick increases the motor speed, works
especially well with the small driver board from ebay, also works well with the
TB6600 but adjust the amperage maybe try adjusting the step speed to
a higher value
*/
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

Adafruit_LiquidCrystal lcd(6, 5, 7);//Or Data pin is #6, Clock is #5 and Latch is #7

#define step_pin 3   // Pin 3 connected to Steps pin on Driver
#define dir_pin 2    // Pin 2 connected to Direction pin on Driver

#define X_pin A0      // Pin A0 connected to joystick x axis
#define Joy_switch 4  // Pin 4 connected to joystick switch
 
 
 
int step_speed = 10;  // Speed of Stepper motor (higher = slower)
 
void setup() {
 
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
 
   pinMode(Joy_switch, INPUT_PULLUP);
   
    // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Target  136");
   
   
}
 
void loop() {
 
  int sensorValue = analogRead(A3);
 
 
  sensorValue = map(sensorValue, 0, 1023, 0, 150);
  lcd.setCursor(0,1);
  lcd.print("Actual");
  lcd.setCursor(8,1);
  lcd.print(sensorValue);
  Serial.println(sensorValue);
  lcd.setBacklight(HIGH);
 
 
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10:
        step_speed=3;  // medium speed
        break;
    }
  }   
   
  if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
    //if (!digitalRead(Limit01)) {}  // check if limit switch is activated
   
       {  //  if limit switch is not activated, move motor clockwise
     
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }     
  }
 
    if (analogRead(X_pin) < 312) {  // If joystick is moved right
   
   
   
      {  //  if limit switch is not activated, move motor counter clockwise
     
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }     
  }
}

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sat Apr 17, 2021 4:03 pm

This sketch was operating the stepper perfectly until I added some lines to monitor A3 and print results to LCD.

The LCD displays are pretty slow - especially the cursor movement commands. That is going to add considerably to the delay between steps.

Another interesting thing is the LCD (A3) reading fluctuates with joystick movement.

What is connected to A3?

adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sat Apr 17, 2021 4:18 pm

At the moment nothing at all is connected to A3 because the stepper isn't working. with nothing at all connected to A3 if I move the joystick the LCD values change.

Side note if I totally remove the LCD ( all the wires) the stepper still doesn't work ? So something is funky with the sketch that Im missing

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sat Apr 17, 2021 8:28 pm

At the moment nothing at all is connected to A3

A floating input is unpredictable. It is not unusual for them to be influenced by voltages on nearby pins.

Side note if I totally remove the LCD ( all the wires) the stepper still doesn't work ?

Unless you remove the LCD code, it is still attempting to write to the display.

adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sat Apr 17, 2021 10:20 pm

Ok, so is there a way to modify the code to speed up the stepper ? I can live with the result if the stepper speed could be sped up a little

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sun Apr 18, 2021 6:32 am

You can reduce your update rate for the display so you are not trying to update between each step:

Code: Select all | TOGGLE FULL SIZE
long lastUpdate = 0;
 
void loop()
{
   long now = millis();
   if (now - lastUpdate > 1000) // update display every second
   {
      int sensorValue = analogRead(A3);
      
      sensorValue = map(sensorValue, 0, 1023, 0, 150);
      lcd.setCursor(0,1);
      lcd.print("Actual");
      lcd.setCursor(8,1);
      lcd.print(sensorValue);
      
      lastUpdate = now;
   }


adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sun Apr 18, 2021 10:13 am

This is baffling, before the code change you suggested the stepper crept, with the changes it doesn't move at all.
I know it is the LCD code which is hindering the operation just don't get why.... Maybe it just is what it is ?

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sun Apr 18, 2021 10:17 am

Please post your current code with the changes added.

adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sun Apr 18, 2021 11:33 am

Code: Select all | TOGGLE FULL SIZE
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

Adafruit_LiquidCrystal lcd(6, 5, 7);//Or Data pin is #6, Clock is #5 and Latch is #7

#define step_pin 3   // Pin 3 connected to Steps pin on Driver
#define dir_pin 2    // Pin 2 connected to Direction pin on Driver

#define X_pin A0      // Pin A0 connected to joystick x axis
#define Joy_switch 4  // Pin 4 connected to joystick switch

 int step_speed = 10;  // Speed of Stepper motor (higher = slower)
 

void setup() {
 
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
 
   pinMode(Joy_switch, INPUT_PULLUP);
   
    // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Target  136");
   
   
}
 
void loop() {
 
  int sensorValue = analogRead(A3);
 
 
  sensorValue = map(sensorValue, 0, 1023, 0, 150);
  lcd.setCursor(0,1);
  lcd.print("Actual");
  lcd.setCursor(8,1);
  lcd.print(sensorValue);
  Serial.println(sensorValue);
  lcd.setBacklight(HIGH);
 
 
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10:
        step_speed=3;  // medium speed
        break;
               
    }
   
   
 if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
   
   
       {  //  if limit switch is not activated, move motor clockwise
     
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed); 
    }     
  }
 
   if (analogRead(X_pin) < 312) {  // If joystick is moved right
   
   
   
      { 
     
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }     
   }   }  }

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sun Apr 18, 2021 11:42 am

That looks the same as your original code. I don't see where you added the changes.

adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sun Apr 18, 2021 1:09 pm

Code: Select all | TOGGLE FULL SIZE
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

Adafruit_LiquidCrystal lcd(6, 5, 7);//Or Data pin is #6, Clock is #5 and Latch is #7

#define step_pin 3   // Pin 3 connected to Steps pin on Driver
#define dir_pin 2    // Pin 2 connected to Direction pin on Driver

#define X_pin A0      // Pin A0 connected to joystick x axis
#define Joy_switch 4  // Pin 4 connected to joystick switch

 int step_speed = 10;  // Speed of Stepper motor (higher = slower)
 

void setup() {
 
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
 
   pinMode(Joy_switch, INPUT_PULLUP);
   
    // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Target  136");
   
   
}

 long lastUpdate = 0;
void loop() {

  long now = millis();
  if (now - lastUpdate > 1000)
 
 
  int sensorValue = analogRead(A3);
 
 
int  sensorValue = map(sensorValue, 0, 1023, 0, 150);
  lcd.setCursor(0,1);
  lcd.print("Actual");
  lcd.setCursor(8,1);
  lcd.print(sensorValue);
  Serial.println(sensorValue);
  lcd.setBacklight(HIGH);
 
 
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10:
        step_speed=3;  // medium speed
        break;
               
    }
   
   
 if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
   
   
       {  //  if limit switch is not activated, move motor clockwise
     
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed); 
    }     
  }
 
   if (analogRead(X_pin) < 312) {  // If joystick is moved right
   
   
   
      { 
     
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }     
   }   }  }

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sun Apr 18, 2021 1:33 pm

That is not the change I suggested. As you have it there, the scope of the 'if' only covers the analog read. So all your LCD code will still get executed between each step - including the redundant backlight command.

https://www.tutorialspoint.com/cplusplu ... tement.htm

adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sun Apr 18, 2021 2:40 pm

Your right, I see I missed a line.
Here is what I believe you corrected
Code: Select all | TOGGLE FULL SIZE
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

Adafruit_LiquidCrystal lcd(6, 5, 7);//Or Data pin is #6, Clock is #5 and Latch is #7

#define step_pin 3   // Pin 3 connected to Steps pin on Driver
#define dir_pin 2    // Pin 2 connected to Direction pin on Driver

#define X_pin A0      // Pin A0 connected to joystick x axis
#define Joy_switch 4  // Pin 4 connected to joystick switch

 int step_speed = 10;  // Speed of Stepper motor (higher = slower)
 

void setup() {
 
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
 
   pinMode(Joy_switch, INPUT_PULLUP);
   
    // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Target  136");
   
   
}

 long lastUpdate = 0;
void loop() {

  long now = millis();
  if (now - lastUpdate > 1000)
  {
 
  int sensorValue = analogRead(A3);
 
 
  sensorValue = map(sensorValue, 0, 1023, 0, 150);
  lcd.setCursor(0,1);
  lcd.print("Actual");
  lcd.setCursor(8,1);
  lcd.print(sensorValue);
  //Serial.println(sensorValue);
  //lcd.setBacklight(HIGH);
  lastUpdate = now;
  }
 
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10: 
        step_speed=3;  // medium speed
        break;
               
    }
   
   
 if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
   
   
       {  //  if limit switch is not activated, move motor clockwise
     
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed); 
    }     
  }
 
   if (analogRead(X_pin) < 312) {  // If joystick is moved right
   
   
   
      { 
     
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }     
   }   }  }

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Re: Stepper Project not working

by adafruit_support_bill on Sun Apr 18, 2021 3:55 pm

That looks better. If you want to make sure that the backlight is on, you can make that call in your setup. You only need to do it once.

So, with the changes, you should see the LCD update once per second. And your steppers should run better - except a slight hiccup every second as you update the display.

adafruit_support_bill
 
Posts: 81001
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Project not working

by sadams260 on Sun Apr 18, 2021 8:55 pm

Stepper doesn't move at all

sadams260
 
Posts: 33
Joined: Thu Jun 23, 2016 9:04 pm

Please be positive and constructive with your questions and comments.