Hello and thank you for your time,
I'm working on a project for school, I'm really not the best at programming. I've tried this code a zillion different ways, so if you would like another iteration, I could provide. I think this is the most elegant solution I've come up with, but I feel like I'm up against a wall, and I'm really just out of ideas as to what to try.
I need my program to engage a dc motor when one of two switches are pressed. It needs to run at a certain speed for a certain amount of time in both cases. Flipping switch one will launch for the distance competition and flipping switch two will launch the accuracy competition. A certain amount of time after either switch is flipped, a servo needs to engage.
I'm attaching my code.. I don't know what exactly to call it, a conditional sort of code. I've also got it with if and while statements if you'd like to see. In either case, the switch isn't working and this project is due on thursday! I would appreciate any insight, I hope you can help me.
#include <AccelStepper.h>
#include <Stepper.h>
#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 distancepin = 0; //Just integer quantities, i like to have too many of them
int accuracypin = 0;
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);
}
}