Arudino Lcd Shield And Stepper Help

Hi.

Noob needs some help:(

Im trying to control a stepper whit the arudino lcd shield (6 button)

I whant the stepper to move "clockvise"when i hold "up" button down, and stop if i relese the button.
Same for "down" button but move "counterclockvise"

As im i noob at this im trying to read diffrent tutorials and copy codes in and out to make it work.

Now im stucked:(

Can someone look at this "code":slight_smile: as you can see i realy dont know mutch about this.

Parts:
Arudino Lcd shield
Arudino Uno
Nema 17 stepper
Easy Motor

ps: i have try the stepper and it works but not in the way i whant whit another code i got online, so wire is good.

Thanks

/David

#define step_pin 3    // Define pin 3 as the steps pin
#define dir_pin 2    // Define pin 2 as the direction pin
#define MS1 5     // Define pin 5 as "MS1"
#define MS2 4       // Define pin 4 as "MS2"
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int button;
int direction;    // Variable to determine the sense of the motor
int steps = 500;      // Number of steps that you want to execute (for full steps, 200 = 1 turn)

void setup() {
   pinMode(MS1, OUTPUT);     // Configures "MS1" as output
   pinMode(MS2, OUTPUT);     // Configures "MS2" as output
   pinMode(dir_pin, OUTPUT);    // Configures "dir_pin" as output
   pinMode(step_pin, OUTPUT);    // Configures "step_pin" as output
   
   pinMode(btnUP, INPUT_PULLUP);
   pinMode(btnDOWN, INPUT_PULLUP);
   digitalWrite(MS1, LOW);      // Configures the steps division (see above)
   digitalWrite(MS2, LOW);    // Configures the steps division (see above)
   digitalWrite(dir_pin, LOW);   // Sense (HIGH = anti-clockwise / LOW = clockwise) - It can be also changed
   
}

void loop() { 

   if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) { // ako ni jedan taster nije pritisnut
  digitalWrite(step_pin, LOW);
  digitalWrite(step_pin, LOW);
   }
  
  
 if (analogRead(btnUP) == LOW && analogRead(btnDOWN) == HIGH) { // ako je taster 1 idle, a taster 2 pritisnut
    digitalWrite(dir_pin, LOW); // move in the LOW direction
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
    
  } else if (analogRead(btnUP) == HIGH && (btnDOWN) == LOW) { // ako je taster 2 idle, a taster 1 pritisnut
    digitalWrite(dir_pin, HIGH); // move in HIGH direction
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
  }
  
}  
int read_buttons() {
  
 int adc_key_in = analogRead(0);
 
 if (adc_key_in > 1000) return btnNONE;
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
}

is that the one?

not read all but this feels weird

   if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) { // ako ni jedan taster nije pritisnut
  digitalWrite([color=red]step_pin[/color], LOW);
  digitalWrite([color=red]step_pin[/color], LOW);
   }

and what about all the analogRead? you have a read_buttons functions - what is that for??

if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) {
digitalWrite(step_pin, LOW);
digitalWrite(step_pin, LOW);

the buttons are analog (0), ?

Besides what J_M_L just said, you need to use your defined 'read_buttons()' function, not 'analogRead()' with a comparison to 'HIGH' or 'LOW'.

ie
Instead of this:-

if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) { // ako ni jedan taster nije pritisnut
  digitalWrite(step_pin, LOW);  // ? ? ?
  digitalWrite(step_pin, LOW);  // ? ? ?
   }

This:-

int buttonVal = read_buttons();
if(buttonVal == btnUP || buttonVal == btnDOWN)    // ako ni jedan taster nije pritisnut
{
    // Do something here, not what you were doing before.
}

etc

digitalWrite(step_pin, LOW);
  digitalWrite(step_pin, LOW);

Do you really mean to write 'step_pin' LOW twice in a row, or did you have something else in mind?
Edit: As I assumed J_M_L was referring to.

Trigger80:
if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) {
digitalWrite(step_pin, LOW);
digitalWrite(step_pin, LOW);

the buttons are analog (0), ?

OK so the function is useless?? (I added a question about the screen - if this is the one, then indeed buttons go to Analog input 0 probably showing with different resistor values which one is selected)

you still have twice step_pin though in that code. probably want one of those to be dir_pin?

OldSteve:
int buttonVal = read_buttons;

missing a pair of () most likely :slight_smile:

J-M-L:
missing a pair of () most likely :slight_smile:

Oops. Not any more. :smiley:

:slight_smile:

Thanks for the replys,

do i getting close?:slight_smile:

void loop() {

int buttonVal = read_buttons();
if(buttonVal == btnUP || buttonVal == btnDOWN)
{
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, LOW);
delay(3);
digitalWrite(step_pin, HIGH);
delay(3);
}

Is this the only loop i need to turn the stepper 2 ways?

J-M-L:
is that the one?

not read all but this feels weird

   if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) { // ako ni jedan taster nije pritisnut

digitalWrite(step_pin, LOW);
  digitalWrite(step_pin, LOW);
  }




