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 openhomeautomation.net and watched the supporting Utube vid at Remotely controlling a motor using Arduino, Python and PHP - YouTube 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 GitHub - makecademy/remote-control-motor: Remotely controlling a motor using Arduino, Python and PHP If I need to post the code from the php file and the javascript files please ask. Not sure if they are needed...