Hi,
I've deployed a master-slave system where: 1)the master triggers the slaves with a given value through wire.write 2)the slave receives this value and within a For loop performs some tasks sending a result to the master through wire.write 3) the master does a Wire.RequestFrom waiting for those bytes (15) and printing those values on a screen.
The master receives the values but those are not the ones sent by the slave (should be hexadecimal numbers: 0x01, 0x02, 0x03 and 0x04). Instead, the master receives the following values 0, 255, 255, ... , 255 (14 times 255).
I insert the codes below:
Master
const int buttonSel = 50;//button to Select options
const int buttonStart = 53; //button to Start option
const int buttonStop = 52; //button to Stop option
const int Marcia = 32; //Initiates Marcia of the TX module
const int PR17 = 22; //PR17 relay of the TX module
const int PR16 = 24; //PR16 relay of the TX module
const int PR15 = 26; //PR15 relay of the TX module
const int PR27 = 28; //PR27 relay of the TX module
const int PR26 = 30; //PR26 relay of the TX module
const int PR25 = 31; //PR25 relay of the TX module
const int PR11 = 33; //PR11 relay of the TX module
const int PR10 = 34; //PR10 relay of the TX module
const int PR9 = 35; //PR9 relay of the TX module
const int PR5 = 36; //PR5 relay of the TX module
const int PR6 = 37; //PR6 relay of the TX module
const int PR14 = 38; //PR7 relay of the TX module (It is PR7 in TX but PR14 in RX)
const int PR23 = 39; //PR23 relay of the TX module
const int PR22 = 40; //PR22 relay of the TX module
const int PR21 = 41; //PR21 relay of the TX module
int programCount = 0;//variable to move through the program
int delay_ = 500;
//int flagStart = 0;
int read;
int pass = 0; // integer to indicate if at least one pins doesn't pass 0-OK 1-FAIL
int Error = 0; // integer to indicate that there is an error during the checking 0-NO 1-YES
int x; int y; int z;
const byte SLAVE_ADDRESS = 42;
const byte MY_ADDRESS = 25;
int flag = 0;
int count = 0;
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
void setup() {
lcd.begin(20, 4);
Serial.begin (9600);
Wire.begin (25);
//Wire.onReceive(receiveEvent);
lcd.setCursor(0, 0);
programCount = 0;//start at the beginning
//pinmode and digitalWrite statements
lcd.print("Select");
}
void loop()
{
switch (programCount)
{
case 0: // This case option remains the "Select" option idle
if (digitalRead(buttonSel) == HIGH)
{
programCount = 1;
}
break;
case 1:
static const uint8_t arraypin[] = {PR5, PR6, PR9, PR10, PR11, PR14, PR15, PR16, PR17, PR21, PR22, PR23, PR25, PR26, PR27};
x = 15;
lcd.clear();
lcd.print("E");
delay(delay_);
if (digitalRead(buttonSel) == HIGH)
{
programCount = 2;
}
if (digitalRead(buttonStart) == HIGH)
{
lcd.clear();
lcd.print("Start ");
digitalWrite(Marcia, LOW);
delay(2000);
digitalWrite(Marcia, HIGH);
for (int i = 0; i < x; i++)
{
digitalWrite(arraypin[i], LOW);
Wire.beginTransmission (SLAVE_ADDRESS);
Wire.write (y = arraypin[i]);
Serial.println("Transmiting from Master");
Serial.println(y);
Serial.println("Receiving from Slave");
int n = Wire.requestFrom(42, 15);
if (n == 15) {
flag = 1;
}
delay(15000);
Serial.println(n);
Serial.println(flag);
if (n == 15 && flag == 1)
{
Serial.println("yes");
for (int i = 0; i < x; i++)
{
byte z = Wire.read();
delay(100);
Serial.println(z, DEC);
}
//delay(15000);
flag = 0;
}
Wire.endTransmission ();
digitalWrite(arraypin[i], HIGH);
}
}
if (digitalRead(buttonStop) == HIGH)
{
lcd.clear();
lcd.print("Stop Edilbox");
digitalWrite(Marcia, LOW);
delay(delay_);
}
break;
case 2:
break;
case 3:
break;
}
}
Slave
const int STOP = 8; //STOP of the RX module
const int CLAX = 9; //CLAX of the RX module
const int START = 10; //START of the RX module
const int LAMP = 11; //LAMP of the RX module
const int PR17_NO = 37; const int PR17_NC = 36; //PR17 relay of the TX module
const int PR16_NO = 39; const int PR16_NC = 38; //PR16 relay of the TX module
const int PR15_NO = 41; const int PR15_NC = 40; //PR15 relay of the TX module
const int PR27_NO = 25; const int PR27_NC = 24; //PR27 relay of the TX module
const int PR26_NO = 27; const int PR26_NC = 26; //PR26 relay of the TX module
const int PR25_NO = 29; const int PR25_NC = 28; //PR25 relay of the TX module
const int PR11_NO = 45; const int PR11_NC = 44; //PR11 relay of the TX module
const int PR10_NO = 47; const int PR10_NC = 46; //PR10 relay of the TX module
const int PR9_NO = 49; const int PR9_NC = 48; //PR9 relay of the TX module
const int PR5_NO = 53; const int PR5_NC = 52; //PR5 relay of the TX module
const int PR6_NO = 51; const int PR6_NC = 50; //PR6 relay of the TX module
const int PR14_NO = 43; const int PR14_NC = 42; //PR7 relay of the TX module
const int PR23_NO = 31; const int PR23_NC = 30; //PR23 relay of the TX module
const int PR22_NO = 33; const int PR22_NC = 32; //PR22 relay of the TX module
const int PR21_NO = 35; const int PR21_NC = 34; //PR21 relay of the TX module
//static const uint8_t arraypin[] = {PR5, PR6, PR9, PR10, PR11, PR14, PR15, PR16, PR17, PR21, PR22, PR23, PR25, PR26, PR27};
int delay_ = 700;
int x; int flag = 0; int y;
const byte MY_ADDRESS = 42;
const byte MASTER_ADDRESS = 25;
//char PIN[] = "pin";
int pin = 0;
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
void setup() {
Wire.begin (MY_ADDRESS);
lcd.begin(20, 4);
Serial.begin (9600);
lcd.setCursor(0, 0);
Wire.onReceive(receiveEvent);
// pinmode statements
}
void loop() {
//delay(delay_);
if (flag == 1)
{
if (y == 36) {
pin = 0; check(pin);
} //PR5 relay of the RX module
else if (y == 37) {
pin = 1; check(pin);
}
else if (y == 35) {
pin = 2; check(pin);
}
else if (y == 34) {
pin = 3; check(pin);
}
else if (y == 33) {
pin = 4; check(pin);
}
else if (y == 38) {
pin = 5; check(pin);
}
else if (y == 26) {
pin = 6; check(pin);
}
else if (y == 24) {
pin = 7; check(pin);
}
else if (y == 22) {
pin = 8; check(pin);
}
else if (y == 41) {
pin = 9; check(pin);
}
else if (y == 40) {
pin = 10; check(pin);
}
else if (y == 39) {
pin = 11; check(pin);
}
else if (y == 31) {
pin = 12; check(pin);
}
else if (y == 30) {
pin = 13; check(pin);
}
else {
pin = 14; check(pin);
}
}
}
void receiveEvent(int howMany) {
y = Wire.read();
flag = 1;
}
void check (int pin)
{
Wire.beginTransmission(25);
static const uint8_t arraypin_NO[] = {PR5_NO, PR6_NO, PR9_NO, PR10_NO, PR11_NO, PR14_NO, PR15_NO, PR16_NO, PR17_NO, PR21_NO, PR22_NO, PR23_NO, PR25_NO, PR26_NO, PR27_NO};
static const uint8_t arraypin_NC[] = {PR5_NC, PR6_NC, PR9_NC, PR10_NC, PR11_NC, PR14_NC, PR15_NC, PR16_NC, PR17_NC, PR21_NC, PR22_NC, PR23_NC, PR25_NC, PR26_NC, PR27_NC};
int z = 15;
Serial.print("PIN"); Serial.println(pin);
for (int i = 0; i < z; i++)
{
if (i == pin)
{
if (digitalRead(arraypin_NO[i]) == 0 && digitalRead(arraypin_NC[i]) == 1)
{
Wire.write(0x01);
Serial.println("1");
}
else
{
Wire.write(0x02);
Serial.println("2");
}
delay(delay_);
}
else
{
if (digitalRead(arraypin_NO[i]) == 1 && digitalRead(arraypin_NC[i]) == 0)
{
Wire.write(0x03);
Serial.println("3");
}
else
{
Wire.write(0x04);
Serial.println("4");
}
delay(delay_);
}
}
flag=0;
Wire.endTransmission();
}
What I'm missing? Thanks