and what about all the analogRead? you have a read_buttons functions - what is that for??

yes its that lcd, i now added the code, looks more right?

Trigger80:
Thanks for the replys,

do i getting close?:slight_smile:

void loop() {

int buttonVal = read_buttons();
if(buttonVal == btnUP || buttonVal == btnDOWN)
{
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, LOW);
delay(3);
digitalWrite(step_pin, HIGH);
delay(3);
}

Is this the only loop i need to turn the stepper 2 ways?

No, this won't do what you describe in your opening post. (I just went back and re-read what you want to do.)
You're taking dir_pin high, regardless of whether 'Up' or 'Down' is pressed, so the motor will move in the same direction both times.

Also, and I might be wrong, are you supposed to pulse the step pin low to move a step, or should you pulse it high? (I don't know what you're using for a driver, or how it works. I've only done a tiny bit of stepper control, using an L298N-based module, so can't help in that respect.)

Either way, you want separate conditionals for each of those buttons.
Ignoring the pulse polarity for the moment, you basically want:-

int buttonVal = read_buttons();
if(buttonVal == btnUP)
{
    digitalWrite(dir_pin, HIGH); 
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

if(buttonVal == btnDOWN)
{
    digitalWrite(dir_pin, LOW); 
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

Note that 'dir_pin' is different for each direction.
N.B. I'll have to assume that your pulse polarity is correct, but if you're not sure, check the documentation for your driver.

And please use code tags when you post code. You started out well, but are slipping now.

Now its starts to move:)

The "btnUP" works, but not the "btnDOWN" ???

I try to make the same code exept for the "LOW,HIGH"

int buttonVal = read_buttons();
    if(buttonVal == btnUP)
{
    digitalWrite(dir_pin, HIGH);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);

  
  
    int buttonVal = read_buttons();
    if(buttonVal == btnDOWN)
{
    digitalWrite(dir_pin, LOW);
    digitalWrite(step_pin, HIGH);
    delay(3);
    digitalWrite(step_pin, LOW);
    delay(3);
}

good - glad this is making progress!

happy coding

Thanks, but if the "bntup" works, why does the "bntDown" dont work?

is it not the same code?

Trigger80:
Now its starts to move:)

The "btnUP" works, but not the "btnDOWN" ???

I try to make the same code exept for the "LOW,HIGH"

int buttonVal = read_buttons();

