Connecting two Arduinos. Master / Slave....

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.

Have you done much reading at the Wire.h library page?
I2C only sends one byte at a time. If you want longer, you need to read the bytes and put them together into something longer, than act on longer thing.

Reading now. Thank you.

If you can convey all the information you need in a single character (and it seems like you can) then it is pointless to make the code more complex just to send additional superfluous characters.

A guy called Claude Shannon made a name for himself out of "less is more".

...R

I still dont understand the

char c = Wire.read();

Is the c a real thing? Or are the using c as a random char?

Hi,
Take a look at some of the different RFID program loops, they might help you understand how to receive and send code from one device to another.

Basically you send a starting tag and a complete tag so that your second system knows when the first system is done sending.

Yes you can do it with one char and then do a switch case for the actions that you want performed based on the char sent

Hope it helps

Jasit

"c" in this case is just a variable name. "incomingByte" could have been as easily used.