I have 2 vibration switches. When I vibrate one of them 3 times, the motor will spin continuously. Once I press a button it should stop. Those functions work, however, there are times when the motor will stop spinning without touching the button. Any ideas as to
#include <Stepper.h>
#define STEPS 200
int slowVibrationSensor;
int mediumVibrationSensor;
int mediumCount;
int slowCount;
//Read variables for the buttons
int emergencyButtonRead;
int fiveRead;
int sevenRead;
int fiveSteps;
//Vibration Pins
const int mediumPin = A5;
const int slowPin = A3;
//Button Pins
const int emergencyButtonPin = 3;
const int fiveFeetPin = 2;
const int sevenFeetPin = 1;
//Motor class declaration
//Steps is the number of steps (200 is full rotation)
//4, 5, 6, and 7 are the digital ports on the board
Stepper stepper(STEPS, 4, 5, 6, 7);
void setup()
{
//Start serial port
Serial.begin(9600);
Serial.println("Stepper test!");
//30 RPMs for the motor
stepper.setSpeed(60);
}
void loop()
{
//Vibration Sensor read analog ports
slowVibrationSensor = analogRead(A3);
mediumVibrationSensor = analogRead(A5);
//Buttons reads the digital pins
emergencyButtonRead = digitalRead(emergencyButtonPin);
fiveRead = digitalRead(fiveFeetPin);
//Print the vibrationSensor. Idles around 400
//Serial.println(mediumVibrationSensor);
//Serial.println(slowVibrationSensor);
//Once the medium sensor hits 1022, enter statement
if(mediumVibrationSensor >1022)
{
//Inc count, and print that it was inc'd
mediumCount++;
Serial.print("Medium Count inc\n");
//If inc'd 7 times, enter statement
if(mediumCount >= 3)
{
//start motor and print that it ran
Serial.println("Forward");
//Step until the button is pressed
while(digitalRead(emergencyButtonPin) == 1)
{
Serial.println(digitalRead(emergencyButtonPin));
stepper.step(1);
//mediumCount = 0;
}
}
}
if(slowVibrationSensor >1022)
{
//Inc count, and print that it was inc'd
slowCount++;
Serial.print("Slow Count inc\n");
//If inc'd 3 times, enter statement
if(slowCount >= 1)
{
//start motor and print that it ran
Serial.println("Forward");
//Step until the button is pressed
while(digitalRead(emergencyButtonPin) == 1)
{
stepper.step(1);
slowCount = 0;
}
}
}
//If this button is pressed, reel it in five feet
if(digitalRead(fiveFeetPin) == 0)
{
while(fiveSteps <= 800 && digitalRead(emergencyButtonPin) == 1)
{
Serial.println(fiveSteps);
stepper.step(1);
fiveSteps++;
}
fiveSteps = 0;
}
//delay(50);
}