if(buttonVal == btnUP)
{
   digitalWrite(dir_pin, HIGH);
   digitalWrite(step_pin, LOW);
   delay(3);
   digitalWrite(step_pin, HIGH);
   delay(3);

int buttonVal = read_buttons();
   if(buttonVal == btnDOWN)
{
   digitalWrite(dir_pin, LOW);
   digitalWrite(step_pin, HIGH);
   delay(3);
   digitalWrite(step_pin, LOW);
   delay(3);
}

Well clearly the polarity of the pulse is correct in the "UP" part, but not in the "DOWN" part. The pulse should be the same in both. Only 'dir_pin' should change. You didn't need to change the other two lines.
Also, there's no need to redeclare 'buttonVal', or to call 'read_buttons()' a second time.

Did you try it the way I showed in reply #10?
Like this:-

int buttonVal = read_buttons();
if(buttonVal == btnUP)
{
    digitalWrite(dir_pin, HIGH);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

if(buttonVal == btnDOWN)
{
    digitalWrite(dir_pin, LOW);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

Now it works fine, BIG THANKS!!

As my prodject is gonna be , stepper,lcd shield,and distance messurment, im almost there now.

I have the lcd shield and distance messurment working in one code.

Then now i have the lcd shield and stepper in on code.

After playing around and try to add this together i find somthing strange.

When i add code for the distance messurment the stepper dont work anymore?:frowning:

Whitout distance messurment:

oid loop() { 
  
 
  int buttonVal = read_buttons();
  if(buttonVal == btnLEFT)
{
    digitalWrite(dir_pin, HIGH);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

if(buttonVal == btnRIGHT)
{
    digitalWrite(dir_pin, LOW);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

this works good.

This is whit the distance code, and stops the stepper, "or bottons" so it does not work.

void loop() { 
  
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance =(((duration/2)/29.4)*2) ; 
  
 
  int buttonVal = read_buttons();
  if(buttonVal == btnLEFT)
{
    digitalWrite(dir_pin, HIGH);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

if(buttonVal == btnRIGHT)
{
    digitalWrite(dir_pin, LOW);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

Im so close to make this work as my very first prodject, any ide of whats goes wrong when i add the distance code?

Thanks

what is trigPin and echoPin?

Trigger80:
Now it works fine, BIG THANKS!!

As my prodject is gonna be , stepper,lcd shield,and distance messurment, im almost there now.

I have the lcd shield and distance messurment working in one code.

Then now i have the lcd shield and stepper in on code.

After playing around and try to add this together i find somthing strange.

When i add code for the distance messurment the stepper dont work anymore?:frowning:

Whitout distance messurment:

void loop() { 

int buttonVal = read_buttons();
 if(buttonVal == btnLEFT)
{
   digitalWrite(dir_pin, HIGH);
   digitalWrite(step_pin, LOW);
   delay(3);
   digitalWrite(step_pin, HIGH);
   delay(3);
}

if(buttonVal == btnRIGHT)
{
   digitalWrite(dir_pin, LOW);
   digitalWrite(step_pin, LOW);
   delay(3);
   digitalWrite(step_pin, HIGH);
   delay(3);
}




this works good.

This is whit the distance code, and stops the stepper, "or bottons" so it does not work.



void loop() {
 
 long duration, distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance =(((duration/2)/29.4)*2) ;

int buttonVal = read_buttons();
 if(buttonVal == btnLEFT)
{
   digitalWrite(dir_pin, HIGH);
   digitalWrite(step_pin, LOW);
   delay(3);
   digitalWrite(step_pin, HIGH);
   delay(3);
}

if(buttonVal == btnRIGHT)
{
   digitalWrite(dir_pin, LOW);
   digitalWrite(step_pin, LOW);
   delay(3);
   digitalWrite(step_pin, HIGH);
   delay(3);
}




Im so close to make this work as my very first prodject, any ide of whats goes wrong when i add the distance code?

Thanks

Please post all of your code, as it now stands. Edit: Not just 'loop()' It would be good to see your variable definitions, pin allocations etc.

J-M-L:
what is trigPin and echoPin?

'ping sensor'

:slight_smile:

Being smart OldSteve...

Ok - I mean which pins are they connected to? what are the values of these 2 variables

--> suspecting conflict with LCD pins (and of course would be nice to know they are declared as output and input)

Analog 0 Button (select, up, right, down and left)
Digital 4 DB4
Digital 5 DB5
Digital 6 DB6
Digital 7 DB7
Digital 8 RS (Data or Signal Display Selection)
Digital 9 Enable
Digital 10 Backlit Control

should not mess with those + of course his dir_pin and step_pin, nor D0 and D1 probably for Serial.

Here is the complet code, if i remove the parts that is coding the distance sensor the stepper works again.

#include <LiquidCrystal.h>
LiquidCrystal LCD(8, 9, 4, 5, 6, 7);

#define step_pin 3    // Define pin 3 as the steps pin
#define dir_pin 2    // Define pin 2 as the direction pin
#define MS1 5     // Define pin 5 as "MS1"
#define MS2 4       // Define pin 4 as "MS2"
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
#define trigPin 13
#define echoPin 10

int button;
int direction;    // Variable to determine the sense of the motor
int steps = 500;      // Number of steps that you want to execute (for full steps, 200 = 1 turn)

void setup() {
  
   LCD.begin(16,2);
   LCD.setCursor(0,0);
   LCD.print("    Pall 120");
   
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   
   pinMode(MS1, OUTPUT);     // Configures "MS1" as output
   pinMode(MS2, OUTPUT);     // Configures "MS2" as output
   pinMode(dir_pin, OUTPUT);    // Configures "dir_pin" as output
   pinMode(step_pin, OUTPUT);    // Configures "step_pin" as output
   
   pinMode(btnUP, INPUT_PULLUP);
   pinMode(btnDOWN, INPUT_PULLUP);
   digitalWrite(MS1, LOW);      // Configures the steps division (see above)
   digitalWrite(MS2, LOW);    // Configures the steps division (see above)
   digitalWrite(dir_pin, LOW);   // Sense (HIGH = anti-clockwise / LOW = clockwise) - It can be also changed
   
  
}

void loop() { 
  
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance =(((duration/2)/29.4)*2) ;
  
  LCD.setCursor(0,1);  
  LCD.print("                "); 
  LCD.setCursor(0,1);   
  LCD.print(distance); 
  LCD.print(" cm");  
  delay(250); 
 
  int buttonVal = read_buttons();
  if(buttonVal == btnLEFT)
{
    digitalWrite(dir_pin, HIGH);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}

if(buttonVal == btnRIGHT)
{
    digitalWrite(dir_pin, LOW);
    digitalWrite(step_pin, LOW);
    delay(3);
    digitalWrite(step_pin, HIGH);
    delay(3);
}  
  
  
}  
int read_buttons() {
  
 int adc_key_in = analogRead(0);
 
 if (adc_key_in > 1000) return btnNONE;
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
}