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.