Hi,
I'm learning how to use functions with Arduino and I have a few questions. I modified the code below from a text I'm learning from, and while I feel I simplified it a bit, there are a few areas of the code I'm unsure how it's actually being used.
/* Goals:
* 1. (done) Enter data into serial monitor
* 2. (done) Print it back out on serial monitor
* 3. (done) Perform math on the data which was entered
*
*
* Notes:
* 1. referencing p. 104 - 107+ in text
*/
/***** FUNCTION PROTOTYPES *****/
int ReadLine(char);
/***** SETUP *****/
void setup()
{
Serial.begin(9600);
}
/***** LOOP *****/
void loop()
{
if (Serial.available() > 0) /* looks for the number of data bytes in the serial buffer.
If any bytes are there (e.g. information), the code will then create
several local variables and call function ReadLine()
*/
{
char myData[20]; // create an array size of 20 elements,
/* If Arduino rec. only use the data type char for storing characters, then it follows
* to only use a data type char for an array if holding the characters
*/
int bufferCount = ReadLine(myData); /* Pass the parameter, char array 'myData' into the arguements of function ReadLine() for the following:
(1) counts the characters in the array which is returned to variable 'bufferCount'
in case there's some value in using it
(2) MOST IMPT: allows array myData to store info. from the serial buffer,
which will then be converted into a number using the function, atoi()
*/
int number = atoi(myData); // converts contents of char array (which is a string) to int data type
/* In C, atoi() converts a string to an integer. The atoi function skips
* all white-space characters at the beginning of the string, converts
* subsequent characters as part of the number, and then stops when it
* encounters the first character that isn't a number.
*
* If the string does not represent an integer at all, atoi will return 0
*
* This line is executed once the Enter key has be pressed
*/
// print number entered
Serial.print("number entered: ");
Serial.println(number);
// perform math on number which was entered
Serial.println();
Serial.print(" number entered / 2 = "); Serial.println(number / 2); // odd number will have decimal truncated
Serial.println();
// print number of characters in array (character count)
Serial.print("bufferCount = "); Serial.println(bufferCount);
}
}
/***** FUNCTIONS *****/
/*****
* Purpose: Read data from serial port until newline characater is read ('\n')
*
* 2 Parameters:
* Character Array, which will strore contents entered from serial monitor
* char myData[] // the character array, 'myData', and will be treated it as a nul-terminating string
*
* Return value:
* int // the number of characters read for the string, using int index
*
* CAUTION: This method will sit here forever if no input is read from the serial monitor port and no newline character is entered. (if that a bad thing?)
*
*****/
int ReadLine(char myData[]) // passes parameter array, 'myData[]' into the arguement of the function, 'Readline()'
{
char new_character; /* stores each character as entered into the serial monitor, which gets passed, one by one, into the buffer
It’s recommended to only use char for storing characters. source: https://www.arduino.cc/reference/en/language/variables/data-types/char/
*/
int index = 0; /* counter for storing incoming characters entered into array 'myData[]'
Each new var 'c' entered will increase the position
of the array which the new character is entered in. This will con't until a '\n' is read via the while loop,
as long as there is data in the buffer (hence the statement, Serial.available() > 0)
*/
while (true)
{
if (Serial.available() > 0)
{
new_character = Serial.read(); // all data entered into the serial monitor by default is a char
if (new_character != '\n') // \n is C programmning code for indicating that the Enter button was pressed
{
myData[index++] = new_character; // place character into char array myData at +1 element position
}
else
{
myData[index] = '\0'; // add a null termination character to the string
break; /* Used to break out of the while loop (even if nesting occurs), hence ending reading from the serial buffer
That occurs is that the break returns a "false" paramtere to the while() function, which ends the loop
The code will then con't to next line of code after the while loop, which here is "return index"
*/
}
}
}
return index; // returns the counter value of var index to var bufferCount
}
Q1. In the code, I have a line with a comment next to it that I’m not sure is correct:
char myData[20]; // create an array size of 20 elements
But the size of the array appears irrelevant. When I replace 20 by 0, the program works the same. Why?
Q2: Why is the max number of characters I’m able to type into the serial 27? At 28+ nothing appears when you hit enter?
As I understand it, Arduino’s buffer has a max size of 64 bytes, and 27 characters is 27*8 bytes = 224 bytes. The reason this appears odd is that in my code ‘bufferCount’ will correctly return 27 if you enter that many characters into the serial monitor, but if I try 28, nothing happens.
This seems even odder since bufferCount is of type ‘int’, and thus, only 5 characters can accurately get recorded (so long as the number entered into the serial monitor is <32,768)