-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialMultiRead.cpp
More file actions
61 lines (53 loc) · 2.35 KB
/
Copy pathSerialMultiRead.cpp
File metadata and controls
61 lines (53 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
SerialMultiRead.h - Library for reading serial input and converting to a single, multi-digit integer
Created by Zachary W. Groce, February 22, 2019
Released into the public domain.
*/
#include "Arduino.h"
#include "SerialMultiRead.h"
SerialMultiRead::SerialMultiRead(){
}
signed long int SerialMultiRead::multiRead(){
/*
Calculation Process:
User inputs a number in the serial window (Example: 123)
Values added to an array (Example: [1, 2, 3])
When serial reads "\n" (new line character), convert the array to an integer
The first digit while be multiplied by 10^exponent, where exponent is one less
than the number of digits (Example: 1 * 10^2 = 100)
Add the result of the above calculation to the variable "number"
Loop through the above process for each digit, decreasing the value of the exponent
by one each time, adding each result to "number"
Return the number
*/
int characters[50]; //Creates an array to hold the incoming digits
int digit, num_digits, exponent; //Creates variables for use in the function
int counter = 0, pos = 0;
signed long int number = 0;
/*
digit = Current serial input being evaluated
num_digits = The number of digits the user entered
exponent = Tracks the value of the exponent for use in the calculation
counter = Incremental input used to track the number of digits entered
pos = tracks the position in the array
number = Final ouput of a multi-digit integer
*/
while(Serial.available() > 0){
digit = Serial.read() - '0';
if(digit == -38){ //Serial input is the "\n" new line character, convert array
num_digits = counter;
exponent = counter - 1;
for(exponent; exponent > -1; exponent--){
number += characters[pos] * (signed long int)round(pow(10,exponent));
pos++;
}
}else if(digit >= 0 && digit <= 9){ //Serial input is a digit 0 - 9
characters[counter] = digit;
counter++;
}else{
Serial.println("Only enter digits 0 - 9"); //Unexpected Serial entry
}
delay(10);
}
return number;
}