Can anyone please help me to edit the below codes for the sensor (in the link); Arduino; Ammonia sensor; serial communication codes

#include "SoftwareSerial.h"

SoftwareSerial NH3_Serial(12,13); //Sets up a virtual serial port
//Using pin 12 for Rx and pin 13 for Tx

byte readNH3 = {0xFF, 0X01, 0X07, 0X00, 0X00, 0X00, 0X00, 0X00, 0X07}; //Command packet to read NH3 (see app note)
byte response = {0, 0, 0, 0, 0, 0, 0, 0, 0}; //create an array to store the response

//multiplier for value. default is 1. set to 3 for K-30 3% and 10 for K-33 ICB
int valMultiplier = 0.1;

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600); //Opens the main serial port to communicate with the computer
NH3_Serial.begin(9600); //Opens the virtual serial port with a baud of 9600

}

void loop()
{
sendRequest(readNH3);
unsigned long valNH3 = getValue(response);
Serial.print("NH3 ppm = ");
Serial.println(valNH3);
delay(2000);

}

void sendRequest(byte packet)
{
while (!NH3_Serial.available()) //keep sending request until we start to get a response
{
NH3_Serial.write(readNH3, 8);
delay(50);
}

int timeout = 0; //set a timeoute counter
while (NH3_Serial.available() < 8 ) //Wait to get a 8 byte response

{
timeout++;
if (timeout > 10) //if it takes to long there was probably an error
{
while (NH3_Serial.available()) //flush whatever we have
NH3_Serial.read();

  break;                        //exit and try again
}
delay(50);

}

for (int i = 0; i < 8; i++)
{
response[i] = NH3_Serial.read();
}
}

unsigned long getValue(byte packet)
{
int high = packet[3]; //high byte for value is 3th byte in packet in the packet
int low = packet[4]; //low byte for value is 4th byte in the packet

unsigned long val = high * 256 + low; //Combine high byte and low byte with this formula to get value
return val * valMultiplier;
}

Gas Sensor Module-JXCT -- Born for IoT (jxct-iot.com)

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi,
Are these codes you have written yourself or cut and pasted from another coder?

Tom... :smiley: :+1: :coffee: :australia: