Analog POT and Software PWM

I have working code that uses POTs on A1,A2,A3,A4 to control the speed of a motor.

I need to be able to use software to also control these via a web page. My software reads these values to set the speed of a motor.

Is there a way to write these values from a web page?

I've been looking at the possibility of AnalogWrite but it seems to only work on Digital PWM pins.

Can I switch pin modes to software PWM based on another switch position if so would that work with the same results as setting the speed with the POT where I could analogwrite to say A1 then Analogread it back into the arduino code?

Very confused, any direction is very much appreciated

Look at it this way. You can have a variable that is set by either the pot, or the web application. That variable can be used to set the motor speed. You don't (and can't) do silly stuff like writing an analog value to an analog input and reading it back in. You can't redefine the limitations of the Arduino pins, they are determined by the limitations of the processor itself.

Is there a way to write these values from a web page?

I'm totally confused about what you're trying to do...

You don't write from... You write to or read from.

You can read from a web page (assuming you've got a network/web connection and you know how to read it). Then, you can use that data/information to change the PWM and change the motor speed remotely from a web page (ignoring the pot setting).

Or, you can take the pot reading, or the PWM value, from your Arduino software and write that information to your web page. There's no need to read or measure the actual PWM voltage-signal since your software knows the value before you write it out to the PWM pin.

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)

For the web page updating your local variables (not the pots themselves!)
Go read about HTML forms and SUBMIT / POST / GET messages.

If this was my project, I'd keep two variables for each pot position...
One to be actively changed by the pot and/or the web data, and a separate which is copied with the most recent pot value - only...

If the pot is moved (regardless of where the active pot/web variable is set), the program comnpares the pot to the copy value, and 'uses' the new value. You could use a LED to indicate if the pot is active, or has been moved off-value because the web has changed the setting...