Pin 16 TX & 17 RX dont receive data in Arduino Mega2560

Hello colleagues, I am working on the design of a sensor that can send the voltage, time and date data from the device located on a solar panel to the computer. My intention is that this device is activated when I send a start signal from the computer, which means that the programming will be in an infinite loop until a signal arrives.

The programming is 98% complete if it were not for the fact that when I send the signal from the computer to the sensor, the sensor does not exit the loop.

It should be noted that to carry out the wireless connection, I use an Xbee antenna, from Digi. The strange thing is that I had already used this programming on my arduino Mega and it had run.

Something interesting to note, is that the sensor has an ATMega328, and that the programming I am carrying out is done in Arduino Mega2560, this should not be a problem but it is not too much to say.

So the idea is to check that this programming can already get out of the loop, then load it to the ATMega and test with the circuit that I have already assembled, nothing else is expected to put the ATMega328 with the programming working at 100.

Xbee has a software called XCTU, with this software I already checked that the antennas are working and if they are, so I can rule out that it is a problem with the antennas. I sincerely feel that it is a programming problem which I can't figure out.

This is the sketch

//--------------------------------------LIBRERIAS-----------------------------------------------

#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SafeString.h>
#include <SoftwareSerial.h>

//--------------------------------------INICIALIZACION DE VARIABLES------------------------------

//DATALOGGER
const int chipSelect = 4;

//ADC
const int ADCpin = A1;
float voltPanel;
String sfVolts = "";

//XBEE
SoftwareSerial XBeeSerial(17, 16); //(Rx, TX)
uint8_t Rx_nextByte;
#define START_DELIMITER 0x7E

//--------------------------------------ENCABEZADO DE TRAMA DE ENVIO------------------------------

byte packet1[] = {0x7E, 0x00, 0x27}; //Byte delimitador y Longitud de trama // variar por el voltaje
byte packet2[] = {0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0xD7, 0xAE, 0xAD, 0xFF, 0xFE, 0x00, 0x00,}; //Id de trama, tipo de trama, direccion de 64bits, opciones y numero de brincos.
// xx.xx,00:11:46,05/01/2021 == 25 chars
byte fullPacket[43];// (Delimitador + longitud )+ (tipo + direccion)+ voltage + fecha + checksum = 37
//  [3 + 14 + 4 + 19 + 1]
char buffer [27]; // 19 la fecha  +1  '\0', 25 bytes de tamaño // cambiar porque hay voltaje
#define DEBUG SafeString::Output

//--------------------------------------SETUP------------------------------
void setup() {
  Serial.begin(9600);
  delay(100);
  XBeeSerial.begin(9600);
  delay(100);
  Serial.println();
  Serial.println("CODIGO PRINCIPAL");
  Serial.println("-------------------");

  SafeString::setOutput(Serial);
}

//--------------------------------------LOOP------------------------------

