Stepper w 2 buttons pushed and potentiometer. Got stuck

Hi.
I got stuck and need your help. I am using a bipolar nema17 stepper with DM542 controller.
I would like to use 2 push buttons, 2 LED and a potentiometer to control the NEMA motor.
Button1 if pushed should move the stepper CW, Button2 if pushed should move CCW. If Button1 is pushed LED1 should be on, and LED2 on when Button2 is pushed. (I didn't work on the LED part in my code below yet).
Potentiometer should be just to adjust the speed (rpm)
I am a beginner and got stuck. I would appreciate any help.
Thank you.

// Defin pins
 
int button_left = 2;  // Push button for CW
int button_right = 3;  // Push button for CCW
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer
 
// Variables
 
int pd = 500;       // Pulse Delay period
boolean setdir = HIGH; // Set Direction
 
// Interrupt Handler
 
void revmotor (){
 
  setdir = !setdir;
  
}
 
void setup() {
  Serial.begin(9600);
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  pinMode (button_left, INPUT);
  attachInterrupt(digitalPinToInterrupt(button_left), revmotor, FALLING);
  
}
 
void loop() {
 int pressed = digitalRead (button_left);
 if (pressed == HIGH)
   { pd = map((analogRead(spd)),0,1023,2000,50);
      
    Serial.println("Button left");
    digitalWrite(driverDIR,LOW);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
    digitalWrite(driverDIR,HIGH);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);}
 
}

is present 3 times in this sketch and doing nothing.
BTW it seems the sketch is AI generated, dump it.

Thanks for getting back. I did remove it, but still doesn't work.

I made your project in Wokwi simulation:

But I really don't know what to do with the code.
If you start with the two buttons and the two leds and the 'setdir' variable, can you make that work without using interrupts ?

The "boolean" is the name that Arduino made up. The 'C' language has "bool".
It can be true or false, not HIGH.

bool setdir = true;

Hi, @hageralf79

Can you please post a copy of your circuit a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What are you using for a power supply?
What model Arduino are you using?

Thanks... Tom... :grinning: :+1: :coffee: :australia:

of course not. if you payed for that code then refund it.

it is actually simple, look

const int button_left = 2;  // Push button for CW
const int button_right = 3;  // Push button for CCW
const int driverPUL = 7;    // PUL- pin
const int driverDIR = 6;    // DIR- pin
const int spd = A0;     // Potentiometer

int pd = 500;       // Pulse Delay period

void setup() {
  Serial.begin(115200);
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  pinMode (button_left, INPUT_PULLUP);
  pinMode (button_right, INPUT_PULLUP);
}

void loop() {
  pd = 2090 - analogRead(spd) * 2;

  if (digitalRead(button_left) == HIGH && digitalRead(button_right) == LOW) digitalWrite(driverDIR, LOW);
  if (digitalRead(button_left) == LOW && digitalRead(button_right) == HIGH) digitalWrite(driverDIR, HIGH);

  digitalWrite(driverPUL, LOW);
  delayMicroseconds(50);
  digitalWrite(driverPUL, HIGH);
  delayMicroseconds(pd);
}

Kolaha, it works, thanks. However the stepper always rotates. I would like to disable the stepper when no buttons are pressed.
How to do that?

Thanks

What should happen if both buttons are pressed ?

Technically that can't happened because I am using a 3way rocket switch which is always off. If I press and release it goes back to off

const int button_left = 2;  // Push button for CW
const int button_right = 3;  // Push button for CCW
const int driverPUL = 7;    // PUL- pin
const int driverDIR = 6;    // DIR- pin
const int spd = A0;     // Potentiometer

int pd = 500;       // Pulse Delay period

void setup() {
  Serial.begin(115200);
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  pinMode (button_left, INPUT_PULLUP);
  pinMode (button_right, INPUT_PULLUP);
}

void loop() {
  pd = 2090 - analogRead(spd) * 2;

  if ( digitalRead(button_right) && digitalRead(button_left) ) return;

  if ( !digitalRead(button_right) ) digitalWrite(driverDIR, LOW);
  if ( !digitalRead(button_left) ) digitalWrite(driverDIR, HIGH);

  digitalWrite(driverPUL, LOW);
  delayMicroseconds(50);
  digitalWrite(driverPUL, HIGH);
  delayMicroseconds(pd);
}

Kolaha, thank you. Works as intended, Awesome.
Where do I put the codes for LED when button1 is pressed LED1 is on, and when Button2 is pressed LED2 is on?

Thanks again

anywhere

digitalWrite(LED1_pin, !digitalRead(button_right));
digitalWrite(LED2_pin, !digitalRead(button_left));

Thank you. I will test it Monday.

Appreciate your help

