Go Down

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

Trigger80

Thanks for helping me out, so close to get it working:)

Code: [Select]
#include <Stepper.h>


#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 12     // Define pin 5 as "MS1"
#define MS2 13       // 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 0
#define echoPin 1

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
   
 
 
   digitalWrite(MS1, LOW);      // Configures the steps division (see above)
   digitalWrite(MS2, LOW);    // Configures the steps division (see above)
   
 
}

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, HIGH);
    delay(1);
    digitalWrite(step_pin, LOW);
    delay(1);
    steps--;
}

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;   
}

wg0z

The best way to check button_push for these  DFROBOT shields is to call analogRead(A0)and range check the return value. never check for a single exact value,  the divider resistances can vary board to board and with board age


try something _LIKE_ this, youll need to experiment to find out the rough value the function returns
for a given button



result = analogRead(A0);

if (result >= 250)
  ; // no button is being pushed
else if (result >= 200)
 UP;
else if ( result >= 170)
  DOWN;
else if ( result >= 140);
  LEFT;
else if (result >= 110);
  RIGHT;
else
  SELECT;

J-M-L

The best way to check button_push for these  DFROBOT shields is to call analogRead(A0)and range check the return value. never check for a single exact value,  the divider resistances can vary board to board and with board age


try something _LIKE_ this, youll need to experiment to find out the rough value the function returns
for a given button



result = analogRead(A0);

if (result >= 250)
  ; // no button is being pushed
else if (result >= 200)
 UP;
else if ( result >= 170)
  DOWN;
else if ( result >= 140);
  LEFT;
else if (result >= 110);
  RIGHT;
else
  SELECT;
that's what he has with

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

The problem now is the the stepper just takes smal stepps realy realy slow, how can i speed up the stepper, if i press the buttons i can feel the stepper moving but icant realy see it move, that is slow:

wg0z

so  what happens if  adc_key_in is in  range 791...999?

Trigger80

i use the bntLeft and bntRight, i did change it now to 800 and 791, now the stepper goes the same way on the buttons, if i change back it goes difrent drections

J-M-L

#36
Sep 11, 2016, 05:51 pm Last Edit: Sep 11, 2016, 05:53 pm by J-M-L
Code: [Select]
#define trigPin 0
#define echoPin 1


that's a bad idea. this is your serial communication port. it means you can't use Serial.print to debug.

Use other pin. move them somewhere else. You can use Analog Pins as Digital pins. move MS1 to A1, move MS2 to A2 and have trigPin to 11 and echoPin to 12

This is slow because you asked it to be slow:

you check the distance at every loop. distance checking takes some time esp when there is nothing in front of your sensor. worse you add a 250ms delay after each display to the LCD which you do at every loop --> get rid of the 250ms wait and use the standard technique for doing several things at the same time to only update the LCD every second for example.

So while you do all this stuff your motors are off. that's because you've programmed this so that when the button is pressed, you move for 1 ms and then stop.

so basically, lets say your sensor measuring takes 10ms, your code today does this:

spend 10ms checking the distance
update LCD (will take a few ms too)
wait 1/4 of second
check if button is pressed and if so move the motor for 1ms and stop

so if you keep the button pressed, you get 1ms worth of motor action for ~270ms worth of doing other thing... no wonder it's slow right?

what you need to do is not stop the motor. if button is pressed, get the motor going as you want. then if no button is pressed next time you come back, then stop the motor. this way the motor keeps spinning all the time while you do the distance ranging and LCD update


Last (but not least) - WHEN ALL THE ABOVE IS WORKING - you will find  out that distance ranging that way is pretty slow overall esp. when there is nothing in front your robot. I recommend using the NewPing Library for Arduino as it lets you do smart things when sensing distance.







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 for the input and the good need to know basic!!

Im gonna use the stepper to move an axis that the sensor is sitting on, so i move the sensor and messure
the distance that way.


So if i just can make the stepper move faster it will be all i need.

I will try this and see how it works out.

Thanks

Trigger80

I have now updated the pin´s and deleted the delay.

The sensor works good, but the stepper is the same, better if i dont use the delay but then the lcd looks
wird.

The problem is that i need to press button to get the stepper moving, then relese to make it stop, bercause i need to stop the sensor at prisice messurment and there will always be a "plate" infront of the sensor., dont know any outher way to make it work??

Can i make this work at all?
Do i need another board?


Thanks

J-M-L

Have you implémented the suggested update of LCD only once per second and not at every loop?
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

As im new to this i dont know how to do that:(

Can you give me some exemple in the code?

J-M-L

I gave you the link above --> see technique for doing several things at the same time

If you don't invest personal effort to learn, I'm stopping here. Goal is for you to become self sufficient, not for other to write your code...
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


Trigger80

I wanna thank everyone for the help and input !!

I made some changes and got it to work, not perfekt but good for a first prodjekt.


Go Up