2

I have an analog sensor which reads values 0-5v, -75 degrees to 75 degrees. Now I have converted to +75 degrees and -75 degrees format depend on the voltage.

From 2.5v to 5v data is read properly (i.e 0-75 degree), but from 0-2.5v they are negative values -75 degree to 0. In the below image array holding register is reading 65493 and 65491 which are negative value. How do I make it readable format? Since my code is very big, only part of code is pasted.

#define ID   1

int Wind_Speed=30;
int Wind_Kmph=67;
signed int tracker_des_angle=-45;
signed int tracker_actual_pos=-43;
Modbus slave(ID, 0, 0);
boolean led;
int8_t state = 0;
unsigned long tempus;
// data array for modbus network sharing
uint16_t au16data[14];


void setup() {

  // start communication
  slave.begin( 9600 );
  rtcSetup();

  tempus = millis() + 100;
  digitalWrite(13, HIGH );
}


void loop() {
  // poll messages
  // blink led pin on each valid message
  state = slave.poll( au16data, 14);
  if (state > 4) {
    tempus = millis() + 50;
    digitalWrite(13, HIGH);
  }
  if (millis() > tempus) digitalWrite(13, LOW ); 
  calcTime();
  convertUTCtoLocal();

  au16data[0]=local_year;
  au16data[1]=local_month;
  au16data[2]=local_day;
  au16data[3]=wkDay;
  au16data[4]=local_h;
  au16data[5]=local_m;
  au16data[6]=local_s;
  au16data[7]=Wind_Speed;
  au16data[8]=Wind_Kmph;
  au16data[9]=tracker_des_angle;
  au16data[10]=tracker_actual_pos;

  au16data[11] = slave.getInCnt();
  au16data[12] = slave.getOutCnt();
  au16data[13] = slave.getErrCnt();
} 

Negtive value read

  • In arduino you have the map() arduino.cc/en/Reference/Map function, typically used to convert from ranges in analogReads. I miss in your code the place where you read your tracker_des_angle and how you convart it to your -+70º scale, can you post it please? – drodri Apr 10 '14 at 10:16
  • That looks like a problem in QModbus itself, or the way you use it. Check if you can setup these words as signed inside qmodbus. – jfpoilpret Apr 10 '14 at 10:52
  • @jfpoilpret i don't have those setting to convert it to readble format. I am thinking to check if no is negative multiply by -1. I know it wrong way. In hexadecimal , We can say if no is -ve subtract FF from No read. – AMPS Apr 10 '14 at 11:10
  • 1
    The point is: can qmodbus display signed value? If not, then you cant do it with qmodbus. If yes, then how but this is qmodbus question not an arduino issue. – jfpoilpret Apr 10 '14 at 12:02
  • You could use a bit to store and display the sign and always send the absolute value in the register. – jfpoilpret Apr 10 '14 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.