Arduino Calculator not working

So, let me throw in some backstory. I changed my computer for a new one and was checking all my sketches, just some routine check. Then, I went to test my 'calculator' I made a few months ago.

It's principle is very basic : you can send '1+3' or '10-5*7' or anything else in this spectrum, and it gives you back the result. Pretty simple stuff, right?

Then I typed 1+1. It gave me back 0. Typed 24-7. It gave me back 0 again. I restarted the IDE and the problem continued. So I tried 1+3-2. Surprisingly, it sent back the correct asnwer, 2!
After that, it would send me the INVALID message (That I created so, if you typed wrong, that would be showing you did it).

I'll leave the code here because I have absolutely no idea of what's going on.

long number1;
long number2;
long number3;
char signal1;
char signal2;
long result;

void setup() {
  Serial.begin(9600);
  Serial.println("Send me some calculation");
  Serial.println("EG : 2+3");
  Serial.println();
}

void loop() {
  if (Serial.available() > 0) {
    number1 = Serial.parseInt();
    signal1 = Serial.read();
    number2 = Serial.parseInt();
    if (Serial.available() < 1) {
      simpleResolution();
    } else {
      signal2 = Serial.read();
      number3 = Serial.parseInt();
      complexResolution();
    }
    Serial.println("Result = ");
    Serial.println(result);
    Serial.println();
    Serial.println("Send me another, if you wish");
    Serial.println();
  }
}

void simpleResolution() {
  switch (signal1) {
    case '+' :
      result = number1 + number2;
      break;
    case '-' :
      result = number1 - number2;
      break;
    case '*' :
      result = number1 * number2;
      break;
    case '/' :
      result = number1 / number2;
      break;
    default :
      Serial.println("INVALID");
      Serial.println();
      result = 0;
      break;
  }
}

void complexResolution() {
  if (signal1 == '+' && signal2 == '+') {
    result = number1 + number2;
    result = result + number3;
  }
  if (signal1 == '+' && signal2 == '-') {
    result = number1 + number2;
    result = result - number3;
  }
  if (signal1 == '+' && signal2 == '*') {
    result = number1 + number2;
    result = result * number3;
  }
  if (signal1 == '+' && signal2 == '/') {
    result = number1 + number2;
    result = result / number3;
  }
  if (signal1 == '-' && signal2 == '+') {
    result = number1 - number2;
    result = result + number3;
  }
  if (signal1 == '-' && signal2 == '-') {
    result = number1 - number2;
    result = result - number3;
  }
  if (signal1 == '-' && signal2 == '*') {
    result = number1 - number2;
    result = result * number3;
  }
  if (signal1 == '-' && signal2 == '/') {
    result = number1 - number2;
    result = result / number3;
  }
  if (signal1 == '*' && signal2 == '+') {
    result = number1 * number2;
    result = result + number3;
  }
  if (signal1 == '*' && signal2 == '-') {
    result = number1 * number2;
    result = result - number3;
  }
  if (signal1 == '*' && signal2 == '*') {
    result = number1 * number2;
    result = result * number3;
  }
  if (signal1 == '*' && signal2 == '/') {
    result = number1 * number2;
    result = result / number3;
  }
  if (signal1 == '/' && signal2 == '+') {
    result = number1 / number2;
    result = result + number3;
  }
  if (signal1 == '/' && signal2 == '-') {
    result = number1 / number2;
    result = result - number3;
  }
  if (signal1 == '/' && signal2 == '*') {
    result = number1 / number2;
    result = result * number3;
  }
  if (signal1 == '/' && signal2 == '/') {
    result = number1 / number2;
    result = result / number3;
  }
}

If anyone knows the problem here, please awnser it for me

Place some Serial.println()s at strategic locations in the sketch to see what variables are doing.

.

Says that we're sending our serial at a snails pace so that might mean that the assumption made above is not a good one.

So what can I do to fix it? I tried faster speeds but then it gets all glitched out...

Joprp05:
So, let me throw in some backstory. I changed my computer for a new one and was checking all my sketches, just some routine check. Then, I went to test my 'calculator' I made a few months ago.

Are you saying that this program worked when you were using the old PC and WITHOUT A SINGLE CHANGE to the Arduino program it is not working now?

If so, are you now using a different version of the Arduino IDE? And if so have you tried installing and using the version of the IDE that you used to be using? (You can install sever versions of the IDE on a PC).

To be honest, I can't see how the version of the IDE could cause the problem, but it would be interesting to try.

...R

Did you do some serial prints to track what your variables are doing?

.

Did you also set the Serial monitor to the same speed when you changed it? The speed in the code has to match what the serial monitor is set to.

I am so freaking dumb...
So dumb...

