Hi everyone,
I'm having great difficulty in wrapping my head around how to set up "my" (bits and pieces stolen from other projects) code (pasted below).
I am wanting to have several buttons that turn the stepper motor a set number of turns.
e.g. Button 1 turns the motor 120 (24000 steps) times. Button 2 turns the motor 240 times (48000 steps.
I am wanting the coil winder to behave like this:
- Do nothing until button press.
- On Button (x) do number of turns.
- Wait until further button press.
Currently I only one button seems to work.
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Stepper.h>
/* Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO: number of steps/revolutions. More info: https://www.makerguides.com */
// Include the AccelStepper library:
// Motor pin definitions:
#define motorPin1 8 // IN1 on the ULN2003 driver
#define motorPin2 9 // IN2 on the ULN2003 driver
#define motorPin3 10 // IN3 on the ULN2003 driver
#define motorPin4 11 // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 4
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, 8, 9, 10, 11);
int Button = 12;
int ButtonA = 13;
int Motor = (8,9,10,11);
int Val = 0;
void setup() {
// Set the maximum steps per second:
stepper.setMaxSpeed(2400);
pinMode (Button, INPUT_PULLUP);
pinMode (ButtonA, INPUT_PULLUP);
pinMode (Motor, OUTPUT);
}
void loop() {
Val = digitalRead(Button); // Setting the Val variable to the output of the
// button, which can be either HIGH or LOW
if (Val == LOW) { // Statement to determine the state of the button
digitalWrite(Motor, HIGH); // If the button is pressed, the buzzer will sound
} else {
digitalWrite(Motor, LOW);
// Set the current position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 500 steps/second until the motor reaches 2400 steps (12 revolutions):
while (stepper.currentPosition() != 2400) {
stepper.setSpeed(200);
stepper.runSpeed();
}
delay(1000);
// Reset the position to 0:
stepper.setCurrentPosition(0);
}
Val = digitalRead(ButtonA); // Setting the Val variable to the output of the
// button, which can be either HIGH or LOW
if (Val == LOW) { // Statement to determine the state of the button
digitalWrite(Motor, HIGH); // If the button is pressed, the buzzer will sound
} else {
digitalWrite(Motor, LOW);
// Set the current position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 500 steps/second until the motor reaches 48000 steps (240 revolutions):
while (stepper.currentPosition() != 48000) {
stepper.setSpeed(200);
stepper.runSpeed();
}
delay(1000);
// Reset the position to 0:
stepper.setCurrentPosition(0);
}
delay(1000);
}
Any help would be greatly appreciated