Connecting two Arduinos with I2C

I connected two arduinos with I2C, no problem. I am trying to now take that 'byte' and sort of convert it to a variable. I just took some code from online and modified it (which is why I really sound like I do not know what I am doing). Here is my code for the transmitting arduino:

//Sender

#include <Wire.h>
int num;

void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
  num = 1;
}

void loop()
{
  delay(500);

}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("5  "); // respond with message of 6 bytes
                       // as expected by master
}

Here is the code for the receiving arduino:

//Reciever

#include <Wire.h>

int num;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}

void loop()
{
  Wire.requestFrom(2, 3);    // request _ bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    num = c;
  }
  
  
  if(num == 5){
   digitalWrite(2, LOW); 
  }
  
  if(num == 44){
   digitalWrite(3, LOW); 
  }

  delay(500);
}

In the transmitting code, you can see that I am sending the number 5. In the receiving code, I want that 5 to become a variable. I tried to say "num = c", where c is char c, but that didn't really work. The led (pos to arduino +5v with resistor, neg to pin 2) is not lighting up. What should I do to fix this?

In the transmitting code, you can see that I am sending the number 5

No, I can see you're sending the ASCII character '5' and some spaces.

Okay, how do I convert the ASCII character '5' to a variable '5'?

Subtract the ASCII character '0'

Sorry, but I am not that good at coding :stuck_out_tongue:
What code should I type and where should I type it?

Look up ASCII tables, say here.

I still don't quite understand how to put that in my code? Where should I put it and what should I write?

In the serial print on the receiving end, c = 5. I am confused now because why can't I just say num = c? What should I say instead?

if(num == '5'){

This is my code on the receiving end now:

//Reciever

#include <Wire.h>

int num;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}

void loop()
{
  Wire.requestFrom(2, 3);    // request _ bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    num = c;
  }
  
  
  if(num == '5'){
   digitalWrite(2, LOW); 
  }
  
  if(num == '44'){
   digitalWrite(3, LOW); 
  }

  delay(500);
}

It is still not working though. Is that what I was supposed to do?

I don't know if this helps, but when I look at num in the serial monitor, it comes out as 32 no matter what number I input for the transmitting end.

if(num == '44'){

Do you ever expect a single character to be == '44' ?

I don't know if this helps, but when I look at num in the serial monitor, it comes out as 32 no matter what number I input for the transmitting end.

AKA space (0x20)

okay, I commented out that part for now with the 44. It is still not working with the 5 though.

Wire.write("5  ");

while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    num = c;
  }
  
  
  if(num == '5'){

What chance do you think the last character received will be a '5'?

Oh, okay I see. It working now, thanks! All I did was get rid of the spaces from the transmitting code and told the receiving code to only expect 1 byte. So, what if I did want to have a double digit like 44? How would I change the code to work for both single digit and double digit numbers?

Here is my code now:

//Sender

#include <Wire.h>

void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(500);

}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("5"); // respond with message of 6 bytes
                       // as expected by master
}
//Reciever

#include <Wire.h>

int num;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}

void loop()
{
  Wire.requestFrom(2, 1);    // request _ bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    num = c;
  }
  
  
  if(num == '5'){
   digitalWrite(2, LOW); 
  }
  
  
/* 
  if(num == '44'){
   digitalWrite(3, LOW); 
  }
*/

  delay(500);
}