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);}
}
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.
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?
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?
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?
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.