0

I am trying to figure out how to make the arduino accept an equation from the user and spit out an answer with a given input. For example, I want the arduino to accept a user input of "2*x". Then, I will request what "x" do you want. I will say "2" for x = 2, and the arduino should spit out "4" as an answer. These functions can come in a variety of forms including sin, cos, exponentials.

Here is my code thus far. I am having trouble setting up how to read entire strings. Then my next problem is converting that string into a function the arduino can recognize.

Thank you for any help you can offer.

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

  }

char p = ' ';
int junk = 0;
void loop() {

  int junk = 0;
  Serial.println(F("Gimme a function"));
  while (Serial.available() == 0); {  // Wait here until input buffer has a    character
     p = Serial.read();
     while (Serial.available() > 0) {  // .parseFloat() can leave non-numeric characters
        junk = Serial.read() ;       // clear the keyboard buffer
     }
  }
  Serial.println(p);
}
  • You need to write some string splitting code, some char array parsing or similar. What you've posted will only fill a buffer; not actually *do* anything with it. Look at the existing questions in the "Related" column to the right; specifically check questions concerning string splitting, string comparison and such. There's plenty of example code available. – jDo Apr 11 '16 at 00:45
  • Thank you for your guidance! – Programmer In Training Apr 11 '16 at 01:00
  • You're welcome. Here's a simple string reading routine: https://stackoverflow.com/a/12439969/6004486 and here's something about matching strings: https://stackoverflow.com/questions/5029612/how-to-match-text-in-string-in-arduino?rq=1 Good luck with your project :) – jDo Apr 11 '16 at 01:10
  • I understand how to read the string. I can't seem to figure out how to actually implement the string so that the Arduino can translate it into a function. How is it related to matching strings? – Programmer In Training Apr 11 '16 at 01:44
  • I don't know exactly what you're doing but I would imagine that you have to look for certain characters in the user input; e.g. how do you find out whether the user wants the `-` or `+` operator? How do you find a parenthesis? That's why I thought about string matching. There are two types of strings in arduino land by the way: 1 - classical C-style `char` arrays and 2 - a "modern" type called `String` which employs a char array internally but comes with additional features (and overhead) – jDo Apr 11 '16 at 08:13

0 Answers0