Thanks you added much more clarity to my confused thinking. In my code I am reading the analog value to a variable. My mind was going to the illogical.
I’m trying to use BLE to connect a node app (running on a PI to perform control of motors.
Do you know of the best method to read or put this variable from a web page over BLE to arduino? is ARest or Firmata the best option or is there a better way to get the variable over to the Arduino from the Web Page? Would I not need to put the variable value or send it to the arduino since I’m sing BLE?
Thanks again for straightening me out, I was headed down a deep rabbit hole.
Here is my current code
#include <Wire.h>
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
// 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 = 5;
int DirectionSW2 = 6;
int DirectionSW3 = 7;
int DirectionSW4 = 8;
//Direction Switch Array
int switches[4] = { DirectionSW1, DirectionSW2, DirectionSW3, DirectionSW4};
// Define switch for Trolley Mode
// TRUE=Trolley mode, FALSE=Train mode
int TrolleyModeSwitch = 4;
//Define Switch for Trolley Delay
// TRUE=30 second delay, FALSE=60 second delay
int TrolleyDelaySwitch = 7;
// define sensible name for trolley mode
#define TROLLEYMODE LOW
void setup() {
// Setup Serial library at 9600 bps
Serial.begin(9600);
//Setup internal PULLUP for trains
pinMode(DirectionSW1,INPUT_PULLUP);
pinMode(DirectionSW2,INPUT_PULLUP);
pinMode(DirectionSW3,INPUT_PULLUP);
pinMode(DirectionSW4,INPUT_PULLUP);
// PULLUP mode for Trolley Switches
pinMode(TrolleyModeSwitch ,INPUT_PULLUP);
pinMode(TrolleyDelaySwitch,INPUT_PULLUP);
// create with the default frequency 1.6KHz
AFMS.begin();
}
void loop()
{
// Loop through trains (trainArray starts at 0)
for (int trainIndex = 0; trainIndex < 4; trainIndex++)
{
// read the speed from the potentiometer (and divide by 4)
int speed = analogRead(trainSpeeds[trainIndex]) / 4;
// get the direction from the switch
int direction = digitalRead(switches[trainIndex]);
if (trainIndex < 3 || (trainIndex == 3 && digitalRead(TrolleyModeSwitch) != TROLLEYMODE))
{
controlTrain(trainIndex, direction, speed);
}
else
{
doTrolley(trainIndex, speed);
}
}
}
/*
set trainspeed and direction for train specified by index
*/
void controlTrain(int index, int direction, int speed)
{
// set direction
if (direction == LOW)
trains[index]->run(FORWARD);
else
trains[index]->run(BACKWARD);
// set speed
trains[index]->setSpeed(speed);
}
/*
delay for number of milliseconds
returns false if delay in progress, else true
*/
bool doDelay(unsigned long duration)
{
// remember the start time of the delay; 0 means that delay is not started
static unsigned long starttime = 0;
if (starttime == 0)
{
// set the start time
starttime = millis();
}
else
{
// if delay lapsed
if (millis() - starttime > duration)
{
// reset start time
starttime = 0;
// indicate that delay has lapsed
return true;
}
}
// indicate delay is in progress
return false;
}
/*
statemachine for trolley mode
*/
void doTrolley(int index, int speed)
{
// remember what trolley is doing
static int trolleystate = 0;
// remember trolley direction
static int trolleydirection = LOW;
// duration of delay to set runtime
unsigned long delayduration;
if(digitalRead(TrolleyDelaySwitch) == HIGH)
{
delayduration = 30000;
}
else
{
delayduration = 60000;
}
switch (trolleystate)
{
case 0:
controlTrain(index, trolleydirection, speed);
if (doDelay(delayduration))
{
// done with this state
trolleystate++;
}
break;
case 1:
// change direction
if (trolleydirection == LOW)
{
trolleydirection = HIGH;
}
else
{
trolleydirection = LOW;
}
// done with this state, back to first state
trolleystate = 0;
break;
}
}
TC_4Trains_in_Array_TrollyChanges_NEW_DChanges.ino (3.95 KB)