My Hardware:
Arduino Mega 2560
Grove Mega Shield
4x Grove I2C Motor Driver v1.2
2x Grove I2C Hub v1.1
I would like to controll 8 DC motors.
I started with one of the Motor Drivers with two DC motors. First i ran the Demo-Code and then i made a few changes in it and everything works fine.
My next step was connecting the second Motor Driver and add a few lines of Code to controll both at a time.
My intention was to let Motor 1 move, then 3, then 2 and then 4. Sadly Motor 1 moves 2 seconds and then motor 2 moves 2 seconds.
The Code i used is in the spoiler. Please tell me what i have done wrong.
[spoiler]
#include <Wire.h>
#define MotorSpeedSet 0x82
#define PWMFrequenceSet 0x84
#define DirectionSet 0xaa
#define MotorSetA 0xa1
#define MotorSetB 0xa5
#define Nothing 0x01
#define IF_M1M2 0x0a // Set the address of the I2CMotorDriver
#define IF_M3M4 0x0f
void MotorSpeedSetIF_M1M2(unsigned char MotorSpeedA , unsigned char MotorSpeedB)
{
MotorSpeedA=map(MotorSpeedA,0,100,0,255);
MotorSpeedB=map(MotorSpeedB,0,100,0,255);
Wire.beginTransmission(IF_M1M2); // transmit to device I2CMotorDriverAdd
Wire.write(MotorSpeedSet); // set pwm header
Wire.write(MotorSpeedA); // send pwma
Wire.write(MotorSpeedB); // send pwmb
Wire.endTransmission(); // stop transmitting
}
void MotorDirectionSetIF_M1M2(unsigned char Direction) // Adjust the direction of the motors 0b0000 I4 I3 I2 I1
{
Wire.beginTransmission(IF_M1M2); // transmit to device I2CMotorDriverAdd
Wire.write(DirectionSet); // Direction control header
Wire.write(Direction); // send direction control information
Wire.write(Nothing); // need to send this byte as the third byte(no meaning)
Wire.endTransmission(); // stop transmitting
}
void MotorSpeedSetIF_M3M4(unsigned char MotorSpeedA , unsigned char MotorSpeedB)
{
MotorSpeedA=map(MotorSpeedA,0,100,0,255);
MotorSpeedB=map(MotorSpeedB,0,100,0,255);
Wire.beginTransmission(IF_M3M4); // transmit to device I2CMotorDriverAdd
Wire.write(MotorSpeedSet); // set pwm header
Wire.write(MotorSpeedA); // send pwma
Wire.write(MotorSpeedB); // send pwmb
Wire.endTransmission(); // stop transmitting
}
void MotorDirectionSetIF_M3M4(unsigned char Direction) // Adjust the direction of the motors 0b0000 I4 I3 I2 I1
{
Wire.beginTransmission(IF_M3M4); // transmit to device I2CMotorDriverAdd
Wire.write(DirectionSet); // Direction control header
Wire.write(Direction); // send direction control information
Wire.write(Nothing); // need to send this byte as the third byte(no meaning)
Wire.endTransmission(); // stop transmitting
}
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
delayMicroseconds(10000); //wait for motor driver to initialization
}
void loop() {
while(1) {
MotorSpeedSetIF_M1M2(100,100);
delay(10); //this delay needed
MotorDirectionSetIF_M1M2(0b1000);
delay(1000);
MotorSpeedSetIF_M3M4(100,100);
delay(10); //this delay needed
MotorDirectionSetIF_M3M4(0b1000);
delay(1000);
MotorSpeedSetIF_M1M2(100,100);
delay(10); //this delay needed
MotorDirectionSetIF_M1M2(0b0010);
delay(1000);
MotorSpeedSetIF_M3M4(100,100);
delay(10); //this delay needed
MotorDirectionSetIF_M3M4(0b0010);
delay(1000);
}
}
[/spoiler]
PS: Sorry for my bad english.