Adding a 2nd motor to remote controlled motor project using Arduino, Python etc

Hi All, I’m in need for some advice and direction please, which can be paid for if required.

I’ve been following a very good tutorial at http://www.openhomeautomation.net/remotely-controlling-a-motor-using-arduino-python-and-php and watched the supporting Utube vid at http://www.youtube.com/watch?v=_yj07TzL_1I and amazingly have managed to complete it. The tutorial shows how to remotely control a dc motor using Arduino,Python & php and an L293D motor driver chip.

I now want to be able to add another motor and more if possible. In the blurb the author mentions that its possible to control two motors using this chip so I want to try this but have no idea how to change the python code and the arduino sketch. I know enough to be able to edit and administer the php side of things.

I have tried to edit the arduino sketch adding new variables where I think they are needed; and the results had a glimmer of being in the right direction but I really had no idea what needed changing and it just feels like Im looking for a blackboard in a very dark room. Can anyone help me to add the correct code. The long term aim is to control 8 motors.

The arduino sketch:

int input;
 
int motorPinPlus = 4;
int motorPinMinus = 5;
int motorPinEnable = 6;
 
int motorDir;
int motorSpeed;
 
const int NB_OF_VALUES = 2;
int valuesIndex = 0;
int values[NB_OF_VALUES];
 
// Setup of the board
void setup() {
   // Initialize pins
   pinMode(motorPinPlus, OUTPUT);
   pinMode(motorPinMinus, OUTPUT);
   pinMode(motorPinEnable, OUTPUT);
 
   // Initialize serial port
   Serial.begin(114000);
 
   motorDir = 1;
   motorSpeed = 200;
}
 
// Main loop
void loop() {
 
   if (Serial.available())
   {
      if (Serial.read() == 'H')
      {
         for(valuesIndex = 0; valuesIndex < NB_OF_VALUES; valuesIndex++)
         {
            values[valuesIndex] = Serial.parseInt();
         }
 
         motorDir = values[0];
         motorSpeed = values[1];
 
         valuesIndex = 0;
      }
 
      setMotor(motorDir, motorSpeed);
   }
}
 
// Function to control the motor
void setMotor(int forward, int speed){
   if (forward == 0){
      digitalWrite(motorPinPlus, HIGH);
      digitalWrite(motorPinMinus, LOW);
   }
   else {
      digitalWrite(motorPinPlus, LOW);
      digitalWrite(motorPinMinus, HIGH);
   }
 
   analogWrite(motorPinEnable, speed);
}

The Python File:

import serial
import time
 
try:
print 'Trying...';
arduino = serial.Serial('/dev/tty.usbmodem1411', 114000)
time.sleep(1)
arduino.flush()
except:
print 'Failed to connect';
 
sleeptime = 0.1
speed = 0
 
while True:
try:
 
  # Open a file
  fo = open('motorCommand.txt','r+')
  speed = fo.read()
  # Close the file
  fo.close()
 
  arduino.write('H,1,' + speed)
  time.sleep(sleeptime)
 
except:
  print 'Fail !';

The complete file list is on GitHub at https://github.com/openhomeautomation/remote-control-motor If I need to post the code from the php file and the javascript files please ask. Not sure if they are needed…

  arduino.write('H,1,' + speed)

This tells the Arduino to run the motor in some direction at some speed. You need to add another value to tell the Arduino which motor to run.

const int NB_OF_VALUES = 2;
int valuesIndex = 0;
int values[NB_OF_VALUES];

You'd need to change this to allow for one ,more value (which motor).

         motorDir = values[0];
         motorSpeed = values[1];

You'd need to add another line to hold the motor number.

      setMotor(motorDir, motorSpeed);

This function needs another argument - the motor number.

int motorPinPlus = 4;
int motorPinMinus = 5;
int motorPinEnable = 6;

These should be arrays, so that motor number is an index.

Thanks for the response Paul.

Your first bit...

arduino.write('H,1,' + speed)

This is a bit I find rather difficult to get my head round. According to the guide the contents in between the ' ' (called the array???) is what is being sent to the arduino sketch. The 'H' is a header and identifies the string about to be read. Now according to what I'm reading (and how I interpret it) the first figure should be a '1' or a '0' and declares the direction of the motor. In this example its hard-coded into the code. Next is the contents read from the file and identified as an integer??? ' speed'. This should be a number between 0 and 255. 0 means the motor is off; 255 is maximum speed.

If I understand what I've read I should be able load the arduino sketch to my Arduino Uno and the run the python file. I should be able to stop the python file running and adjust the direction and speed and the restart the python file and see the motor change accordingly?

I should be able to stop the python file running and adjust the direction and speed and the restart the python file and see the motor change accordingly?

Yes. Restarting the Python script will restart the Arduino, though. So, the motor will stop, too.