Im trying to build a robot. I have a master board with an MD25 for just two large motors for movement, and two servos. This is powered by a 12V 2.0Ah battery. The second arduino powers just two small dc motors, with a separate 9v battery, that spin in such a way to project ping pong balls. The two servos act to release the ping pong balls using a chamber system. When the boards are not connected, the servos release the balls perfectly, and the constantly spinning dc motors will fire the balls.
I developed a master/slave code to turn the dc motors on at the right times, rather than being on constantly. The main target is to have the spinning dc motors to stop when the master board is disconnected from its power supply. I assume they have to be connected in some way to do this. This is when the servos misbehave and twitch and start to release balls randomly. I resorted back to the separate codes, and even a common grounding is enough to cause the twitching problems. I have no idea why the common grounding does this..?
I understand that servos draw a lot of current, but there will be a time where both the motors and servos are active.
Thanks in advance.
Master code with steup and some of the initial movements of movement motors and servos.
#include <Wire.h> // Calls for I2C bus library
#define MD25ADDRESS 0x58 // Address of the MD25
#define SPEED1 0x00 // Byte to send speed to both motors for forward and backwards motion if operated in MODE 2 or 3 and Motor 1 Speed if in MODE 0 or 1
#define SPEED2 0x01 // Byte to send speed for turn speed if operated in MODE 2 or 3 and Motor 2 Speed if in MODE 0 or 1
#define ENCODERONE 0x02 // Byte to read motor encoder 1
#define ENCODERTWO 0x06 // Byte to read motor encoder 2
#define ACCELERATION 0xE // Byte to define motor acceleration
#define CMD 0x10 // Byte to reset encoder values
#define MODE_SELECTOR 0xF // Byte to change between control MODES
int DualSpeedValue = 0; // Combined motor speed variable
int speed1 = 0; // Set forward speed for Mode 3 to initial value 0
int speed2 = 0; // Set turn factor for Mode 3 to initial value 0
int Mode = 2; // MODE in which the MD25 will operate selector value
float Wheel_1_Distance_CM = 0; // Wheel 1 travel distance variable
float Wheel_2_Distance_CM = 0;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // a maximum of eight servo objects can be created
int pos = 20; // variable to store the servo position
// Wheel 2 travel distance variable
int motorPin = 11;
int motorPin2 = 12;
byte x = 0;
void setup(){
Wire.begin(); // Begin I2C bus
Serial.begin(9600); // Begin serial
delay(100); // Wait for everything to power up
Wire.beginTransmission(MD25ADDRESS); // Set MD25 operation MODE to mode 2
Wire.write(MODE_SELECTOR);
Wire.write(Mode);
Wire.endTransmission();
myservo.attach(11); // attaches the servo on pin 11 to the servo object
myservo2.attach(12);
pinMode(motorPin, OUTPUT); //sets servos to hold position
pinMode(motorPin2, OUTPUT);
for(pos = 120; pos>=25; pos-=2) // goes from 180 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 120; pos>=25; pos-=2) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
encodeReset();
// Calls a function that resets the encoder values to 0
}
void loop(){
delay(6000);
DualSpeedValue = 150; // Sets a combined motor speed value for upcoming forward motion function
Wheel_1_Distance_CM = 40; // Sets wheel 1 travel distance value - cm, for upcoming forward motion function
Wheel_2_Distance_CM = 40; // Sets wheel 2 travel distance value - cm, for upcoming forward motion function
forward(); // Calls a function that moves the platform forward
stopMotor(); // Calls a function that stops the platform
encodeReset(); // Calls a function that resets the encoder values to 0
x=1;
Wire.beginTransmission(4); // transmit to device #4
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission();
DualSpeedValue = 150; // Sets a combined motor speed value for upcoming right turn function
Wheel_1_Distance_CM = +16.25; // Sets wheel 1 travel distance value - cm, for upcoming right turn motion function
Wheel_2_Distance_CM = -16.25; // Sets wheel 2 travel distance value - cm, for upcoming right turn motion function
Right_Turn(); // Calls a function that turn the platform right by 90 degrees
stopMotor(); // Calls a function that stops the platform
encodeReset(); // Calls a function that resets the encoder values to 0
for(pos = 25; pos < 120; pos += 2) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(500);
for(pos = 120; pos>=25; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(100);
for(pos = 25; pos < 120; pos += 2) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(100);
for(pos = 120; pos>=25; pos-=2) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
This is the slave code that reads x is 1 or 2 and will turn the motors on/off.
#include <Wire.h>
int x = 0;
int motorPin = 9;
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode(motorPin, OUTPUT);
x = 0;
}
void loop()
{
if (x==1){
motorsOn();
}
if (x==2){
motorsOff();
}
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany){
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read();
Serial.print(c);
}
x = Wire.read(); // receive byte as an integer
Serial.println(x);
}
void motorsOn(){
digitalWrite(motorPin, HIGH); // turn the LED on (HIGH is the voltage level)
}
void motorsOff(){
digitalWrite(motorPin, LOW); // turn the LED off by making the voltage LOW // wait for a second
}