|
- #include <Servo.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include<Stepper.h>
- LiquidCrystal_I2C lcd(0x27,16,2); //配置LCD地址及行列
- Servo myservo; //創(chuàng)建一個(gè)舵機(jī)控制對(duì)象
- int led1=3; //定義LED1引腳
- int led2=4; //定義LED2引腳
- int buzzer=5; //定義蜂鳴器引腳
- int button=6; //定義按鍵引腳
- int length; //定義一個(gè)變量用來表示共有多少個(gè)音符
- int steps=512;
- Stepper myStepper(steps, 8, 9, 10, 11);
- int song[] =
- {
- /* 歌曲頻率*/
- 589, 495, 441, 495,
- -1, 589, 495, 441, 589,
- 495, -1, 495, 495, 495, 441, 495, -1, 495,
- 589, 495, 441, 589, 495, -1,
- };
- float duration[]=
- {
- /* 持續(xù)的時(shí)間 單位 ms */
- 1, 1, 0.5, 0.5,
- 1, 0.5, 0.5, 0.5, 0.5,
- 1, 0.5, 0.5, 0.5, 1, 0.5, 1, 0.5, 0.5,
- 0.5,0.5, 0.5, 0.5, 1, 1,
- };
- void setup()
- {
- {
- pinMode(led1,OUTPUT); //3號(hào)數(shù)字口設(shè)置為輸出狀態(tài)
- pinMode(led2,OUTPUT); //4號(hào)數(shù)字口設(shè)置為輸出狀態(tài)
- }
- pinMode(buzzer,OUTPUT);
- length = sizeof(song) / sizeof(song[0]); //這里用了一個(gè)sizeof函數(shù),查出數(shù)組里有多少個(gè)音符
- for (int x = 0; x < length; x++) //循環(huán)音符的次數(shù)
- {
- tone(buzzer, song[x]); //依次播放tune數(shù)組元素,即每個(gè)音符
- delay(400 * duration[x]); //每個(gè)音符持續(xù)的時(shí)間,即節(jié)拍duration,400是調(diào)整時(shí)間的越大,曲子速度越慢,越小曲子速度越快
- noTone(buzzer); //停止當(dāng)前音符,進(jìn)入下一音符
- }
- {
- Serial.begin(9600);
- myservo.attach(2); //綁定對(duì)象至D7
- }
- {
- // 設(shè)置轉(zhuǎn)速,單位r/min
- myStepper.setSpeed(20);
-
- // 初始化串口
- Serial.begin(9600);
- }
- }
- void duoji1()
- {
- myservo.write(150); //寫入角度
- Serial.println(myservo.read()); //打印舵機(jī)角度
- }
- void duoji2()
- {
- myservo.write(0); //寫入角度
- Serial.println(myservo.read()); //打印舵機(jī)角度
- }
- void duoji3()
- {
- myservo.write(10); //寫入角度
- Serial.println(myservo.read()); //打印舵機(jī)角度
- }
- void clockwise()
- {
- // 順時(shí)針一次旋轉(zhuǎn)
- Serial.println();
- myStepper.step(520);
- delay(1000);
- }
- void clockwises()
- {
- // 順時(shí)針一次旋轉(zhuǎn)
- Serial.println();
- myStepper.step(-520);
- delay(1000);
- }
-
- void xianshi1()
- {
- lcd.init(); //初始化LCD
- lcd.backlight(); //打開背光
- lcd.setCursor(3,0); //設(shè)置顯示位置
- lcd.print("Welcome to "); //顯示字符數(shù)據(jù)
- lcd.setCursor(5,1); //設(shè)置顯示位置
- lcd.print("Home !"); //顯示字符數(shù)據(jù)
- }
- void xianshi2()
- {
- lcd.init(); //初始化LCD
- lcd.backlight(); //打開背光
- lcd.setCursor(4,0); //設(shè)置顯示位置
- lcd.print("Good Bye"); //顯示字符數(shù)據(jù)
- lcd.setCursor(4,1); //設(shè)置顯示位置
- lcd.print("*Locked*"); //顯示字符數(shù)據(jù)
- }
- void loop()
- {
- jiance();
- }
- void jiance()
- {
- int buttonState = digitalRead(button); // 檢測(cè)按鍵
- delay(1000);
- int n =digitalRead(button); //創(chuàng)建一個(gè)變量n,按鍵狀態(tài)采集出來賦值給他。
- if (n==HIGH) //判斷n是否為高電平,如果是執(zhí)行下面的語(yǔ)句,不是則跳過。
- {
-
- digitalWrite(led2,LOW); //LED2滅
- digitalWrite(led1,HIGH); //LED1亮
- xianshi1(); //運(yùn)行xianshi
- delay(1000);
- duoji1(); //運(yùn)行xianshi
- setup();
- delay(1000);
- clockwise(); //運(yùn)行xianshi
- delay(11000);
-
- digitalWrite(led1,LOW); //LED1滅
- digitalWrite(led2,HIGH); //LED2亮
- xianshi2(); //運(yùn)行xianshi2
- setup();
- clockwises();
- delay(1000);
- duoji2(); //運(yùn)行xianshi
- delay(1000);
- duoji3(); //運(yùn)行duoji3
- }
- }
復(fù)制代碼 |
|