Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Serial Integer Reader, by Pedro Tome'
- * Version 0.01
- *
- * Reads integer numbers from the Serial buffer and translates them
- * into the value of the variable "number".
- *
- * Use this code if you want to read integer values from another Arduino,
- * which should have the Serial Integer Writer code, by Pedro Tome'.
- *
- * Example:
- * The number 3782 is outputted to the Serial buffer.
- * The variable "number" now holds the value 3782.
- *
- * The maximum value is 65535 due to "number" being an unsigned int.
- */
- char charArray[6];
- int intArray[6];
- unsigned int number;
- void setup() {
- Serial.begin(9600);
- }
- void loop () {
- int charIndex = 1;
- int intIndex = 1;
- int max_intIndex;
- number = 0;
- /* If there is something in the serial buffer */
- if (Serial.available()){
- /* Read the data from Serial and store it in a charArray */
- while (Serial.available()) { // while there's something in the serial buffer...
- int serialIn = Serial.read();
- delay(5);
- charArray[charIndex] = serialIn; // store everything that was read in charArray byte-by-byte
- charArray[charIndex+1] = '\0'; // add a NULL value
- charIndex++;
- }
- charIndex = 1; // Reset charIndex
- /* Separate the charArray and convert it into an intArray */
- while (charArray[charIndex] != '\0') { // while the value is not NULL, ie, while there's a relevant value
- max_intIndex = intIndex;
- intArray[intIndex] = charArray[charIndex]-'0';
- charIndex++;
- intIndex++;
- }
- intIndex = 1; // Reset intIndex
- /* Build one single number from the intArray */
- for (intIndex = 1; intIndex <= max_intIndex; intIndex++) {
- number = number*10 + intArray[intIndex];
- }
- Serial.print("number: "); Serial.println(number);
- Serial.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement