This article by Bo Master original, if there is not the place please specify, reprint please indicate the source.
1 /*********************************2 code function: Serial control RGB tri-color lamp3 using functions:4 Serial.flush (); Empty buffers5 serial.available (); Reads the amount of data for the current serial buffer pool6 serial.print (string); Send it out via TX7 serial.write (string); Write to a string8 serial.println (string); Go through TX and add a newline at the end9 strtok (String, ' delimiter '); String DecompositionTen strtol (String address, NULL (default), set binary); Character Reshaping One constrain (source data, interval lower bound, upper bounds of interval); Interval function A Analogwrite (PIN number. level); IO output function - creation time: 2016*01*05 - Author Email: [Email protected] the ********************************/ - Charbuffer[ -];//declaring a character space - intRed,green,blue;//Define a variable - + intRedpin = One;//Red - intGreenpin =Ten;//Green + intBluepin =9;//Blue A at voidSetup () { -Serial.begin (9600);//Initialize serial port baud rate -Serial.flush ();//empty the serial cache - Pinmode (redpin,output); - Pinmode (greenpin,output); - Pinmode (bluepin,output); in } - voidLoop () to { + if(Serial.available () >0){//reads the amount of data for the current serial buffer pool - intindex =0; theDelay -); * intNumChar = Serial.available ();//get the number of current cache pools $ if(NumChar > the){//as long as the last 15 charactersPanax NotoginsengNumChar = the; - } the while(numchar--){ +buffer[index++] = Serial.read ();//extracting data from the cache A } thesplitstring (buffer);//Incoming function function + } - } $ voidSplitstring (Char*data) { $Serial.print ("Data entered:"); - serial.println (data); - Char*parameter; theparameter = strtok (data,",");//Explode string - while(Parameter! = NULL) {//whenWuyi setled (parameter); theParameter = Strtok (NULL,","); - } Wu for(intx=0;x< -; x + +){ -buffer[x]=' /'; About } $ Serial.flush (); - } - voidSetled (Char*data) { - if((data[0]=='R')|| (data[0] =='R')){//whether it is R A intAns = Strtol (data+1NullTen);//character Reshaping +Ans = constrain (ans,0,255);//go to interval value theAnalogwrite (Redpin,ans);//Output -Serial.print ("Red is set to:"); $ serial.println (Ans); the } the the if((data[0] =='g')|| (data[0] =='G')){ the intAns = Strtol (data+1NullTen); -Ans = constrain (ans,0,255); in Analogwrite (Greenpin,ans); theSerial.print ("Green is set to:"); the serial.println (Ans); About } the the if((data[0] =='b')|| (data[0] =='B')){ the intAns = Strtol (data+1NullTen); +Ans = constrain (ans,0,255); - Analogwrite (Bluepin,ans); theSerial.print ("Blue is set to:");Bayi serial.println (Ans); the } the}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
Reference book: HTTP://VDISK.WEIBO.COM/S/B_U0YKSWLLQNM?CATEGORY_ID=0&PARENTS_REF=B_U0YKSX62TSP
Reference website: http://wiring.org.co/reference/
Reference page: http://www.cnblogs.com/jikexianfeng/p/6243654.html
Reference page: http://www.cnblogs.com/rayray/p/3198091.html
Serial control RGB tri-color lamp