omg! I need help, so for the first bit of the code for my servo motors, I used a state change detection for a push button. Now I am trying to use a push button to move a stepper motor. I the state change detection is messing with that my stepper motor button. any thoughts on how to fix that.
/* 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);
// SERVO MOTOR
// this constant wont change:
const int servobutton = 2;
const int servopin1 = 9;
const int servopin2 = 10;
const int stepperbutton = 8;
//declaring servo#1 as servo1 and servo #2 as servo2
VarSpeedServo servo1;
VarSpeedServo servo2;
// Variables can/will change:2
int buttonPushCounter = 0;
int lastButtonState = 0;
int stepperval;
void setup(){
// initialize the button pin as a input:
pinMode(servobutton, INPUT);
pinMode(stepperbutton, INPUT);
//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); // 10 rpm
Serial.begin(9600);
}
void loop(){
stepper();
// FOR SERVO MOTORS
//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) {
//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);
}
}
void stepper() {
stepperval == digitalRead(stepperbutton);
if (stepperval == HIGH) {
Serial.println("on");
myMotor->step(100, FORWARD, SINGLE);
myMotor->step(100, BACKWARD, SINGLE);
delay(50);
} else {
Serial.println("off");
delay(50);
}
}
/* Help with state changing button
https://www.arduino.cc/en/Tutorial/StateChangeDetection
https://forum.arduino.cc/index.php?topic=61586.0 */