However, connecting everything to work in tandem the way we hope is not going quite as smoothly. Here is our code to date.
//Define the sensors
const int seat = 12;
const int fwdStop = 2;
const int revStop = 4;
//Interrupt sensor
int shiftStageTwo = 0;
//Define the motors
const int fwd = 7;
const int rev = 8;
//Define the stages in the process
int stageOne = 0;
int stageTwo = 0;
int stageThree = 0;
//Interrupt stuff
volatile int shiftStagetwo = fwdStop;
void setup() {
//Set pin modes
pinMode(seat, INPUT);
pinMode(fwdStop, INPUT);
pinMode(revStop, INPUT);
pinMode(fwd, OUTPUT);
pinMode(rev, OUTPUT);
//Set interrupts
attachInterrupt(stageTwo, swap, LOW);
}
//Interrupt function
void swap() {
if (shiftStagetwo == fwdStop) {
digitalWrite(fwd, HIGH);
digitalWrite(rev, HIGH);
}
else {
digitalWrite(fwd, HIGH);
digitalWrite(rev, HIGH);
}
}
void loop(){
stageOne = digitalRead(seat);
stageTwo = digitalRead(fwdStop);
stageThree = digitalRead(revStop);
if (stageOne == HIGH) {
//delay(5000); //delays don't work if multiple delays are present
digitalWrite(fwd, LOW);
digitalWrite(rev, HIGH);
}
else {
digitalWrite(fwd, HIGH);
digitalWrite(rev, HIGH);
}
if (stageTwo == LOW) {
digitalWrite(fwd, HIGH);
digitalWrite(rev, LOW);
}
else {
digitalWrite(fwd, HIGH);
digitalWrite(rev, HIGH);
}
// if (stageThree == LOW) {
// delay(5000);
// digitalWrite(fwd, HIGH);
// digitalWrite(rev, LOW);
// detachInterrupt(stageTwo);
// }
// else {
// digitalWrite(fwd, HIGH);
// digitalWrite(rev, HIGH);
// }
}
The interrupt function has effectively let us switch between motors, but things are still not quite up to par. Dominic's recommendation is to simplify our code and reduce most of our methods to if and while loops. That's next on our plate!
No comments:
Post a Comment