0

good afternoon I'm working on: SEND & RECEIVED one integer from Master(NANO) to Slave(UNO). Squematic without DHT's sensors I'll get temperature, humidity and heat index from DHT11 ID01, DHT11 ID02 and DHT22 ID03, so I have created a Matrix

  //  ******************************************************
  //  * Data Matrix from SENSORS DHT's 11 or 22
  //  ******************************************************
  //  * ID  | temperature  | humidity |      heat index    |
  //  * 1   |      1       |    2     |          3         |
  //  * 2   |      1       |    2     |          3         |
  //  ******************************************************
  //  * 3   |      1       |    2     |          3         |
  //  ******************************************************
  //  *        temperature from Sensor ID=1 -> 11
  //  *           humidity from Sensor ID=1 -> 12
  //  *         heat index from Sensor ID=1 -> 13
  //  ******************************************************

  //  *********************************
  //  * SEND TO SLAVE
  //  *********************************
  //  * ID=1 -> 11, 12, 13
  //  * ID=2 -> 21, 22, 23
  //  * ID=3 -> 31, 32, 33


  //  **************************************
  //  * RECEIVED FROM SLAVE
  //  **************************************
volatile unsigned int uitID1, uitID2;             //  valor float*10 de la Temperatura [°C], registrada por el sensor ID
volatile unsigned int uihID1, uihID2;             //  valor float*10 de la Humedad [%], registrada por el sensor ID
volatile unsigned int uihicID1, uihicID2;         //  valor float*10 de la Sensación Térmica [°C], calculada para el sensor ID

In order to get the temperature from DHT's, you must have the DHT's modules or you could generate a random int, for my point of view is the same, and on second case you only need two differents ARDUINO’s and six wires GND, D10, D11, D12 & D13, plus (+) if you use NANO as MASTER, and UNO as SLAVE, using SPI.

  1. I random generate an INTEGER < 250 [11, 12 or 13], I think it is does not necesary to separate into TWO bytes(Low and High), send from Master side.

    uitID = uint16_t(random(11,14));
    //  or you could send an Integer
    //  iID = byte(random(11,14));          
    //    masterRECEIVED=SPI.transfer(iID);    
    masterRECEIVED=SPI.transfer16(uitID);    
    //  I tried to generate sensor ID=11 or 12 or 13 ...
    //  In this step no matter what I send from Master ..., 
    //  I want received
    //  sequential Unsigned INTEGER from 250 to 270
    Serial.print(F("SEND >> "));
    Serial.println(uitID);
    
  2. From Slave side, if I "received COMMAND==11" I send at SPDR a sequential integer from [250 to 270], in order to view that LOW & HIGH bytes are "sending" and "receiving"

  3. If I received COMMAND=12 I send at SPDR a sequential integer from [1350 to 1320]
  4. If I received COMMAND=13, I do not send anything (at least for the moment ...)

Output Serial Monitor COMM14

SlaveTransfer01.ino

22:01:50.533 -> Version N°: 01
22:01:50.566 -> Last modification: 09/10/2019 21:00
22:01:53.402 -> 
22:01:53.402 -> << 12, >> L=E2, H=4
22:01:56.502 -> << 12, >> L=E1, H=4
22:01:59.520 -> << 13, >> 
22:02:02.577 -> << 13, >>
22:02:05.628 -> << 12, >> L=E0, H=4
22:02:08.637 -> << 13, >>
22:02:11.682 -> << 11, >> L=FA, H=0
22:02:14.728 -> << 13, >>
22:02:17.772 -> << 13, >>
22:02:20.819 -> << 12, >> L=DF, H=4
22:02:23.867 -> << 11, >> L=FB, H=0

Up to this point, all works OK.


So I need your help for modify the master & slave codes that could get this SCHEMATIC problem:

  1. On MASTER, generate a random number [11,14]= COMMAND, send this byte (e.g. N° 11)
  2. Send to SLAVE -> 11, and receive on MASTER -> 00 FA = the answer of the COMMAND 11, wait 2 or 3 seconds,
  3. On MASTER, generate a random number, (e.g N° 12)= NEW COMMAND
  4. Send to SLAVE -> 12, and receive on MASTER -> 04 E2 = the answer of COMMAND 12, wait 2 or 3 seconds,
  5. generate a random [11,14], send this byte, and received 00 FB (00 FA ++) if the number generated was 11, or 04 E1 (00 E2--) if was 12, or “nothing” (00 00) if was 13 …

those inside a loop()


I'm trying with Floats, struct & union, using SPI.transfer16(unsigned int), SPI.transfer(byte) ... but I couldn't be able to get the correct code, that works on a loop as I described in paragraph above.

I understood that in a squematic mode, the Master code may be similar to:

//  Use random generator
//  volatile byte randNumber = random(11,14);
//   suposed you’ll get 11
M: //1st call 
 //SEND >> 11    
 SPI.transfer(11);
S: //GET 11 << 
// At ISR (SPI_STC_vect)
command = SPDR;
switch (command){ 
    case 11: 
         // be carefull
         // TWO bytes ¡!!!!!!!!!!!!!
        SPDR = seqNumber & 0xff;
        SPDR = (seqNumber >> 8)& 0xff;
         break;
    case 12: 
         // be carefull
         // TWO bytes ¡!!!!!!!!!!!!!
        SPDR = seqNumber1 & 0xff;
        SPDR = (seqNumber1 >> 8)& 0xff;
         break;
    case 13:case 21:case 22:case 23:
         break;
    default:
         break;   
 } //end switch
M: //2nd call, get LOW byte
mLow = SPI.transfer(11);
// 3th call, get HIGH byte
mHigh= SPI.transfer(11);
// Loop with new random generator
//  volatile byte randNumber = random(11,14);

Here where I'm stuck, so please Could you help me with the code ?

volatile byte randNumber = random(11,14);
// 11, 12 or 13 ONE byte
dataID=randNumber;
Serial.print(F("mSEND= "));
Serial.print(dataID);

//  FIRST CALL to SPI.transfer()
SPI.transfer(dataID);    
uint8_t isOK=sprintf(buff,"%.4X",mSend);
Serial.print(F(", "));
Serial.print(buff);

Thank you very much for your time, I'll hope receive a brief code, make some changes and of course, upload into the forum.

Best regards, Adrian (Spanish spoken, be patience!!!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.