Kolaha, sorry took longer than expected.
Can you help with 2 more questions please?
1.
All is working, however LED stays on after I release the switch. The motor stops after the switch is released which is good. What code do I need here to turn off the LED once the button is not engaged anymore?

I also combined the code with the previous one I made using the ultrasonic sensor to measure distance. This is separate from the motor function but connecting it to the same Arduino mega board.
When I run the 2 codes separately both are working. But if I combine them the ultrasonic sensor and the display are working as well as the switch with the LEDs but the motor won't move.
Where is the problem?

See the picture below as well as the code

https://wokwi.com/projects/375819414502503425

//lcd
#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 

const int trigPin = 9;
const int echoPin = 10;  
long duration;
int distanceCm, distanceInch, distanceMm;

// motor
const int button_left = 52;  // Push button for CW
const int button_right = 53;  // Push button for CCW
const int driverPUL = 30;    // PUL- pin
const int driverDIR = 32;    // DIR- pin
const int spd = A0;     // Potentiometer
const int leftLED = 12;
const int rightLED = 11;

int pd = 500;       // Pulse Delay period

void setup() {
  Serial.begin(115200);
  //lcd
  lcd.begin(16,2); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


  //motor
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  pinMode (button_left, INPUT_PULLUP);
  pinMode (button_right, INPUT_PULLUP);
  
}

void loop() {
  pd = 2090 - analogRead(spd) * 2;

//lcd
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);

   duration = pulseIn(echoPin, HIGH);
   distanceCm= duration*0.034/2;
   distanceInch = duration*0.0133/2;
   distanceMm = duration*0.34/2;
   Serial.println(distanceMm);
   lcd.setCursor(0,0);
   lcd.print("Distance: "); 
   lcd.print(distanceMm);
   lcd.print(" mm ");
   delay(300);
   lcd.setCursor(0,1);
   lcd.print("Distance: ");
   lcd.print(distanceInch);
   lcd.print("inch");
   delay(10);
   
//motor
  if ( digitalRead(button_right) && digitalRead(button_left) ) return;

  if ( !digitalRead(button_right) ) digitalWrite(driverDIR, LOW);
    
  if ( !digitalRead(button_left) ) digitalWrite(driverDIR, HIGH);
  
  digitalWrite(rightLED, !digitalRead(button_right));
  digitalWrite(leftLED, !digitalRead(button_left));
  
  digitalWrite(driverPUL, LOW);
  delayMicroseconds(50);
  digitalWrite(driverPUL, HIGH);
  delayMicroseconds(pd);
}

This is a real trap, und I would never do so. You exit loop(), and none of the following statements is executed anymore if both buttons are released. So also these statements:

are not executed anymore. And that's why your led isn't switched off.

Always let loop() run to the end, and create if{ blocks} if some statements ( as creating motor pulses ) shouldn't be executed.

1 Like

The screendump of the circuit in Wokwi does not match with the code.
Did you copy the circuit from someone else and then added your own code ?

If you make the circuit diagram look better, then you make it easier for yourself.
For example:

1 Like

i said "anywhere" and it was not right, it should stay before this:

@hageralf79 mark best(most helpful) answer as solution and open other topic to other issue

1 Like
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int button_left = 52;  // Push button for CW
const int button_right = 53;  // Push button for CCW
const int driverPUL = 30;    // PUL- pin
const int driverDIR = 32;    // DIR- pin
const int spd = A0;     // Potentiometer
const int leftLED = 12;
const int rightLED = 11;
const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  pinMode (button_left, INPUT_PULLUP);
  pinMode (button_right, INPUT_PULLUP);
}

void loop() {
  static uint32_t duration = 0;
  static int distanceCm, distanceInch, distanceMm,  pd;
  static uint32_t timerMills = 0;
  const byte measDealay = 100;
  if (millis() - timerMills >= measDealay) {
    timerMills += measDealay;

    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distanceCm = duration / 58.29;
    distanceInch = duration / 22.77;
    distanceMm = duration / 5.8292;
    Serial.println(distanceMm);
    lcd.setCursor(0, 0);
    lcd.print("Distance: ");
    lcd.print(distanceMm);
    lcd.print(" mm ");

    lcd.setCursor(0, 1);
    lcd.print("Distance: ");
    lcd.print(distanceInch);
    lcd.print("inch");
  } 


  digitalWrite(rightLED, !digitalRead(button_right));
  digitalWrite(leftLED, !digitalRead(button_left));
  pd = 2090 - analogRead(spd) * 2;

  if ( digitalRead(button_right) != digitalRead(button_left) ) {
    digitalWrite(driverDIR, digitalRead(button_right));

    digitalWrite(driverPUL, LOW);
    delayMicroseconds(50);
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
  }
}
2 Likes

Perfect. Thank you so much kolaha