Go Down

Topic: Arudino Lcd Shield And Stepper Help (Read 1 time) previous topic - next topic

Trigger80

Sep 11, 2016, 01:08 pm Last Edit: Sep 11, 2016, 01:14 pm by Trigger80
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":) 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

Code: [Select]
#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;   
}

J-M-L

#1
Sep 11, 2016, 01:30 pm Last Edit: Sep 11, 2016, 01:35 pm by 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??
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Trigger80

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


the buttons are analog (0), ?


OldSteve

#3
Sep 11, 2016, 01:36 pm Last Edit: Sep 11, 2016, 01:41 pm by OldSteve
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:-
Code: [Select]
if (analogRead(btnUP) == LOW || analogRead(btnDOWN) == LOW) { // ako ni jedan taster nije pritisnut
  digitalWrite(step_pin, LOW);  // ? ? ?
  digitalWrite(step_pin, LOW);  // ? ? ?
   }

This:-
Code: [Select]
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

Code: [Select]
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.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

J-M-L

#4
Sep 11, 2016, 01:37 pm Last Edit: Sep 11, 2016, 01:40 pm by J-M-L
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?
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

J-M-L

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

OldSteve

Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

J-M-L

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Trigger80

#8
Sep 11, 2016, 02:05 pm Last Edit: Sep 11, 2016, 02:10 pm by Trigger80
Thanks for the replys,

do i getting close?:)

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?

Trigger80

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?

OldSteve

Thanks for the replys,

do i getting close?:)

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:-
Code: [Select]
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.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

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"



Code: [Select]
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);
}

J-M-L

good - glad this is making progress!

happy coding
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Trigger80

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

is it not the same code?


OldSteve

Now its starts to move:)

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


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



Code: [Select]
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:-
Code: [Select]
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);
}
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Go Up