0
votes

This sketch works on Arduino Uno but does not to work on Arduino Mega 2560. The same connections. The same logic konwerter. To be sure I have tried all TX and RX pins on the board 0, 1, 14-21 and I did not find any solution. So I think the sketch has issue I cant resolve. Sketch upload is succesful. No errors.

#include <stdio.h>
#include <PMS.h>
#define N 23

char linia1[16], linia2[16];
String sumPM25, sumPM10; 
unsigned char bufor [N];
int PM25 = 10, PM10 = 10;
int wartoscPM25(unsigned char *thebuf);
int wartoscPM10(unsigned char *thebuf);
char sprawdzLancuch(unsigned char *thebuf, char leng);
int a=0;
void setup(){
   Serial.begin(9600);
}

void loop(){
  if(Serial.find(0x42))    
    Serial.readBytes(bufor,N);

     if(bufor[0] == 0x4d){
      if(sprawdzLancuch(bufor,N)){  
        PM25=wartoscPM25(bufor);
        PM10=wartoscPM10(bufor);  
     }
    }

   sprintf(linia1,"%d",PM25);
   Serial.print(linia1);
   sprintf(linia2,"%d",PM10);
   Serial.println(linia2);
   delay(1000);
}

int wartoscPM25(unsigned char *buf) 
{
  int PM25v;
  PM25v=((buf[11]<<8) + buf[12]);  
  return PM25v;
}

int wartoscPM10(unsigned char *buf) 
{
  int PM10v;
  PM10v=((buf[13]<<8) + buf[14]); 
  return PM10v;
}

bool sprawdzLancuch(unsigned char *buf, int dlugosc) 
{  
  bool flaga=0;
  int suma=0;

  for(int i=0; i<(dlugosc-2); i++){
  suma+=buf[i];
  }
 suma=suma + 0x42;

  if(suma == ((buf[dlugosc-2]<<8)+buf[dlugosc-1]))  
  {
    suma = 0;
    flaga = 1;
  }
  return flaga;
}

I use logic converter on both Uno and Mega to connecte PMS3003.

1

1 Answers

0
votes

The serials are on the following pins.

Serial: 0 (RX) and 1 (TX);

Serial1: 19 (RX) and 18 (TX);

Serial2: 17 (RX) and 16 (TX);

Serial3: 15 (RX) and 14 (TX).

Since Serial is also connected to the USB, you should probably connect the deice up to one of the other ports and then use the corresponding SerialX to communicate to it.

Also verify you have the RX and TX pins between the Arduino and the device (and the logic converters) hooked up correctly.

Sometimes datasheets are show the Rx to a device meaning Rx from the host (Rx into the device), and sometimes Rx to the host (ie TX from the device).