int sensorPin = 2; // Sensor pin 1
int speakerPin = 12; // Positive Lead on the Piezo
int blinkPin = 13; // Positive Leg of the LED we will use to indicate
// signal is received
int startBit = 2000; // This pulse sets the threshold for a transmission start bit
int one = 1000; // This pulse sets the threshold for a transmission that
// represents a 1
int zero = 400; // This pulse sets the threshold for a transmission that
// represents a 0
int ret[2]; // Used to hold results from IR sensing.
void setup() {
pinMode(blinkPin, 13);
pinMode(speakerPin, 12);
pinMode(sensorPin, 2);
for (int i = 1;i < 4;i++) {
digitalWrite(blinkPin, HIGH);
playTone(900*i, 200);
digitalWrite(blinkPin, LOW);
delay(200);
}
Serial.begin(9600);
Serial.println("Ready: ");
}
void senseIR() {
int who[4];
int what[4];
while(pulseIn(sensorPin, LOW) < startBit) {
digitalWrite(blinkPin, LOW);
}
digitalWrite(blinkPin, HIGH);
who[0] = pulseIn(sensorPin, LOW);
who[1] = pulseIn(sensorPin, LOW);
who[2] = pulseIn(sensorPin, LOW);
who[3] = pulseIn(sensorPin, LOW);
what[0] = pulseIn(sensorPin, LOW);
what[1] = pulseIn(sensorPin, LOW);
what[2] = pulseIn(sensorPin, LOW);
what[3] = pulseIn(sensorPin, LOW);
Serial.println("---who---");
for(int i=0;i<=3;i++) {
Serial.println(who
);
if(who > one) {
who = 1;
} else if (who > zero) {
who = 0;
} else {
// Since the data is neither zero or one, we have an error
Serial.println("unknown player");
ret[0] = -1;
return;
}
}
ret[4]=convert(who);
Serial.println(ret[4]);
Serial.println("---what---");
for(int i=0;i<=3;i++) {
Serial.println(what);
if(what > one) {
what = 1;
} else if (what > zero) {
what = 0;
} else {
// Since the data is neither zero or one, we have an error
Serial.println("unknown action");
ret[0] = -1;
return;
}
}
ret[1]=convert(what);
Serial.println(ret[1]);
return;
}
int convert(int bits[9]) {
int result = 0;
int seed = 1;
for(int i=3;i>=0;i--) {
if(bits == 1) {
result += seed;
}
seed = seed * 2;
}
return result;
}
void loop() {
senseIR();
if (ret[0] != -1) {
playTone(1000, 50);
Serial.print("Who: ");
Serial.print(ret[0]);
Serial.print(" What: ");
Serial.println(ret[1]);
}
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
大家好, 我想問上列的程式要如何修改才有下圖(Table 1 )的結果? 謝謝各位.
Table 1
---who---
681
1282
686
682
4
---what---
1282
682
683
1278
9
Who: 4 What: 9
該程式的網址: 訪客無法瀏覽此圖片或連結,請先 註冊 或 登入會員 。
他的檢查提示看不太懂 " 2. 在您的 IR 感測器上指定測試遙控裝置並開始按下按鈕。"
各位大大, 這問題我已經苦惱了两個星期, 有誰能破解嗎?