Lesson 23--Microduino "Serial port receives string and translated into an array"
From Microduino Wiki
Language: | English • 中文 |
---|
ContentsObjectiveThis lesson will show to how to translate the string received from serial port into an array and then use these data to control LED. For example, the serial port received data "100,200,300,400,500,600", then output the PWM by port 3,5,6,9,10 they are the PWM output port on Microduino. EquipmentMicroduino-Core Microduino-FT232R
Schematic
//Define a comdata variable and the initial value is NULL
String comdata = ""; //Define a comdata variable and the initial value is NULL
//numdata is array to save the character data
int numdata[6] = {0}, PWMPin[6] = {3, 5, 6, 9, 10, 11}, mark = 0;
void setup()
{
//Define Pin 0~6 as output
for(int i = 0; i < 6; i++) pinMode(PWMPin[i], OUTPUT);
Serial.begin(9600);
}
void loop()
{
//j is the index in the array numdata
int j = 0;
//Check the serial buffer, read the string one by one
while (Serial.available() > 0)
{
//Append the string to comdata
comdata += char(Serial.read());
//Delay 2ms, serial port will prepare the next number. If not, will lost data
delay(2);
//Make the serial data, if no data, break this while
mark = 1;
}
if(mark == 1) //If received data, then splite comdata, otherwise no action
{
//Display input string (optional)
Serial.println(comdata);
//Display the length of input string (optional)
Serial.println(comdata.length());
/*******************Important content*******************/
//Go through the input string by the length
for(int i = 0; i < comdata.length() ; i++)
{
//Analyze the comdate[i], if it is a separator(In this example,it is comma), then analyze next array element
//For example:11,22,33,55, the 11 will be stored in numdata[0], when find the comman, j will be increased to 1
//Then will store new character to numdata[1],find the comman again,will use numdata[2],until the end of the string
if(comdata[i] == ',')
{
j++;
}
else
{ //If no comma, (input number) * 10 + (the number of read before)
//(comdata[i] - '0') means that convert char '0' to ASCII number 0
//If input number is 12345, there are 5 times without comma, then following sentence will execute 5 times
//The left number will be got first and numdata[0]=0,
//So the first circulation is taht numdata[0] = 0*10+1 = 1
//The second time: numdata[0]=1, circulation is: numdata[0] = 1*10+2 = 12
//The third time: numdata[0]=12, circulation is: numdata[0] = 12*10+3 = 123
//The forth time: numdata[0]=123, circulation is: numdata[0] = 123*10+4 = 1234
//At last the string is 0
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
}
}
//comdata string has been converted to number and stored in numdata, clean the comdata for next time using
//If not, this time's result will impact next time
comdata = String("");
//Output numdata in circle, and write to PWM pin
for(int i = 0; i < 6; i++)
{
Serial.print("Pin ");
Serial.print(PWMPin[i]);
Serial.print(" = ");
Serial.println(numdata[i]);
analogWrite(PWMPin[i], numdata[i]);
numdata[i] = 0;
}
//Reset the mark to 0 for next circulation use
mark = 0;
}
} Result
In serial window, input six groups data splited by comma, then printed out the PWM value of corresponding six PWM ports
In serial window input six groups data splited by common, then you can see the LED's brightness. The higher the value of the digital, the more brightness. |