I've work hard on my project and now it's fonctionnel. The carriage is running 2 times back and forth. When the carriage is moving, a infrared lamp is lighting. Instead of coding that way, I would like to control it with 2 limit switch. One at each extremity. I'm using a stepper 12 volts nema 17 motor, a L298N driver (VMA409) and a relay module to control the infrared light.
Here's my code
Thank you for the help
#include <Stepper.h>
#define button1 2
#define button2 3
//int enA = 4; // Motor energy control
//int enB = 5;
const int STEPS_PER_REV = 1300; // change this to fit the number of steps per revolution
// for 1.8 degree step angle 200 will maximum
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(STEPS_PER_REV, 8,9,10,11); // L298n port IA IB IC ID accordingly
void setup(){
pinMode(4, OUTPUT); // Motor energy
pinMode(5, OUTPUT); // Motor energy
pinMode(6, OUTPUT); // Lamp relay control
pinMode(button1, INPUT_PULLUP); // Input pulse to arduino by pressing button 1
pinMode(button2, INPUT_PULLUP); // Input pulse to arduino by pressing button 2
myStepper.setSpeed(5); // RPM, 60 Rotation per minute
}
void loop() {
if (digitalRead(button1) == LOW || digitalRead(button2) == LOW) { // If no key is pressed
if (digitalRead(button1) == LOW && digitalRead(button2) == HIGH) { // if one is idle (L) and the other one (R) key pressed
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
myStepper.step(STEPS_PER_REV ); // Stepper motor rotates CW (Clockwise)
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(500); // pause time after per steps 0,5 second
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
myStepper.step(-STEPS_PER_REV ); // Stepper motor rotates CCW (Anticlockwise)
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(500); // pause time after per steps 0,5 second
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
myStepper.step(STEPS_PER_REV ); // Stepper motor rotates CW (Clockwise)
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(500); // pause time after per steps 0,5 second
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
myStepper.step(-STEPS_PER_REV ); // Stepper motor rotates CCW (Anticlockwise)
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(500); // pause time after per steps 0,5 second
} } }