I'm building an Android Controlled Rover using Bluetooth (BluefruitEZLink)
I'm doing it using Processing for android but because my motorshield uses pins 10/11 for RX/TX I need to put the Blue tooth onto it's own arduino and send commands using I2C.
https://www.pololu.com/product/2507 -motorshield Link
I could use a Mega but I'm running a lot of voltage and current through the motor shield and i want to protect the EZLink.
This example works and is the basis for my arduino/bluetooth communication:
just so you know "mySerial" is the serial monitor on the android which displays "LED is on" when I tap the screen but the slave arduino reader is only showing -1-1-1-1-1-1-1-1-1-1-1-1 until i hit the android screen then it switches to 8108108108 ad infinitum.
Master writer Code
//MasterWriterI2CBluetoothProcessing
#include <Wire.h>
#include <SoftwareSerial.h>
//led is in slave
char chByte = 0; // incoming serial byte
String strInput = ""; // buffer for incoming packet
String strCompare = "switch";
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
//initialize BT Serial
mySerial.begin(9600);
}
//int x = 0;
void loop() {
Wire.beginTransmission(9); // transmit to device #8
// sends five bytes
// sends one byte
while (mySerial.available() > 0)
{
// get incoming byte:
chByte = mySerial.read();
if (chByte == '\r')
{
//compare input message
if(strInput.equals(strCompare))
{
//toggle LED
// Wire.write("led state ");
Wire.write(chByte);
mySerial.println("LED is ON");
delay(1000);
//Wire.write(x=0);
//mySerial.println("LED is OFF");
}
//reset strInput
strInput = "";
}
else
{
strInput += chByte;
}
}
//x++;
delay(500);
Wire.endTransmission(); // stop transmitting
Slave Reader:
//SlaveReaderOf Bluetooth Command to turn LED on
// Include the required Wire library for I2C
#include <Wire.h>
#include <SoftwareSerial.h>
int LED = 13;
int x = 0;
void setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
Serial.println(x);
}
void loop() {
//If value received is 0 blink LED for 200 ms
if (x >0) {
digitalWrite(LED, HIGH);
Serial.print(x);
}
else {
digitalWrite(LED, LOW);
Serial.print(x);
}
}
The I2C example I'm working off of is this which I haven't actually gotten to work with the LED (pin13) but it does send the correct x values.
Anyways this shouldn't be too hard but I'm messing something up