HI,
I'm using the Adafruit Motor Shield V3.
Been through a bunch of tutorials but just can't figure it out.
I have 4 trains connected to the board identified as Train1, Train2, Train3 and Train4.
Each train has a direction switch. DirectionSW1, DirectionSW2, DirectionSW3, and DirectionSW4
Each Train has a POT to control Speed. Speed1, Speed2, Speed3, Speed4
Currently I have an array setup to handle Train1-3. This part works.
For Train4, I have to set it up so it has 2 addition switches. 1 to flip it to Trolley Mode or Train Mode. the other to set the delay for 30-60 seconds.
In Train Mode, it needs to operate the same as the other 3 trains.
When in Trolley Mode, it has to run for a set period of time... Say 30 Seconds then wait for a set period of time (30 or 60 seconds) then reverse direction.
In Trolley Mode the POT for Speed4 needs to control speed as well.
I'm lost. I have tried the mills function but I can never seem to figure out how to make the program work.
My code just keeps getting out of hand and never works for the trolley/train mode switch with the timers.
Not sure how to structure the code.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. This controls the Trains attached to the ports defined.
Adafruit_DCMotor *Train1 = AFMS.getMotor(1);
Adafruit_DCMotor *Train2 = AFMS.getMotor(2);
Adafruit_DCMotor *Train3 = AFMS.getMotor(3);
Adafruit_DCMotor *Train4 = AFMS.getMotor(4);
// Train Motor Array
Adafruit_DCMotor *trains[4] = {Train1, Train2, Train3, Train4};
//Define POT Speed controls
int trainSpeeds[4] = {A0, A1, A2, A3};
// Train state array
int trainStates[4];
//Define Switches that control Direction of Trains
int DirectionSW1 = 2; // Direction Switch for Train 1. Connected to Digital Input 1
int DirectionSW2 = 3; // Direction Switch for Train 2. Connected to Digital Input 2
int DirectionSW3 = 4; // Direction Switch for Train 3. Connected to Digital Input 3
int DirectionSW4 = 5; // Direction Switch for Train 4. Connected to Digital Input 4
int switches[4] = { DirectionSW1, DirectionSW2, DirectionSW3, DirectionSW4};
// Define switches related to Trolleys
int TrolleyModeSwitch = 6; // TRUE=Trolley mode, FALSE=Train mode
int TrolleyDelaySwitch = 7; // TRUE=5 second delay, FALSE=10 second delay
//
// Setup Board and PinMode
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
//Setup internal PULLUP for trains
pinMode(DirectionSW1,INPUT_PULLUP); // Direction1 Switch
pinMode(DirectionSW2,INPUT_PULLUP); // Direction2 Switch
pinMode(DirectionSW3,INPUT_PULLUP); // Direction3 Switch
pinMode(DirectionSW4,INPUT_PULLUP); // Direction4 Switch
// PULLUP mode for Trolley Switches
pinMode(TrolleyModeSwitch ,INPUT_PULLUP); // TrainTrolleyMode Switch
pinMode(TrolleyDelaySwitch,INPUT_PULLUP); // TrolleyWaitDelay Switch
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
}
void loop() {
// Loop through trains (trainArray starts at 0)
for (int trainIndex=0; trainIndex<4; trainIndex++) {
trainStates[trainIndex] = digitalRead(switches[trainIndex]);
if (trainStates[trainIndex] == LOW) {
trains[trainIndex]-> run(FORWARD);
} else {
trains[trainIndex]-> run(BACKWARD);
}
trains[trainIndex] -> setSpeed(analogRead(trainSpeeds[trainIndex])/4);
}
}
attached is a frizzing diagram and the code that works for all 4 trains in the array. Again, I need to pull the 4th out of array and add the train/trolley switch with delay function.
Thanks in advance for the help, just really need the help of someone that knows more than I do.
TC_4Trains_in_Array.ino (2.35 KB)