I found a video on youtube, copied and edited it. So that the code light the led (slave) from a push button (master), rather than a PC keyboard.
I was wondering if there is a simple way to it... Rather than sending H or L. I would like to send something like "Button1"
I dont really understand the char c = Wire.read();
and why I can only send one letter... If I change it to a word, it doesn't work.
Master
//i2c Master Code(Mega 2560)
#include <Wire.h>
const int buttonPin = 2;
int buttonState = 0;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
Wire.begin();
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Wire.beginTransmission(5);
Wire.write('H');
Wire.endTransmission();
}
else {
Wire.beginTransmission(5);
Wire.write('L');
Wire.endTransmission();
}
}
Slave
//i2c Slave Code(Uno)
#include <Wire.h>
void setup()
{
Wire.begin(5);
Wire.onReceive(receiveEvent);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
}
void loop()
{
}
void receiveEvent(int howMany)
{
while(Wire.available())
{
char c = Wire.read();
if(c == 'H')
{
digitalWrite(13,HIGH);
}
else if(c == 'L')
{
digitalWrite(13,LOW);
}
}
}
Any help would be great.
Thanks.