Passing Serial data over I2C to another Arduino
here have arduino mega (master) , uno (slave). both of them connected laptop on usb ports. turn on led on arduino uno when write "on" on adruino mega's serial monitor have on laptop's screen. unfortunately doesn't work.
i grateful if let me know how can solve problem.
here's master code connected both i2c , serial ports.
and here's slave code connected via i2c master.
i grateful if let me know how can solve problem.
here's master code connected both i2c , serial ports.
code: [select]
#include <wire.h>
string input_data_string = "";
char input_data_string[5];
const int ledpin =13;
void setup() {
wire.begin();
serial.begin(9600);
while (!serial) {
}
}
void loop() {
delay(10);
}
void serialevent() {
while (serial.available()) {
char inchar = (char)serial.read();
input_data_string += inchar;
if (contains(input_data_string ,"read")){
input_data_string.tochararray(input_data_string,5);
//serial.println ("message arduino");
wire.begintransmission(2); // transmit device #2
wire.write(input_data_string); // sends string
wire.endtransmission(); // stop transmitting
input_data_string ="";}
if (contains(input_data_string ,"on")){
input_data_string.tochararray(input_data_string,5);
//serial.println ("message arduino");
wire.begintransmission(2); // transmit device #2
wire.write(input_data_string); // sends string
wire.endtransmission(); // stop transmitting
input_data_string ="";}
if (contains(input_data_string ,"off")){
input_data_string.tochararray(input_data_string,5);
//serial.println ("message arduino");
wire.begintransmission(2); // transmit device #2
wire.write(input_data_string); // sends string
wire.endtransmission(); // stop transmitting
input_data_string ="";}
}
}
bool contains( string s, string search) {
int max = s.length() - search.length();
(int i=0; i<= max; i++)
{
if (s.substring(i) == search) return true; // or i
}
return false; //or -1
}
and here's slave code connected via i2c master.
code: [select]
#include <wire.h>
string data = "";
char data[5];
void setup() {
wire.begin(2);
wire.onreceive(receiveevent);
serial.begin(9600);
pinmode(13,output);
digitalwrite(13,low);
}
void loop() {
delay(100);
}
void receiveevent(int howmany) {
while( wire.available()){
delay(500);
data += (char)wire.read();
}
if (data == "on"){
digitalwrite(13, high);
data="";}
digitalwrite(13,low);
if (data == "off"){
digitalwrite(13, low);
data="";}
}
your code seems confused: if want send data master slave, @ wire.h example:
master:
slave:
if want send , receive string:
master
slave:
ps: didn't try code on 2 arduinos
master:
code: [select]
#include <wire.h>
void setup()
{
wire.begin(); // join i2c bus (address optional master)
}
byte x = 0;
void loop()
{
wire.begintransmission(8); // transmit device #8
wire.write("x "); // sends 5 bytes
wire.write(x); // sends 1 byte
wire.endtransmission(); // stop transmitting
x++;
delay(500);
}
slave:
code: [select]
#include <wire.h>
void setup()
{
wire.begin(8); // join i2c bus address #8
wire.onreceive(receiveevent); // register event
serial.begin(9600); // start serial output
}
void loop()
{
delay(100);
}
// function executes whenever data received master
// function registered event, see setup()
void receiveevent(int howmany)
{
while (1 < wire.available()) // loop through last
{
char c = wire.read(); // receive byte character
serial.print(c); // print character
}
int x = wire.read(); // receive byte integer
serial.println(x); // print integer
}
if want send , receive string:
master
code: [select]
#include <wire.h>
#define i2caddress 9
string text;
void gettext(){
if(serial.available()) // check if send message
{
while(serial.available())
{
char c = serial.read(); // read next character.
text += c;
delay(10);
}
}
}
void sendtext() {
wire.begintransmission(i2caddress); // transmit device #9
for(int i=0; i<(text.length()); i++){
wire.write(text[i]);
}
wire.endtransmission(); // stop transmitting
}
void setup() {
serial.begin(9600);
serial.println("master here");
// start i2c bus master
wire.begin();
}
void loop() {
text = "";
while(text.length() == 0) { // until send text in serial monitor
gettext();
}
if(text.length() > 0) {
serial.print("send: " + text);
sendtext();
}
delay(500);
}
slave:
code: [select]
#include <wire.h>
#define i2caddress 9
#define ledpin 13
void receivestring(int bytes) {
string text = "";
while (wire.available()) {
char c = wire.read(); // receive byte character
text += c;
}
serial.print("printing: ");
serial.println(text);
manageled(text);
}
void manageled(string cmd) {
if(cmd == "on")
digitalwrite(ledpin,high);
else if(cmd == "off")
digitalwrite(ledpin,low);
else
serial.println("unknown command: " + cmd);
}
void setup() {
serial.begin(9600);
serial.println("slave here");
// start i2c bus slave on address 9
wire.begin(i2caddress);
// attach function trigger when received.
wire.onreceive(receivestring);
// led
pinmode(ledpin,output);
digitalwrite(ledpin,low);
}
void loop() {
delay(10);
}
ps: didn't try code on 2 arduinos
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Passing Serial data over I2C to another Arduino
arduino
Comments
Post a Comment