Good day ladies and gents,
I was wondering if you could kindly please help me with a big problem I am facing at the moment. I am trying to get a few arduinos to connect to each other through I2C but I am not getting the results I wanted. But before I go into the coding, I will give a run down of my goal.
#There’s 4 slave arduino, each connected to 4 LDRs.
#The slave simply sends the LDR analog reading to the master.
#The master receives the analog reading from each arduino.
#The master executes a few if commands, such as
if analogreadingslave11 > analogreadingslave 23 // ie the reading from slave1LDR1 is greater than slave2LDR3
…so and so…
So in essence, its a pretty simple idea of sending analog readings from 4 arduinos to the 1 master. But I keep getting unexpected results. So without further ado, here are my coding.
Master
int sensorReading00;
int sensorReading01;
int sensorReading02;
int sensorReading03;
int sensorReading10;
int sensorReading11;
int sensorReading12;
int sensorReading13;
int sensorReading20;
int sensorReading21;
int sensorReading22;
int sensorReading23;
int sensorReading30;
int sensorReading31;
int sensorReading32;
int sensorReading33;
#include <Wire.h>
void requestSensor(char address, int* sensor0, int* sensor1, int* sensor2, int* sensor3)
{
byte data[] = {0, 0, 0, 0, 0, 0, 0, 0};
Wire.requestFrom(0x30, 8);
byte c = 0;
while (c < 8)
{
if (Wire.available())
{
data[c] = Wire.receive();
c++;
}
}
*sensor0 = (((int) data[0]) << 8) + data[1];
*sensor1 = (((int) data[3]) << 8) + data[3];
*sensor2 = (((int) data[4]) << 8) + data[5];
*sensor3 = (((int) data[6]) << 8) + data[7];
}
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
requestSensor(0x30, &sensorReading00, &sensorReading01, &sensorReading02, &sensorReading03);
requestSensor(0x31, &sensorReading10, &sensorReading11, &sensorReading12, &sensorReading13);
requestSensor(0x32, &sensorReading20, &sensorReading21, &sensorReading22, &sensorReading23);
requestSensor(0x33, &sensorReading30, &sensorReading31, &sensorReading32, &sensorReading33);
Serial.print("Analog reading00 = ");
Serial.println(sensorReading00); // the raw analog reading
Serial.print("Analog reading01 = ");
Serial.println(sensorReading01); // the raw analog reading
Serial.print("Analog reading02 = ");
Serial.println(sensorReading02); // the raw analog reading
Serial.print("Analog reading03 = ");
Serial.println(sensorReading03); // the raw analog reading
Serial.print("Analog reading10 = ");
Serial.println(sensorReading10); // the raw analog reading
Serial.print("Analog reading11 = ");
Serial.println(sensorReading11); // the raw analog reading
Serial.print("Analog reading12 = ");
Serial.println(sensorReading12); // the raw analog reading
Serial.print("Analog reading13 = ");
Serial.println(sensorReading13); // the raw analog reading
Serial.print("Analog reading20 = ");
Serial.println(sensorReading20); // the raw analog reading
Serial.print("Analog reading21 = ");
Serial.println(sensorReading21); // the raw analog reading
Serial.print("Analog reading22 = ");
Serial.println(sensorReading22); // the raw analog reading
Serial.print("Analog reading23 = ");
Serial.println(sensorReading23); // the raw analog reading
Serial.print("Analog reading30 = ");
Serial.println(sensorReading30); // the raw analog reading
Serial.print("Analog reading31 = ");
Serial.println(sensorReading31); // the raw analog reading
Serial.print("Analog reading32 = ");
Serial.println(sensorReading32); // the raw analog reading
Serial.print("Analog reading33 = ");
Serial.println(sensorReading33); // the raw analog reading
delay(100);
}
Slave
int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0
int photocellReading0; // the analog reading from the analog resistor divider
int photocellPin1 = 1; // the cell and 10K pulldown are connected to a1
int photocellReading1; // the analog reading from the analog resistor divider
int photocellPin2 = 2; // the cell and 10K pulldown are connected to a2
int photocellReading2; // the analog reading from the analog resistor divider
int photocellPin3 = 3; // the cell and 10K pulldown are connected to a3
int photocellReading3; // the analog reading from the analog resistor divider
#include <Wire.h>
void requestHandler()
{
int data[4];
data[0] = analogRead(photocellPin0);
data[1] = analogRead(photocellPin1);
data[2] = analogRead(photocellPin2);
data[3] = analogRead(photocellPin3);
Wire.send((byte*) data, 4);
}
void setup()
{
Wire.begin(0x30); // join i2c bus (address optional for master)
Serial.begin(9600);
}
void loop() {
photocellReading0 = analogRead(photocellPin0);
photocellReading1 = analogRead(photocellPin1);
photocellReading2 = analogRead(photocellPin2);
photocellReading3 = analogRead(photocellPin3);
Serial.print("Analog reading0 = ");
Serial.println(photocellReading0); // the raw analog reading
Serial.print("Analog reading1 = ");
Serial.println(photocellReading1); // the raw analog reading
Serial.print("Analog reading2 = ");
Serial.println(photocellReading2); // the raw analog reading
Serial.print("Analog reading3 = ");
Serial.println(photocellReading3); // the raw analog reading
delay(500);
}
Serial.print result on the master
Analog reading00 = 255
Analog reading01 = -1
Analog reading02 = -1
Analog reading03 = -1
Analog reading10 = 255
Analog reading11 = -1
Analog reading12 = -1
Analog reading13 = -1
Analog reading20 = 255
Analog reading21 = -1
Analog reading22 = -1
Analog reading23 = -1
Analog reading30 = 255
Analog reading31 = -1
Analog reading32 = -1
Analog reading33 = -1
As you can see, not the results I was hoping for. So if you guys could please enlighten me, it would be absolutely brilliant. I am about ready to throw in the towel after frustrations after frustrations after frustrations with not getting what I expected.
Thank you very much for your time and effort. Much appreciated.
Ryan