void loop() {

//--------------------------------------LOOP MIENTRAS ESPERA EL MENSAJE DE ARRANQUE------------------------------
 
  Serial.println("Espera a recibir señal");
  while (XBeeSerial.available()) {
    Serial.println("Espera a recibir seña222222l");
    Rx_nextByte = XBeeSerial.read();
    if (Rx_nextByte == START_DELIMITER) {
      
      
      //Inicia a mandar datos Si el primer Byte que recive de la PC es un 0x7E.  
      Serial.println("Señal recibida");

            
//--------------------------------------SE INICIA TOMANDO LA HORA FECHA ------------------------------    

      //RTC
      tmElements_t tm;
      if (RTC.read(tm)) {
        sprintf(buffer, "%02d:%02d:%02d,%02d/%02d/%04d", tm.Hour , tm.Minute, tm.Second, tm.Day, tm.Month, tmYearToCalendar(tm.Year));
        Serial.println(buffer); // Compruebo la fecha y hora actual.
      }
//--------------------------------------SE COMIENZA A FORMAR Y CONTAR LOS CARACTERES DEL ENCABEZADO ------------------------------  
        size_t idx = 0;
        memmove(fullPacket + idx, packet1, sizeof(packet1));
        idx += sizeof(packet1);
        memmove(fullPacket + idx, packet2, sizeof(packet2));
        idx += sizeof(packet2);
        if ((idx + 6 + 19 + 1) > sizeof(fullPacket)) {
          Serial.println(F("1 Paquete completo esta mal."));
        }

//--------------------------------------SE TOMAN 20 MUESTRAS DEL VOLTAJE DEL SENSOR EN EL ADC ------------------------------  
        
        sfVolts = ""; // Voltage xx.xx == 5 y coma 1 == 6     
        const int SAMPLES = 20;
        float fv = (float(analogRead(ADCpin)) * 4.7 / 1023) * 6 + 10;
        fv = 0;
        for (int i = 0; i < SAMPLES; i++) {
          fv = fv + ((float(analogRead(ADCpin)) * 4.7 / 1023) * 6 + 10);
        }
        fv = fv / SAMPLES;
        Serial.println(fv);
        sfVolts = String(fv)+',';

//--------------------------------------SE EMPAQUETA EL DATO DEL ADC EN LA TRAMA------------------------------          
        
        memmove(fullPacket + idx, sfVolts.c_str(), 6); // Siempre van a ser 6 caracteres de largo
        idx += 6;
        if ((idx + 19 + 1) > sizeof(fullPacket)) {
            Serial.println(F("2 Paquete completo esta mal."));
        }
        
//--------------------------------------SE EMPAQUETA EL DATO DEL RTC EN LA TRAMA------------------------------          
        
        memmove(fullPacket + idx, buffer, 19);
        idx += 19;
        //Dejo un ultimo byte para tener el Checksum.
        if ((idx + 1) != sizeof(fullPacket)) {
            Serial.println(F("3 Paquete completo esta mal."));
        }
        
//--------------------------------------SE DA EL FORMATO A LA TRAMA D DATOS Y SUS CARACTERES SE CONVIERTEN A HEX------------------------------          
        int chksum = 0;
        for (size_t i = 3; i < idx; i++) { // comienza despues del tamaño dela trama
          chksum += fullPacket[i];
        }
        chksum = (0xff & chksum);
        chksum = (0xff) & (0xff - chksum);
        fullPacket[idx] = chksum;
        idx++; // El tamao total de la trama
        if (idx != sizeof(fullPacket)) {
          Serial.println(F("fullPacket size error"));
        } 
        size_t printSize = idx * 3; // ..<space>
  
         XBeeSerial.write (fullPacket , idx);
         Serial.println();//Se comprueba que el formato de la Trama API.
        
//--------------------------------------SE GUARDAN LOS DATOS EL EL DATALOGGER------------------------------          
        String dataString = "";
        dataString += String(sfVolts);
        dataString += String(buffer);
        File dataFile = SD.open("TensionDePanel.txt", FILE_WRITE);
        if (dataFile) {
          dataFile.println(dataString);
          dataFile.close();
        }
        }
//--------------------------------------SE ENVIA LA TRAMA------------------------------          
        
        //XBeeSerial.write (fullPacket[i], 3);//Se envia la trama del sensor a la PC.
        delay(100);
        }
      }

    
//--------------------------------------FUNCION QUE DA FORMATO A LA HORA Y FECHA------------------------------          
                   
void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

Why are you using SoftwareSerial on a Mega that has multiple Hardware UARTS?

Because initially I started working on programming with an arduino UNO, but my arduino UNO got screwed up or stopped working when I removed the microcontroller, trying to upload the programming to a microcontroller.

So I decided to use a MEGA because it was the one I had on hand at the time. Besides that it is just to test that the programming runs well, after that I convert the Mega to ISP and load the sketch to my ATmega328 microcontrollers.

According to the SS library page:

"Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)."

Pin 17 doesn't show up in the list of supported pins. Can you try one of the other pins listed for RX?

3 Likes

Workind dude. But working for Mega. 10 -> RX 9->TX. But 9 &10 for UNO are for crystall.