北郵信通導(dǎo)論作業(yè)代碼
流水燈
int brightness = 5;
//LED的亮度
int fadeAmount = 5;
//亮度變化增量
void setup()
{
? pinMode(11, OUTPUT);? //發(fā)綠色光
? pinMode(10, OUTPUT);? //發(fā)紅色光
? pinMode(9, OUTPUT);? ?//發(fā)藍(lán)色光
}
?
void loop()
{
? for(int num=11;num>8;num--)
? {
? ? for(int i=0;;i++)
? ? {
? ? ? analogWrite(num, brightness);//點(diǎn)亮LED
? ? ? if (brightness >= 255)
? ? ? {
? ? ? ? fadeAmount = -fadeAmount ;
? ? ? };
? ? ? brightness = brightness + fadeAmount;
? ? ? if(brightness <= 0 )
? ? ? {
? ? ? ? break;
? ? ? }
? ? ? delay(5);
? ? };
? ? fadeAmount = 5,brightness = 5;
? ? analogWrite(num, 0);
? ? ?delay(5);
? }
}
彩色呼吸燈
int brightness = 5;
//LED的亮度
int fadeAmount = 5;
//亮度變化增量
void setup()
{
? pinMode(11, OUTPUT);? //發(fā)紅色光
? pinMode(10, OUTPUT);? //發(fā)綠色光
? pinMode(9, OUTPUT);? ?//發(fā)藍(lán)色光
}
?
void loop()
{
? for(int num=11;num>8;num--)
? {
? ? for(int i=0;;i++)
? ? {
? ? ? analogWrite(num, brightness);//點(diǎn)亮LED
? ? ? if (brightness >= 255)
? ? ? {
? ? ? ? fadeAmount = -fadeAmount ;
? ? ? };
? ? ? brightness = brightness + fadeAmount;
? ? ? if(brightness <= 0 )
? ? ? {
? ? ? ? break;
? ? ? }
? ? ? delay(30);
? ? };
? ? fadeAmount = 5,brightness = 5;
? ? analogWrite(num, 0);
? ? ?delay(30);
? }
}
自動(dòng)控制路燈
int threshold =400; //光強(qiáng)值,根據(jù)實(shí)際情況調(diào)整
void setup ( )
{
Serial.begin(115200);
//設(shè)置串口波特率
pinMode(10, OUTPUT); //設(shè)置輸出端口
}
void loop( )
{
int n = analogRead(A3); //讀取模擬口A3
Serial.println(n);
//串口打印,輸出n的值
if (n>threshold )
//晚上光線暗,n值變大
digitalWrite(10, HIGH);
//點(diǎn)亮路燈
else
digitalWrite(10, LOW);
//關(guān)閉路燈
delay(200);
}
數(shù)字溫度計(jì)
unsigned int tempMin = 24;
//亮燈溫度
unsigned int tempMax = 28;
//報(bào)警溫度
void setup( ) {
Serial.begin(115200);
//串口初始化
analogReference(INTERNAL); //調(diào)用板載1.1V基準(zhǔn)源
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
}
void loop( ) {
double analogVotage = 1.1*(double)analogRead(A3)/1023;
double temp = 100*analogVotage;
//計(jì)算溫度
unsigned int dutyCycle;
//占空比
if (temp <= tempMin) {
//小于亮燈門(mén)限值
dutyCycle = 0; digitalWrite(11, LOW);
}
else if (temp < tempMax) { //小于報(bào)警門(mén)限
dutyCycle = (temp-tempMin)*255/(tempMax-tempMin);
digitalWrite(11, LOW);
}
else{
//發(fā)光二極管亮度最大值,并啟動(dòng)聲音報(bào)警
dutyCycle = 255; digitalWrite(11, HIGH);
}
analogWrite(10, dutyCycle);
//控制發(fā)光二極管發(fā)光
Serial.print("Temp: "); Serial.print(temp);
Serial.print(" DegreesDuty cycle: ");
Serial.println(dutyCycle);
delay(100);
// 控制刷新速度
}
按鍵計(jì)數(shù)器
int count = 0;
void setup() {
????pinMode(2,INPUT); //按鍵接引腳2,按下時(shí)為低電平
????Serial.begin(9600);
????Serial.println(count);
}
void loop() {
????if (digitalRead(2) == LOW) { // 如果按鍵按下
????delay(20); // 延遲20ms,去抖動(dòng)
????if (digitalRead(2) == LOW) { // 再次檢測(cè)按鍵
????count++; // 按鍵計(jì)數(shù)加1
????Serial.println(count);
????while(digitalRead(2) == LOW); // 等待按鍵釋放
????????}
????}
}
智能溫控風(fēng)扇
double analogVotage;
//模擬電壓值
double temp;
//溫度
unsigned int dutyCycle;
//占空比
unsigned int tempMin = 25; //零速溫度
unsigned int tempMax = 33; //滿速溫度
void setup( ) {
????Serial.begin(9600);
????//波特率配置串口通訊
????analogReference(INTERNAL); //調(diào)用板載1.1V基準(zhǔn)源
}
void loop( ) {
????analogVotage = 1.1*(float)analogRead(A3)/1023;
????temp = 100*analogVotage;
????//電壓換算成溫度
????if (temp <= tempMin)
????dutyCycle = 0;
????else if (temp < tempMax)
????dutyCycle = (temp-tempMin)*255/(tempMax-tempMin);
????else
????dutyCycle = 255;
????analogWrite(10, dutyCycle); //產(chǎn)生PWM,控制電機(jī)轉(zhuǎn)速
????Serial.print("Temp: "); Serial.print(temp);
????Serial.print(" DegreesDuty cycle: ");
????Serial.println(dutyCycle);
????delay(80);
????// 控制刷新速度
}