Hey, so before I talk about the problem, my code is supposed for my stepper motor is when the button is clicked (HIGH) the stepper motor should move once and stop. The problem I am having is the stepper motor is moving more than once through the line of codes and is stopping randomly. any tips would help. I also checked my wiring and I belive it is correct. furthermore, I am using a Adafruit Motor Sheild v2.3
/* PLATFORM - one click of the button goes to 0 degrees, the next
click would make it go to 180 degrees and so on and so one
Stepper Motor- pendulum type code */
#include <Wire.h>
#include <VarSpeedServo.h> // custom speed library
#include <Adafruit_MotorShield.h> // library for shield driver
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
// this constant wont change:
// const = the integer will not change
const int servobutton = 2;
// servo button is connected to pin 2
const int servopin1 = 9;
// servo#1 is connected to pin 9
const int servopin2 = 10;
//servo#2 is connected to pin 10
const int stepperbutton = 8;
//stepper button is connected to pin 8
const int stepperled = 13;
//stepperled is connected to pin 13
const int servoled = 11;
//servoled is connected to pin 11
//declaring servo#1 as servo1 and servo #2 as servo2
VarSpeedServo servo1;
VarSpeedServo servo2;
// Variables can/will change:
int buttonPushCounter;
// buttonpushcounter will change depending on the amount of button pushs
int lastButtonState;
//lastbuttonstate will record the last state (HIGH OR LOW)
int stepperval;
// reads the value of the stepper button, if it is high or low
void setup(){
// initialize the button pin as a input:
pinMode(servobutton, INPUT);
//declaring servobutton as input (the arduino will read from this)
pinMode(stepperbutton, INPUT);
//declaring stepperbutton as input (the arduino will read from this)
pinMode(stepperled, OUTPUT);
//declaring stepperled as output ( the arduino will send values to the led)
//initialize the servo motor to servopin 1 and 2
servo1.attach(servopin1);
servo2.attach(servopin2);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(50); // 50 rpm
Serial.begin(9600); // starting serial monitor
}
void loop(){
// servo(); //this will read from void servo
stepperval = digitalRead(stepperbutton); //stepper value will be used to read Stepperbuttons value/state
if (stepperval == 1) {
digitalWrite(stepperled,LOW); //stepperled will turn off
myMotor->step(50, FORWARD, DOUBLE);
delay(5);
myMotor->step(50, BACKWARD, DOUBLE);
delay(5);
myMotor->step(50, BACKWARD, DOUBLE);
delay(5);
myMotor->step(50, FORWARD, DOUBLE);
delay(500);
/* if the button is pressed, the stepper motor will go through a loop of the gear moving back and forward,
* creating a pendulum like effect. Then the motor will be released so it does not keep on going
*/
} else if (stepperval == 0) {
digitalWrite(stepperled, HIGH); //if the button is not pressed, the led will stay on, meaning it is not in use
delay(500); //delay of 500ms
}
}
/*
void servo() { //this code is used for the servo button
//read the pushbutton input pin:
int sbutton = digitalRead(2);
//compare the state of button to its previous state
if (sbutton != lastButtonState) {
// if the state has changed, increment the counter
if (sbutton == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
//delay to avoud bouncing
delay(50);
}
// save the current state as the last state, for next tie through the loop
lastButtonState = sbutton;
//move after every 2 button click
if (buttonPushCounter % 2 == 0) {
digitalWrite(servoled, HIGH);
//after the first button click, the servo motor will move to 180 degrees
servo1.slowmove(180,10);
servo2.slowmove(180,10);
} else {
//every secound click, the servo motor will move back to 0 degress
servo1.slowmove(0, 10);
servo2.slowmove(0, 10);
}
}
*/
/* Help with state changing button
https://www.arduino.cc/en/Tutorial/StateChangeDetection
https://forum.arduino.cc/index.php?topic=61586.0 */