I am using a Nano to scan multiple IR sensors, and send the results to an R4 via I2C. I can form the string on the Nano, and it prints locally correctly. When the R4 sees it it is just a number that has no meaning. What am I doing wrong?
//Nano Ir sensor scanner
// must use ATMega328P OLD BOOTLOADER
#include <Wire.h>
// Define sensor pin and data variable
const int sensor1Pin = A0;
const int sensor2Pin = A1;
const int sensor3Pin = A2;
int sensor1Value;
int sensor2Value;
int sensor3Value;
void setup() {
Serial.begin(9600);
Wire.begin(8); // Set Nano as I2C slave with address 8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(1000);
sensor1Value = digitalRead(sensor1Pin);
sensor2Value = digitalRead(sensor2Pin);
sensor3Value = digitalRead(sensor3Pin);
char values[100];
sprintf(values,"%d%d%d ", sensor1Value,sensor2Value,sensor3Value);
Serial.println(values); // forms the string as 3 digits, 0 or 1
}
void requestEvent() {
Serial.println("Sending");
sensor1Value = digitalRead(sensor1Pin);
sensor2Value = digitalRead(sensor2Pin);
sensor3Value = digitalRead(sensor3Pin);
// form a char string with 3 sensor states
char values[100];
sprintf(values,"%d%d%d ", sensor1Value,sensor2Value,sensor3Value);
Wire.write(values); // Send sensor data to master
}
Did you forget to use code tags when you posted your sketch ?
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Make a simple check of your I2C Network by reading data = 0x1234 from the Slave and then try with data = 13.75 and try with your Sensor data. Arduino UNOR4: Master Sketch:
#include <Wire.h>
// Listener
void setup()
{
Wire.begin(); // Initialize as I2C master
Serial.begin(9600);
Serial.println("Listening");
}
void loop()
{
Wire.requestFrom(8, 2); //reading two bytes from Slave
Serial.println("Receiving");
byte receivedDataH = Wire.read(); // Read MSByte of data received
byte receivedDataL = Wire.read(); // Read LSByte of data received
int receivedData = receivedDataH << 8 | receivedDataL;
Serial.println(receivedData); // shows: 1234
delay(1000); // Add a delay
}
Arduino NANO: Slave Sketch:
#include <Wire.h>
// Define sensor pin and data variable
int data = 0x1234;
void setup()
{
Serial.begin(9600);
Wire.begin(8); // Set Nano as I2C slave with address 8
Wire.onRequest(requestEvent); // register event
}
void loop()
{
}
void requestEvent()
{
Wire.write(highByte(data));
Wire.write(lowByte(data));
}
Apparently I'm just talking to myself here. I'll make one last effort and then call it.
You are writing a character array. Three ASCII digits and a trailing null.
You are reading a single byte. Which in this case is the first ASCII digit you sent. A '0' or a '1', whose ASCII decimal values are 48 and 49. And then assigning that to an int and expecting I honestly don't know what.
You must read data in the same representation it was written in. If you write it in ASCII, you must read it in ASCII.
As was pointed out above, I was creating a char array and then reading it as an int. I send the values of 3 IR senors as 0 or 1.
The code on the read side that works is:
Wire.requestFrom(8, 4); //reading two bytes from Slave
Serial.println("Receiving");
byte receivedDataH = Wire.read(); // Read MSByte of data received
byte receivedDataL = Wire.read(); // Read LSByte of data received
int receivedData = receivedDataH<< 8 | receivedDataL;
Serial.println(receivedData); // shows sensor state
This prints the following
000
001
100
111 depending on the values sent.
Now i need to get this passed into s0, s1 and s2 to represent the 3 inputs.
The remote slave will monitor multiple sensors, the master is a webserver posting the sensor states(hopefully) on a control panel and i want to keep them apart. I agree one r4 can do it all, just thinking ahead. I will need separation between the 2 up to about 3ft eventually.
It might work but will be buggy. Use something designed to communicate over distances. I use CAN as it is inexpensive and multi master good for a few thousand feet.