'1' is a single character.
'2' is a single character.
'20' is two characters.
I recommend separating your numbers by some non-numeric character (typically a comma). Then you can read in characters until you see a comma, and pass those characters on to atoi() to get the int value of those characters. Done right, it should be able to handle any size number (though you will still be limited by the size of number int can store, or long if you switch to that datatype).
I've been trying but can't make it work can only make it work if I only print the result.
can someone please make the required changes so that it works whit case.
char buffer [10]; // 9 digits is overkill, I know.
buffer [0] = '1'; // these assignments are just for illustration, in reality
buffer [1] = '2'; // you would read your input one character at a time
buffer [2] = '\0'; // from Serial.
// be aware, however, that the Serial monitor doesn't send a delimiter at the end of
// input
int val = atoi (buffer);
switch (val)
{
case 1: break;
case 2: break; // and so on.
}
Your original multicharacter constant approach would work, but it gets a bit tricky beyond two digits for an 'int'
Below is some code i use to test servos which captures a string of characters and converts the string into a number. You might get some ideas from the code.
// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n;
char carray[6]; //converting string to number
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
myservo.writeMicroseconds(n); // for microseconds
//myservo.write(n); //for degees 0-180
readString="";
}
}