Dear everyone, i need help in the programming of stepper motor with an IC of L298.
i already connect the IC L298 on my breadboard and try the coding of one revolution clockwise and
anticlockwise. I adjust the coding to my motor specification.
#include <Stepper.h>
const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(80);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
I did try to implement switch so that when i press the switch it can rotate in a position , here is the coding
#include <Stepper.h>
const int stepsPerRevolution = 0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11); // for your motor
// set pin numbers:
const int buttonPin = 2;
const int buttonPin1 = 3;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState1 = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(80);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
const int stepsPerRevolution = 400;
// step one revolution in one direction:
myStepper.step(stepsPerRevolution);
delay(500);
}
else{
if (buttonState1 == HIGH){
// step one revolution in the other direction:
const int stepsPerRevolution = 400;
myStepper.step(-stepsPerRevolution);
delay(500);
}
}
}
Hope to hear a positive response