|
謝謝指點,現在把他全部宣告在外面就可以了- const int irReceiver_a = 2; // IRA
- const int irLed_a = 3;
- const int irReceiver_b = 4; // IRB
- const int irLed_b = 5;
- const unsigned int frequency = 38000; // 發射頻率(單位: Hz)
- void setup() {
- Serial.begin(9600); // 開啟 Serial port, 通訊速率為 9600 bps
- pinMode(irReceiver_a, INPUT); // 把 irReceiverin 接腳設置為 INPUT
- pinMode(irLed_a, OUTPUT); // 把 irLedin 接腳設置為 OUTPUT
- pinMode(irReceiver_b, INPUT); // 把 irReceiverout 接腳設置為 INPUT
- pinMode(irLed_b, OUTPUT); // 把 irLedout 接腳設置為 OUTPUT
-
- tone(irLed_a, frequency); // 產生指定頻率的脈波 (Pulses)
- tone(irLed_b, frequency);
- }
- int ira = digitalRead(irReceiver_a);
- int irb = digitalRead(irReceiver_b);
- int check(void) {
- static char lasta=0,lastb=0;
- if(lasta!=ira) {
- if(ira==1&&irb==1) {
- Serial.println("IN");
- return 1;
- }
- }
- if(lastb!=irb) {
- if(irb==1&&ira==1) {
- Serial.println("OUT");
- return -1;
- }
- }
- lasta=ira;
- lastb=irb;
- return 0;
- }
- int p;
- void loop() {
- int c = check();
- if(c != 0) {
- p = p + c;
- Serial.println(c);
- Serial.println(p);
- }
- }
複製代碼 接下來就是上傳來實驗看看 |
|