Hello!
I am working on I2C communication between Mega(MASTER) and UNO(SLAVE) to send a simple character from one to the other.
I have written the following code, but I am not sure if it is written correctly. Please take a look and let me know if I wrote it right, and if not what changes I need to make.
Mega (MASTER) code:
//This code is for MEGA (Master). The Mega will send a single
//variable to the UNO and let it carry out a pre-set program in the UNO
#include<Wire.h> //library for I2C communication
void setup()
{
Wire.begin(); //join i2c bus
}
void loop()
{
Wire.beginTransmission(3); //transmit to device #3, the waveshield
Wire.write('X'); //send a single byte to the arduino UNO
Wire.endTransmission(); //stop transmitting
delay(500); //delay to help buffer out
}
NOTE: If this needs to be a function, then do the following:
-------------------------------------------------------------------------
void Arduino_Trans()
{
Wire.beginTransmission(3); //transmit to device #3, the waveshield
Wire.write('X'); //send a single byte to the arduino UNO
Wire.endTransmission(); //stop transmitting
delay(500); //delay to help buffer out
}
-------------------------------------------------------------------------
UNO (SLAVE) Code:
//Slave set up for the UNO. It will receive a character from the MEGA
//and execute its program.
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
void setup()
{
Wire.begin(3); //join i2c bus with address #3
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
}
void loop()
{
delay(100); //delay to help buffer
}
void receiveEvent(int howMany)
{
while(1 < Wire.available()) //loop through all but the last
{
char c = Wire.read(); //receive byte as a character
//Serial.print(c); //print character only for debugging
if(c =='X')
{
Serial.println("Hello World!");
delay(1000);
}
NOTE: I am initializing several libraries because I will be expanding the slave program as soon as I verify that it actually works..
Thank you,
Yesenia