Arduino Calculator -part 2

So, a few days ago I made a post about my calculator made with arduino. Supposedelly, it was meant to do the math for you, by means of typing it on the Serial Monitor. But it wasn't working.

Original posts and development of the calculator: Arduino Calculator not working - Project Guidance - Arduino Forum

I eventually, with some help, made some bit of code that could actually recognise the data and make the operation. But, since that post is kinda dead, and I have other problems to adress, I decided to make a whole new post.

Here is the partial-working code:

int numData = 32;
boolean newData = false;
char dataReceived[32];
char tempData[32];
long number1 = 0;
char calSignal;
long number2 = 0;
long result = 0;

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

void loop() {
  recvWithEndMarker();
  if (newData = true) {
    strcpy(tempData, dataReceived);
    parseData();
    doMaths();
    clearData();
    newData = false;
  }
}

void recvWithEndMarker() {
  //Made by Robin2
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            dataReceived[ndx] = rc;
            ndx++;
            if (ndx >= numData) {
                ndx = numData - 1;
            }
        }
        else {
            dataReceived[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void parseData() {
  char * strtokIndex;
  strtokIndex = strtok(tempData, " ");
  number1 = atol(strtokIndex);
  Serial.println(number1);
  strtokIndex = strtok(NULL, " ");
  strcpy(calSignal, strtokIndex);
  Serial.println(calSignal);
  strtokIndex = strtok(NULL, " ");
  number2 = atol(strtokIndex);
  Serial.println(number2);
}

void doMaths() {
  if (calSignal == '+') {
    result = number1 + number2;
  }
  if (calSignal == '-') {
    result = number1 - number2;
  }
  if (calSignal == '*') {
    result = number1 * number2;
  }
  if (calSignal == '/') {
    result = number1 / number2;
  }
  Serial.print("Result is = ");
  Serial.println(result);
}

void clearData() {
  number1 = '!';
  calSignal = '!';
  number2 = '!';
}

As it stands now, I have two main problems:

  • It dosen't stop to print the results to Serial Monitor
  • It can only recognise data once, and then it won't accept anything more

Any ideas of why is this happening?

Your code does not compile :wink: It barks at

  strcpy(calSignal, strtokIndex);

calSignal is a single char, not a (pointer to a) array.
Change it to

calSignal = *strtokIndex;

And the code permanently prints because of

  if (newData = true)