I am new to programming and I am getting a littlebit in trouble.
I want to read 3 values I typed into the console to control a RGB LED with my arduino.
I use an
int array[3]={};
to store the data sent over serial.
But I have absolutely no idea on how to write the data into the array.
It would be nice if someone has got a code-snippet.
Some example:
Serial input:
255,255,255
Output of the LED: White
Kind Regards
(Sorry for bad english)
First try:
if(Serial.available() >= 3) {
for(int i = 0; i < 4; i ++) {
array[i] = Serial.read();
}
Serial.flush();
}
The Output of the Array then is: {‘2’,‘5’,‘5’,’,’}
(Yes, I know, that I have to change the numbers, on how much data I will collect)
If you enter the numbers using serial, the ardiuno will receive it as ascii text.
You need to collect the chars which represent each number, convert to an actual number, and store it in the array. Repeat three times. There are countless examples of this.
The atoi() function bears investigation, as well as a beginner book on string handling in C. A string is a NULL terminated array of chars. While you have an array of chars, it is NOT NULL terminated, so it is NOT a string, so you can NOT use the string handling functions (like atoi()).