First off let me say i am a noob.. No doubt about it.
I am attempting to build a robot.. got that part covered. Mechanics are pretty much taken care of. The programming not so much.
I am using the Motor Shield from Ladyada, and I must say that i love it. Easy to use. Easy to understand. The only thing i can say negative about it is that i ran out of pins. It uses a lot of the pins up and i am left with mainly my analog pins and a couple of digital pins. When trying to control a serial lcd, multiple distance and obstacle detectors, I am out of pins.
The thought came to me of setting up i2c and using multiple arduinos to control my bots functions, offloading the motor control to one board with the motor shield attached, then being able to add my lcd as another board that would also control my lighting, and a few other small circuits, and be able to send navigation information from my main arduino to both of the others.
I got the i2c communication up with very little problems using the wire example in the arduino 10 sketchbook. The problem i have starts when i begin modifying the code. I am getting some strange things back from my slave arduino when it prints out my data.
The code that follows is code that uses the IR.h library available in the arduino playground, and wire.h. The code is supposed to receive a signal from a universal remote, and then transmit the proper action to be taken to the slave arduino.
The slave arduino then receives this data, and translates it to motion. ( in this example i have it just printing the results to the serial monitor, later it will actually control the motor shield)
When you run this code, and use a standard ir receiver like the ones available from radio sh*** and a universal son* remote, it will print in the serial monitor the direction i want to travel, but it is also adding some numbers to the end of the data :
ahead32
ahead32
ahead32
rever115
rever115
right32
left 32
left 32
Can someone please tell me what is happening? I totally don't understand, and i have done it in several different codes, and i always get those odd numbers.
Here is the master sender code i am using:
#include <IR.h>
#include <Wire.h>
void setup()
{
Serial.begin(38400);
Wire.begin(); // join i2c bus as master address not required
IR::initialise(0);// IR receiver hardware is on pin2.
}
void loop()
{
if (IR::queueIsEmpty())
{
// Do something more interesting
}
else
{
IR_COMMAND_TYPE code;
while (IR::queueRead(code))
{
if (code == 16){
forward();
} else if (code == 17){
reverse();
} else if (code == 18){
right();
} else if (code == 19){
left();
} else if (code == 20){
stopall();
}
}
}
}
void forward(){
Wire.beginTransmission(4); // transmit to device #4
Wire.send("ahead "); // sends six bytes
Wire.endTransmission(); // stop transmitting
}
void reverse(){
Wire.beginTransmission(4); // transmit to device #4
Wire.send("revers"); // sends six bytes
Wire.endTransmission(); // stop transmitting
}
void left(){
Wire.beginTransmission(4); // transmit to device #4
Wire.send("left "); // sends six bytes
Wire.endTransmission(); // stop transmitting
}
void right(){
Wire.beginTransmission(4); // transmit to device #4
Wire.send("right "); // sends six bytes
Wire.endTransmission(); // stop transmitting
}
void stopall(){
Wire.beginTransmission(4); // transmit to device #4
Wire.send("stop!"); // sends six bytes
Wire.endTransmission(); // stop transmitting
}
Here is the code i am using on the slave (which is actually the example code from the wire library):
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
#include <Wire.h>
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop()
{
delay(100);
}
// 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.receive(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.receive(); // receive byte as an integer
Serial.println(x); // print the integer
}
Any help would be greatly appreciated, i have been beating this horse for about three days now.