Go Down

Topic: Easydriver button control (Read 736 times) previous topic - next topic

zeus2kx

Hi Guys,
I was following Example 5 on this page.

Everything is working fine.
Now I want to change a little bit the code so that the motor would rotate only when the button is pressed and held.
In the posted example, when the button is pressed once, the motor starts rotating.

Code: [Select]
  if (digitalRead(LEFT_PIN) == 0) {
    sign = -1;
  }
  else {   
    sign = 0;
  }
  if (digitalRead(RIGHT_PIN) == 0) {
    sign = 1;
  }
  else {   
    sign = 0;
  }
  if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }


Right button is working the way I wanted but for some reason, left button press does nothing.
What is causing this problem?
 
I will be thankful for your help.

Robin2

You need to post the complete program.

Is the code snippet the original code, or your attempt at changing it?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

zeus2kx

You need to post the complete program.

Is the code snippet the original code, or your attempt at changing it?

...R
Yes original, I just changed above posted lines..
Like I mentioned earlier, original code works just fine..

zeus2kx

I will paste the modified code:

Code: [Select]
#include <AccelStepper.h>
int Stepping = false;
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 2, 3);

// Define our three input button pins
#define  LEFT_PIN  9
#define  STOP_PIN  11
#define  RIGHT_PIN 10

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 5000
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);
 
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = -1;
  }
  else {   
    sign = 0;
  }
  if (digitalRead(RIGHT_PIN) == 0) {
    sign = 1;
  }
  else {   
    sign = 0;
  }
  if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop). 
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}

zeus2kx

Any help will be highly appreciated.
Thanks

Robin2

Yes original, I just changed above posted lines..
Like I mentioned earlier, original code works just fine..
Please post the original code so we can see how you have changed it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

zeus2kx

#6
Jan 17, 2018, 09:42 pm Last Edit: Jan 17, 2018, 09:46 pm by zeus2kx
Original which works:

Code: [Select]
#include <AccelStepper.h>
int Stepping = false;
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 2, 3);

// Define our three input button pins
#define  LEFT_PIN  9
#define  STOP_PIN  11
#define  RIGHT_PIN 10

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 5000
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);
 
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  digitalWrite(LEFT_PIN, HIGH);
  digitalWrite(STOP_PIN, HIGH);
  digitalWrite(RIGHT_PIN, HIGH);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
 if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {   
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop). 
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}


I think I have problems with if and else function.. but can't figure out.

Only this snippet is different:
Code: [Select]
  if (digitalRead(LEFT_PIN) == 0) {
    sign = -1;
  }
  else {   
    sign = 0;
  }
  if (digitalRead(RIGHT_PIN) == 0) {
    sign = 1;
  }
  else {   
    sign = 0;
  }
  if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

instead of
Code: [Select]
if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {   
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }


Robin2

#7
Jan 17, 2018, 10:18 pm Last Edit: Jan 17, 2018, 10:22 pm by Robin2
Right button is working the way I wanted but for some reason, left button press does nothing.
What is causing this problem?
That is because whatever setting is detected for the left button is immediately over written by the right button.

Try this
Code: [Select]
leftPinState = digitalRead(LEFT_PIN);
rightPinState = digitalRead(RIGHT_PIN);
stopPinState = digitalRead(STOP_PIN);

sign = 0;
if (stopPinState) != 0) {
    if (leftPinState == 0) {
        sign = -1;
    }
    if (rightPinState == 0) {
        sign = 1;
    }
}


Note that saving the pin values into variables allows you to print the values for debugging purposes.

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

zeus2kx

#8
Jan 17, 2018, 10:28 pm Last Edit: Jan 17, 2018, 10:40 pm by zeus2kx
Great, thank you.
Working as I expected.

Best,
Z

Go Up