Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************
- * Read multiple integers over serial
- * James C4S / byerly0503@gmail.com / www.cmiyc.com
- *
- * NEWLINE *MUST* be enabled to work.
- * Arduino's Serial Monitor must have NewLine enabled (lower right corner)
- *
- * Maximum Value of integerValue depends on its data type.
- * unsigned int max is 999
- * unsigned long max is 999999999
- * otherwise, the number received rolls over.
- *
- ******************************/
- void setup() {
- Serial.begin(9600);
- }
- unsigned int integerValue=0; // Max value is 999
- char incomingByte;
- void loop() {
- if (Serial.available() > 0) { // something came across serial
- integerValue = 0; // throw away previous integerValue
- while(1) { // force into a loop until '\n' is received
- incomingByte = Serial.read();
- if (incomingByte == '\n') break; // exit the while(1), we're done receiving
- if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
- // convert ASCII to integer, add, and shift left 1 decimal place
- integerValue = ((incomingByte - 48) + integerValue)*10;
- }
- integerValue /= 10; // shift back right once, corrects for when '\n' is received
- Serial.println(integerValue); // Do something with the value
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement