c++ - Sending multiple values through serial port -
i using arduino uno project, don't know how go achieving want. i'm beginner therefore don't know how use functions want.
i want send 3 values through serial monitor @ once.
the first value string points operator (a, s, m, d, p), , want take 2 more values. example, if input 'a012556 should add 12 , 556 give me 568.
currently doing this, it's asking each input separately. example, ask operator (i can input ints because cannot take string/chars) first, input 1 (should addition), , asks first number, , second number, , adds them both , outputs result. works fine, it's not i'm trying achieve.
serial.println ("enter chosen operator"); serial.println ("a addition, s subtraction, m multiplication,"); serial.println ("d division, , p potentiometer: "); // takes input user int theoperator = datainput(); if (theoperator == 1) { // if input equal 1, carry out function 'addition()' addition(); } else if ..............
above in loop function currently, , points function below. same subtract/multiply/divide/etc.
void addition() { serial.println ("a"); serial.println ("please enter first number in format nnn: "); firstnumber = datainput(); // asks user input first set of numbers /* message must in format cnnnnnn therefore first number must greater or equal -99 , less or equal 999*/ if (firstnumber >= -99 && firstnumber <= 999) { serial.println (firstnumber); // prints first set of numbers user view } else { /* if data input not match format cnnnnnn error message display if input invalid red led turn on */ digitalwrite(led_red, high); serial.println ("--------------- error ---------------"); } serial.println ("please enter second number in format nnn: "); secondnumber = datainput(); // asks user input second set of numbers /* message must in format cnnnnnn therefore second number must greater or equal -99 , less or equal 999*/ if (secondnumber >= -99 && secondnumber <= 999) { // led turn off if on because valid input digitalwrite(led_red, low); serial.println (secondnumber); // prints second set of numbers user view } else { digitalwrite(led_red, high); serial.println ("--------------- error ---------------"); } /* give time red error led stay on user notice he/she has made invalid input */ delay(500); digitalwrite(led_red, low); // case addition, add first , second numbers value = (firstnumber + secondnumber); serial.print("value: "); // prints value of 2 sets of numbers user can see value of message serial.println(value); }
therefore there way of doing this? need substring?
thank in advance, advice appreciated.
yes, substring need.
fortunately, string provides substring function.
https://www.arduino.cc/en/reference/stringsubstring
you code bit this:
// expecting operation in "oxxxyyy" o operator, xxx, yyy operands. bool parseoperation(string& strinput, char& op, int& x, int& y) { if (strinput.length() != 7) return false; op = strinput[0]; x = atoi(strinput.substring(1, 4).c_str()); y = atoi(strinput.substring(4).c_str()); return true; } // call as: void loop() { string str; char op; int r, x, y; // read user input str... if (parseoperation(str, op, x, y)) { switch(op) { case 'a': case 'a': op = '+'; r = x + y; break; // etc... default: // print error message... break; } // print x op y = r } else { // print error message... } }
Comments
Post a Comment