0

In my recent project, I'm working with Arduino with JSON.

I made software on JSON. Now, I want to send data from this software to my Arduino. Serial communication is done between Arduino and JSON. But somehow, I can't able to get proper data from JSON to my Arduino. So, I google out and find library named ArduinoJson. And here, I found out example code. So, I tried out that code and modified based on my requirement.

Here, below is my code.

Now, Software sends me value. And I split it out into three variable MAP_Json, TPS_Json, and LOAD_Json. In Arduino side, I have already the value of MAP, TPS, and LOAD.

So, now my main intention is to compare MAP_Json with MAP & TPS_Json with TPS. If they are matched then LOAD_Json is added to LOAD. But, when I try it out, then LOAD_Json not added to LOAD. I don't understand why this happened?

 #include <ArduinoJson.h>

//----------------------------For MAP calculation----------------------------//
float MAP_MIN = 0.85;
float MAP_MAX = 1.90;

int LOAD_MIN_MAP[] = {1, 2};
int LOAD_MAX_MAP[] = {3, 4};

float mMAP[2] = {};
float yMAP[2] = {};

int i, j, a;
float MAP, mFinalMAP, yFinalMAP;

//----------------------------For TPS calculation----------------------------//
float TPS_MIN = 0.00;
float TPS_MAX = 5.00;

int LOAD_MIN_TPS[] = {1, 3};
int LOAD_MAX_TPS[] = {2, 4};

float mTPS[2] = {};
float yTPS[2] = {};

int k, l, b;
float TPS, mFinalTPS, yFinalTPS;

//----------------------------For LOAD calculation---------------------------//
int LOAD_MIN = 2;
int LOAD_MAX = 100;

float injTime_MIN_LOAD[] = {8.00, 9.06};
float injTime_MAX_LOAD[] = {10.60, 11.86};

float mLOAD[2] = {};
float yLOAD[2] = {};

int m, n, c;
float LOAD, mFinalLOAD, yFinalLOAD;

//------------------------------ Variable ---------------------------------//

float load;

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

void loop()
{
  //----------------------ARDUINO DATA-------------------------//

  //----------------------------MAP----------------------------//

  MAP = analogRead(A0) * (5.0 / 1023.0);

  for(a = 0; a < 2; a++)
  {
    mMAP[i] = (LOAD_MIN_MAP[a] - LOAD_MIN_MAP[a]) / (MAP_MAX - MAP_MIN);
    yMAP[j] = mMAP[i] * (MAP - MAP_MIN) + LOAD_MIN_MAP[a];
  }

  //----------------------------TPS----------------------------//

  TPS = analogRead(A1) * (5.0 / 1023.0);

  for(b = 0; b < 2; b++)
  {
    mTPS[k] = (LOAD_MAX_TPS[b] - LOAD_MIN_TPS[b]) / (TPS_MAX - TPS_MIN);
    yTPS[l] = mTPS[k] * (TPS - TPS_MIN) + LOAD_MIN_TPS[b];
  }

  //----------------TPS/MAP CALLIBRATION / LOAD----------------//

  mFinalTPS = (yMAP[1] - yMAP[0]) / (TPS_MAX - TPS_MIN);
  yFinalTPS = mFinalTPS * (TPS - TPS_MIN) + yMAP[0];

  mFinalMAP = (yTPS[1] - yTPS[0]) / (MAP_MAX - MAP_MIN);
  yFinalMAP = mFinalMAP * (MAP - MAP_MIN) + yTPS[0];

  LOAD = (yFinalTPS + yFinalMAP) / 2;

  Serial.print("T : ");
  Serial.print(TPS);
  Serial.print("\t");
  Serial.print("M : ");
  Serial.print(MAP);
  Serial.print("\t");
  Serial.print("L : ");
  Serial.print(LOAD);
  Serial.print("\t");

  while(!Serial)
  {
    // wait serial port initialization
  }

  StaticJsonBuffer<200> jsonBuffer;

  char json[] = "{\"TPS\":1.67, \"MAP\":1.83,\"LOAD\":30}"; // JSON DATA

  JsonObject& root = jsonBuffer.parseObject(json);

  // Test if parsing succeeds.
  if(!root.success())
  {
    Serial.println("parseObject() failed");
    return;
  }

  // Splitted JSON DATA

  float TPS_Json = root["TPS"];
  float MAP_Json = root["MAP"];
  float LOAD_Json = root["LOAD"];

  Serial.print("T_J : ");
  Serial.print(TPS_Json);
  Serial.print("\t");
  Serial.print("M_J : ");
  Serial.print(MAP_Json);
  Serial.print("\t");
  Serial.print("L_J : ");
  Serial.print(LOAD_Json);
  Serial.print("\t");

  //Compare JSON DATA and ARDUINO DATA

  if(TPS_Json == TPS && MAP_Json == MAP) //If matched
  {
    load = LOAD_Json + LOAD;            // Add LOAD_Json and LOAD
  }

  Serial.print("Final Load :");
  Serial.print(load);
  Serial.print("\n");
}

EDIT 1: Improve question formatting

EDIT 2: Upload Complete code

11
  • Downvoter, please tell me what is wrong?
    – Hasan
    May 2, 2017 at 9:01
  • 1
    What output do you expect to get, and what output do you actually get? Just saying "I can't do X" doesn't help us work out what you expect to happen and what is happening.
    – Majenko
    May 2, 2017 at 9:59
  • JSON isn't javascript its a way of representing data that uses a syntax derived from JS. If you can't write your own parsing code then look at some of the libraries available. I agree with the down vote, its not a well phrased question. May 2, 2017 at 10:49
  • @Majenko ok...let me update question in more detail.
    – Hasan
    May 2, 2017 at 11:14
  • @CodeGorilla Ok...I think I have to clear all things.
    – Hasan
    May 2, 2017 at 11:15

1 Answer 1

2

Comparison of floating point values is nigh on impossible. They aren't as accurate as you may think anyway, and two values have to be precisely the same to be considered equal - not something that is easy to achieve (23.4893782 != 23.4892480).

Instead you should either round them to a specific precision, or take the difference between the two values and decide if they are "close enough".

E.g.:

float diff = abs(TPS - TPS_Json);
if (diff < 0.5) {
    // do something
}
1
  • Ok...Got it. Let me try out this. Thank you for this.
    – Hasan
    May 2, 2017 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.