I want my code to run my motor for a specific amount of time at a specific speed and then stop and not do anything else. I'm having trouble here, I think with my switch and the if loops running on repeat. What is a good way for me to stop the code from looping? And any helpful hints on my switches will be appreciated as well? Thanks and I hope you had a wonderful thanksgiving if you're American. If you're not, I hope you just had a great day in general.
#include <Servo.h>
#include <Adafruit_MotorShield.h>
#include <Wire.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *DC1 = AFMS.getMotor(1);
Adafruit_DCMotor *DC2 = AFMS.getMotor(2);
Adafruit_DCMotor *DC3 = AFMS.getMotor(3);
Adafruit_DCMotor *DC4 = AFMS.getMotor(4);
Servo Servo1;
void setup() {
digitalWrite(A0, HIGH); //Turns on the pullup resistor for A0
AFMS.begin(); //Initializes the motorshield board
int motorPin1 = 1;
int motorPin2 = 2;
int motorPin3 = 3;
int motorPin4 = 4;
pinMode(A0, INPUT); //Sets the Analog Pin 0 as an input
digitalWrite(A0, HIGH);
pinMode(A1, INPUT);
digitalWrite(A1, HIGH);
pinMode(A1, INPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
Servo1.attach(9);
}
void loop() {
int MAXampDrawTime = 1000;
int MAXampDrawSpeed = 20;
int NORMampDrawTime = 1500;
int NORMampDrawSpeed = 10;
int DrawTime = MAXampDrawTime + NORMampDrawTime;
int ServoTime = 450;
int previousMillis = 0;
int inPin0 = LOW;
int inPin1 = LOW;
inPin0 = digitalRead(A0);
inPin1 = digitalRead(A1);
unsigned long time_zero = millis();
unsigned long current_time = millis();
if (inPin0 == HIGH) {
time_zero = millis();
current_time = millis();
while (millis() - time_zero < MAXampDrawTime); {
dcMoveConst(MAXampDrawSpeed, 1);
dcMoveConst(MAXampDrawSpeed, 2);
dcMoveConst(MAXampDrawSpeed, 3);
dcMoveConst(MAXampDrawSpeed, 4);
}
while (millis() - time_zero < NORMampDrawTime); {
dcMoveConst(NORMampDrawSpeed, 1);
dcMoveConst(NORMampDrawSpeed, 2);
dcMoveConst(NORMampDrawSpeed, 3);
dcMoveConst(NORMampDrawSpeed, 4);
}
if (millis() - time_zero > DrawTime) {
dcMoveConst(0, 1);
dcMoveConst(0, 2);
dcMoveConst(0, 3);
dcMoveConst(0, 4);
}
if (millis() - time_zero >= ServoTime); {
Servo1.write(0);
delay(400);
Servo1.write(90);
}
}
if (inPin1 == HIGH) {
time_zero = millis();
current_time = millis();
MAXampDrawTime = 1000;
MAXampDrawSpeed = 20;
NORMampDrawTime = 1500;
NORMampDrawSpeed = 10;
DrawTime = MAXampDrawTime + NORMampDrawTime;
while (millis() - time_zero < MAXampDrawTime); {
dcMoveConst(MAXampDrawSpeed, 1);
dcMoveConst(MAXampDrawSpeed, 2);
dcMoveConst(MAXampDrawSpeed, 3);
dcMoveConst(MAXampDrawSpeed, 4);
}
while (millis() - time_zero < NORMampDrawTime); {
dcMoveConst(NORMampDrawSpeed, 1);
dcMoveConst(NORMampDrawSpeed, 2);
dcMoveConst(NORMampDrawSpeed, 3);
dcMoveConst(NORMampDrawSpeed, 4);
}
if (millis() - time_zero > DrawTime) {
dcMoveConst(0, 1);
dcMoveConst(0, 2);
dcMoveConst(0, 3);
dcMoveConst(0, 4);
}
if (millis() - time_zero >= ServoTime); {
Servo1.write(0);
delay(400);
Servo1.write(90);
}
}
}