長輩視訊-arduino
這邊主要是Arduino接收到人的動作所要下達的指令,還有與PC連線時,PC要確保USB還連線著,所以PC會定時傳訊息詢問連線狀態,Arduino要做回應。
code
#define phonepin 6 #define audiopin 7 #define voluppin 4 #define voldownpin 5 #define audioctrlpin 3 #define earphonectrlpin 2 #define ledpin 13 int phonestate = 0; int phonestate_ = 0; int audio = 0; int audio_ = 0; int audiostate = 0; int volup = 0; int volup_ = 0; int voldown = 0; int voldown_ = 0; void setup() { // put your setup code here, to run once: pinMode(phonepin, INPUT); pinMode(audiopin, INPUT); pinMode(voluppin, INPUT); pinMode(voldownpin, INPUT); pinMode(audioctrlpin, OUTPUT); pinMode(earphonectrlpin, OUTPUT); pinMode(ledpin, OUTPUT); digitalWrite(audioctrlpin, HIGH); delay(10); digitalWrite(audioctrlpin, LOW); audiostate = 1; Serial.begin(9600); //Keyboard.begin(); if(phonestate == 0){ digitalWrite(ledpin, HIGH); }else{ digitalWrite(ledpin, LOW); } } void loop() { // put your main code here, to run repeatedly: //delay(30); /*pick up and hang up phone 拿起電話及掛斷電話*/ phonestate = digitalRead(phonepin); if(phonestate_ != phonestate){ phonectrl(); } /*audio ctrl 擴音控制*/ audio = digitalRead(audiopin); if(audio_ != audio){ audioctrl(); } /*volume up 音量調大*/ volup = digitalRead(voluppin); if(volup_ != volup){ volupctrl(); } /*volume down 音量調小*/ voldown = digitalRead(voldownpin); if(voldown_ != voldown){ voldownctrl(); } /*read serial text from PC 讀電腦傳的訊息*/ if (Serial.available()>0){ String text = serialreadtext(); //char text = Serial.read(); if(text == "Hey"){ Serial.println("exist"); } else if(text == "quit"){ Serial.println("quit"); } } } /************** *************** ************** other code *************** ************** ***************/ /*read string 讀字串*/ String serialreadtext(){ char serialtext; String data = ""; int space = -1; while((serialtext = Serial.read()) != '\n'){ if((int)serialtext != space){ data += serialtext; } } return data; } /*pick up and hang up phone 拿起電話及掛斷電話*/ void phonectrl(){ delay(30); if(phonestate == 0){ Serial.println("pick up"); digitalWrite(ledpin, HIGH); digitalWrite(earphonectrlpin, HIGH); delay(10); digitalWrite(earphonectrlpin, LOW); audiostate = 0; } else{ Serial.println("hang up"); digitalWrite(ledpin, LOW); digitalWrite(audioctrlpin, HIGH); delay(10); digitalWrite(audioctrlpin, LOW); audiostate = 1; } phonestate_ = phonestate; } /*audio ctrl 擴音控制*/ void audioctrl(){ delay(30); if(audio == 1&& audiostate == 0){ digitalWrite(audioctrlpin, HIGH); delay(10); digitalWrite(audioctrlpin, LOW); audiostate = 1; } else if(audio == 1 && audiostate == 1){ digitalWrite(earphonectrlpin, HIGH); delay(10); digitalWrite(earphonectrlpin, LOW); audiostate = 0; } audio_ = audio; } /*volume up 音量調大*/ void volupctrl(){ delay(30); if(volup == 1){ Serial.println("vol up"); //Keyboard.write(175); } volup_ = volup; } /*volume down 音量調小*/ void voldownctrl(){ delay(30); if(voldown == 1){ Serial.println("vol down"); //Keyboard.write(174); } voldown_ = voldown; }
留言
張貼留言