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?
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?
//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);
}