资源描述:
《Arduino程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、目录:1串口控制LED灯。12数码管骰子33滚珠开关64以正弦波亮度渐变75LED跑马灯8总结91串口控制LED灯。1功能:通过串口控制哪个灯发光及发光亮度2程序代码:charbuffer[18];intred,green,blue;intRedPin=11;intYellowPin=10;intBluePin=9;voidsetup(){Serial.begin(9600);Serial.flush();pinMode(RedPin,OUTPUT);pinMode(YellowPin,OUTPUT);pinMod
2、e(BluePin,OUTPUT);}voidloop(){if(Serial.available()>0){intindex=0;delay(100);intnumChar=Serial.available();if(numChar>15){numChar=15;}while(numChar--){buffer[index++]=Serial.read();}splitString(buffer);}}voidsplitString(char*data){Serial.print("Dataentered:");
3、Serial.println(data);char*parameter;parameter=strtok(data,",");while(parameter!=NULL){setLED(parameter);parameter=strtok(NULL,",");}for(intx=0;x<16;x++){buffer[x]=' ';}Serial.flush();}voidsetLED(char*data){if((data[0]=='r')
4、
5、(data[0]=='R')){intAns=strtol(data
6、+1,NULL,10);Ans=constrain(Ans,0,255);analogWrite(RedPin,Ans);Serial.print("Redissetto:");Serial.println(Ans);}if((data[0]=='y')
7、
8、(data[0]=='Y')){intAns=strtol(data+1,NULL,10);Ans=constrain(Ans,0,255);analogWrite(YellowPin,Ans);Serial.print("Yellowissetto:");Se
9、rial.println(Ans);}if((data[0]=='b')
10、
11、(data[0]=='B')){intAns=strtol(data+1,NULL,10);Ans=constrain(Ans,0,255);analogWrite(BluePin,Ans);Serial.print("Blueissetto:");Serial.println(Ans);}}3程序截图:设置:黄灯亮度1,红灯亮度50,绿灯亮度100,输入:r1b50g100
可以观察到。绿灯最亮,红灯次之,黄灯最暗4动态效果:见视频:2数
12、码管骰子1功能介绍:当按下一个按钮时候,数码管数字渐变,频率由快而慢,并最后随机的停在一个数字上。2程序代码:voidclearNum(){for(inti=4;i<=10;i++){digitalWrite(i,LOW);}}voidshowNum1(){clearNum();digitalWrite(9,HIGH);digitalWrite(10,HIGH);}voidshowNum2(){clearNum();digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalW
13、rite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(4,HIGH);}voidshowNum3(){clearNum();digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);}voidshowNum4(){clearNum();digitalWrite(6,HIGH);digitalWrite(7,HIGH);dig
14、italWrite(10,HIGH);digitalWrite(9,HIGH);}voidshowNum5(){clearNum();digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(8,HIGH);digitalWrite(7,HIGH);digitalWrite(10,HIGH);}voidsh