Joprp05:
I am so freaking dumb...
So dumb...

Welcome to the club

Glad the solution was simple.

...R

Nnnope it wasn't

Did you also set the Serial monitor to the same speed when you changed it? The speed in the code has to match what the serial monitor is set to.

Still did the same thing as before (even in 115200 baud rate)

Did you do some serial prints to track what your variables are doing?yy

Interestingly enough, i tried that.

I wrote this :

if (Serial.available() > 0) {
    number1 = Serial.parseInt();
    signal1 = Serial.read();
    number2 = Serial.parseInt();
    if (Serial.available() < 1) {
      simpleResolution();
    } else {
      signal2 = Serial.read();
      number3 = Serial.parseInt();
      complexResolution();
    }
    Serial.println(number1);
    Serial.println(number2);
    Serial.println(signal1);
    Serial.println("Result = ");
    Serial.println(result);
    Serial.println();
    Serial.println("Send me another, if you wish");
    Serial.println();
  }
}

It showed me (digited 1+1) 1;1;+
Still, result 0.

tried baud rate 115200 and still same thing.
I right now am starting to get confused

I'll try the older version and see if that works

Since you ask the user to input the whole equation in one line this is easy. Don't parse anything out at first. Just take in all the data and stick it into a char buffer. Keep adding each character you read into the buffer until you get to a newline character. At that point, you know you've got the entire thing. Then, parse the numbers and operators out of that char array. strtok function may be helpful. And of course, you need atoi function to convert the ascii into real numbers like parseInt is doing for you now.

Firstly, sorry, that nope wasn't for this, my internet failed for a sec.

Secondly (I am so embarresed to say this), I kinda don't know how to do that? I can try, but i'm not an expert when it comes to manipulating data (I am so ashamed). I'll try to give you a prototype

Thirdly, no Robin, IDE 1.8.4 didn't work. I guess it's the code. Maybe i mistakenly pressed a key and bugged the whole thing. I don't have a clue at this point

Thank you, I will

Just one last thing.

This code (at least was) an expansion of THIS code here :

long number1;
long number2;
char calSignal; 
long result;

void setup() {
  Serial.begin(9600);
  Serial.println("Send me a calculation");
  Serial.println("E.G. : 2+3");
  Serial.println();
}

void loop() {
  while(Serial.available() > 0) {    
    number1 = Serial.parseInt();    
    calSignal = Serial.read(); 
    number2 = Serial.parseInt();
    resolution();
    Serial.println("Result = ");
    Serial.println(result);
    Serial.println(); 
    Serial.println("Send me another, if you wish"); // prints
    Serial.println(); 
  }
}

void resolution() { 
  switch (calSignal) {
    case '+' :
    result = number1 + number2; 
    break;
    case '-' :
    result = number1 - number2;
    break;
    case '*' :
    result = number1 * number2;
    break;
    case '/' :
    result = number1 / number2; 
    break; // break to exit the "case"
    default :
    Serial.println("INVALID");
    Serial.println();
    result = 0;
  }
}

Edit : now this code is also failing! What am i gonna do? What am I doing wrong....

Oho boy now i get it! I think i can do this now! Thanks for all the help. If I don't manage to do it, may I show it here?

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

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

void loop() {
  recvWithEndMarker();
  if (newData = true) {
    strcpy(tempData, dataReceived);
    parseData();
    doMaths();
    showResults();
    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);
  strtokIndex = strtok(NULL, " ");
  strcpy(calSignal, strtokIndex);
  strtokIndex = strtok(NULL, " ");
  number2 = atol(strtokIndex);
}

void doMaths() {
  if (calSignal == '+') {
    result = number1 + number2;
  }
  if (calSignal == '-') {
    result = number1 - number2;
  }
  if (calSignal == '*') {
    result = number1 * number2;
  }
  if (calSignal == '/') {
    result = number1 / number2;
  }
}

void showResults() {
  Serial.print("Result is = ");
  Serial.println(result);
}

Yeah... it didn't work. It keeps printing result = 0 over and over. I am pretty tired now

Ok so look at this bit of code :

strtokIndex = strtok(NULL, " ");
  strcpy(calSignal, strtokIndex);
  Serial.println(calSignal);

This isn't finding the calSignal, so it returns 'blank' (blankspace)
Soooo, what did i do wrong?

yeah, typed and saw very well : 1 + 1

oh, ok... I get it now...

OH YEAHHHHHHHH

WOHOOOOOOHOHOHOOO!

FINALLY!

IT WORKED!

(now I have to fix the non-stop printing issue but FOR GODS SAKE IT DID THE MATH CORRECTELY AND RECOGNISED THE DATA SEND!)