本帖最後由 qq83429 於 2015-3-22 02:25 編輯 曾經我做過一個小玩具控制四個LED燈我只有控制亮滅其他你能夠改變一下 去控制它前進後退 要APP站內信囉 #include <SortwareSenal.h> SoftwareSerial mySeria1(3, 2); ll RX, TX arduino uno pin<-->TX,RX bluetoc Const byte ledpin_F=7; Const byte ledpin_B=8; Const byte ledpin_L=9; Const byte ledpin_R=l0; Const byte textpin=1l; char val; void setup() { Serial.begin(9600); pinMode(ledpin_F,OUTPUT); pinMode(ledpin_B,OUTPUT); pinMode(ledpin_L,OUTPUT); pinMode(ledpin_R,OUTPUT); pinMode(textpin,OUTPUT); mySerial.begin(9600); } void loop() { digita1Write(textpin,l-IIGH); delay(200); if (mySerial.avai1able()>0){ va1=mySeria1.read0; Seria1.print("val= "); Seria1.print1n(va1); switch (val){ case 』w': stopled(); digitalWrite(ledpin_F,I-IIGH); break; case 'x': stopled(); digita1Write(ledpin_B,l-IIGH); break; case 『a』: st0pledO; digita1Write(1edpin_L,HIGH); break; case 『d』: st0pledO; digita1Write(1edpin_R,HIGH); break; case 『s』: st0pledO; break; } digita1Write(textpin,I_OW); delay(500); } } void stopled(){ digita1Write(1edpin_F,LOW); digita1Write(1edpin_B,LOW); digita1Write(1edpin_L,LOW); digita1Write(1edpin_R,LOW); } |
回復 5# ymin #include <SoftwareSerial.h> #include <Wire.h> SoftwareSerial I2CBT(10,11); int n=0; int o=0; int p=0; int ledPin1 = 5; int ledPin2 = 9; int ledPin3 = 6; void setup() { Serial.begin(9600); I2CBT.begin(9600); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } void loop() { byte cmmd[20]; int insize; while(1){ if ((insize=(I2CBT.available()))>0){ //讀取藍牙訊息 Serial.println("input size = "); Serial.println(insize); for (int i=0; i<insize; i++){ Serial.print(cmmd=char(I2CBT.read())); Serial.print(" "); } } //--------------------------------------------------------------------------- (LED1) if (cmmd[0] == 97) { //讀取第一個字 n=n+25.5; if(n>=255) { n=255; } analogWrite(ledPin1,n); delay(500); cmmd[0]=0; } if(cmmd[0] == 98){ n=n-25.5; if(n<=0) { n=0; } analogWrite(ledPin1,n); delay(500); cmmd[0]=0; } //--------------------------------------------------------------------------- (LED2) if (cmmd[0] == 99) { //讀取第一個字 o=o+25.5; if(o>=255) { o=255; } analogWrite(ledPin2,o); delay(500); cmmd[0]=0; } if(cmmd[0] == 100){ o=o-25.5; if(o<=0) { o=0; } analogWrite(ledPin2,o); delay(500); cmmd[0]=0; } //--------------------------------------------------------------------------- (LED3) if (cmmd[0] == 101) { //讀取第一個字 p=p+25.5; if(p>=255) { p=255; } analogWrite(ledPin3,p); delay(500); cmmd[0]=0; } if(cmmd[0] == 102){ p=p-25.5; if(p<=0) { p=0; } analogWrite(ledPin3,p); delay(500); cmmd[0]=0; } } //while }//loop 這是我用pwm控制3色LED變換不同的顏色,應該是一樣的東西你可以做修改 |
小黑屋|手機版|Archiver|機器人論壇 from 2005.07
GMT+8, 2025-1-31 10:40 , Processed in 0.208665 second(s), 11 queries , Apc On.
Powered by Discuz! X3.2
© 2001-2013 Comsenz Inc.