Start a new topic

Arduino/Nextion Floating Point Calculator

Hey Guys

 

There a several Arduino 'Calculator' projects out there. And the question has been asked lots of times "How do I work with floating point numbers on Nextion" or words to that effect.

 

Short answer "You can't!", but your Arduino can. However there is nothing in the library functions to do such tasks easily.

 

Here's a little something you might find educational, and perhaps a way of implementing such tasks in your Arduino/Nextion projects.

 

If you look at some of the calculator projects out there, most are based on the old school 2 line LCD's, and the code is quite complex for what it does, plus you need to hook up a keypad as well.

 

Nextion can greatly simplify the hardware requirements, so the rest should be easy :-)

 

I threw together some extremely simple code and a relatively simple HMI to come up with the Arduino/Nextion floating point calculator. All the comm's are done in plain old ascii, nothing extraordinary here :-).

 

For library users there is a technique included, to switch between nexListen mode and calc mode just using a button to invert 2 bools, this ensures that the code doesn't interfere with normal library function.

 

Yes I know this demo itself serves little useful purpose, but for the die hard tinkerers that must get that floating point calculation done or perhaps grab the sin of x value or whatever via Nextion input, this is a start. There are a few bugs, which I'm not going to squash, the point is in the how to. Have fun.

 

Steve.

 

Known issues are: 

1. first atof() use is failing, probably due to stray data collected into the numArray string. 

2. If you 'CE' the decimal point you can't re-enter it in the same operation - requires more code than worth the effort when 'C' to start over will do just as well.


image


 

float firstNumber = 0; //stores first number
float secondNumber = 0;  //stores second number
float result = 0;

uint8_t opType = 0;
uint8_t count = 0;
char numArray[20];
bool calc = false;
bool nexListen = true;


void printResult () {

  Serial.print("t0.txt=\"");
  Serial.print(result, 3);
  Serial.print("\x22\xff\xff\xff");  
}

void doCalc () {

  switch (opType) {//You could add any mathematical operation to the list :-)
    case 0:
      result = firstNumber + secondNumber;
      printResult();
      break;
    case 1:
      result = firstNumber - secondNumber;
      printResult();
      break;
    case 2:
      result = firstNumber * secondNumber;
      printResult();
      break;
    case 3:
      result = firstNumber / secondNumber;
      printResult();
      break;
  }
}


void setup() {
  Serial.begin(115200);
  numArray[0] = '\0';
  while (Serial.available()) Serial.read();
  //Serial.print("\nSerial Input Calculator Test\n\n");
}


void loop() {

  if (calc) {

    while (Serial.available( )) {

      char inChar = Serial.read();
      numArray [count ++] = inChar;
      if (inChar == '?') {
        opType = numArray [count - 2] - 48;
        numArray[count - 2] = '\0';
        firstNumber = atof (numArray);
        numArray[0] = '\0';
        count = 0;
      }
	  
      if (inChar == '\r') {
        numArray[count - 1] = '\0';
        secondNumber = atof (numArray);
        numArray[0] = '\0';
        count = 0;		
        doCalc();
      }
 
      if (inChar == '$') {
        calc = false;
        nexListen = true;
        Serial.print("b0.bco=63488\xff\xff\xff");//bool switch confirmed (On/Off color change)
      }
    }
  }
  if (nexListen) {
    //NexListen normally runs here
    while (Serial.available( )) {//NexListen code can watch for the '$' and switch bools instead of the following
      char inChar = Serial.read();
      if (inChar == '$') {
        nexListen = false;
        calc = true;
        Serial.print("b0.bco=2016\xff\xff\xff");//bool switch confirmed (On/Off color change)
      }
    }
  }
}





 

HMI

There is even a Nextion side logic 4 op calculator in Stump the Chump. The code was given to the challenger on solving and is available to the other challengers when Stump the Chump ends Mar 31 =)

Yeah,


I'm pointing (excuse the pun) more at users who want to move floating point math around the Arduino/Nextion combo, the calculator is just a platform to show how easy it is?


I'll bet when Gerry's code is released, it's a pretty complex beast for most beginners.

 mhmhmhmh I never thought about my "beasty code" :-)


Even I have not seen Gerry's code


But I know mine was a fight to get 4 solid digits of accuracy.

My Nextion side float used two integers (whole, fractional) to implement,

so I agree not really a beginners topic.

If you wanted to get creative with the above, then there is no reason you couldn't call some really complex algorithm to do a bunch of fancy line drawing or whatever. You could probably do the analogue (digital) clock on an ESP8266 with real(near)time NPT in not too many lines of code after you've done all the WiFi stuff =) 

For sure it could do analog clock .. and thanks for contribution BTW


Some code revisions with basic trig function examples added.

image

 

ino
HMI
Login or Signup to post a comment