Checksum calculation for XBee/Writing on serial code won't compile

Hi,

I am trying to calculate a checksum to broadcast an API frame from my Arduino containing an analog measurement. Below you can find the code.

int sensorValue=0;
int sensorPin=A0;
byte checkSum = 0;
byte valueFrame=0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue=analogRead(sensorPin);
  valueFrame=byte(sensorValue/4, HEX);
  checkSum = 0xFF - (0x01+0x00+0xFF+0xFF+0x00+0x01+valueFrame);
  byte frame[] = {0x7E, 0x00, 0x07, 0x01, 0x00, 0xFF, 0xFF, 0x00, 0x01, valueFrame, checkSum};
  Serial.write(frame, sizeof(frame));
}

The compiling error is an "exit status 1" so I can't find out what the problem is.

Can someone help me ?

Thanks in advance.

Sabre254

modified your code a bit where I thought the error could be could be coming from.

let us know how it goes! :slight_smile:

#define sensorPin A0
int sensorValue=0;
byte checkSum = 0;
byte valueFrame=0;

void setup() {
  Serial.begin(9600);
}

void loop() {
int temp;
  sensorValue=analogRead(sensorPin);
  valueFrame=map(sensorValue, 0, 1023, 0, 255); //bear in mind that you will be loosing resolution! you're going from 10 bit (ADC resolution) to 8 bits (byte type)! 
  temp = 0xFF - (0x01+0x00+0xFF+0xFF+0x00+0x01+valueFrame);
  checkSum = (byte) temp;
  byte frame[] = {0x7E, 0x00, 0x07, 0x01, 0x00, 0xFF, 0xFF, 0x00, 0x01, valueFrame, checkSum};
  Serial.write(frame, sizeof(frame));
}

hope that helps

Thank you for responding,

I just noticed the problem might lay somewhere else Even if I put an empty loop the code won't compile.
Any tips on that ?

Post the entire error message. You can get more information on the cause of the failure to compile if you enable verbose output while compiling. Go to File, Preferences and check the compilation box by Show verbose output during.

0x01+0x00+0xFF+0xFF+0x00+0x01 = 0, in byte arithmetic.