Home > other >  Infrared sensor speed, output a high level of change, but the Arduino to read more
Infrared sensor speed, output a high level of change, but the Arduino to read more

Time:11-21

Everybody is good, novice,
Thinking of arduino infrared obstacle avoidance module identification white stickers, realize speed function,
The code is as follows:
 int LED=13;//Use the onboard Uno 
Volatile float n=0;
Float dt;
Float Speed;
Float begin_time;
Const byte interruptPin=2;//2 pins as the interrupt trigger pin


Void setup () {
PinMode (LED, the OUTPUT);
PinMode (interruptPin, INPUT);

/*
* parameter 1
Interrupt - interrupt source, 0 corresponding 2 pin; 1 corresponding to 3 pins and so on, here we write 0, namely by external interrupt pin 2 pin,

Parameter 2
Function - the interrupt handler, the interrupt occurs, what to do, here we make it happen is interrupted, do n +=1, is used to count pulse, the number of times a level change

Parameter 3
Mode - trigger mode, the trigger mode there are four types: LOW (LOW level trigger), CHANGE (level CHANGE triggers), RISING (trigger) LOW level to high levels, FALLING (trigger) high level to LOW levels,
Here we choose to CHANGE (level CHANGE triggers), infrared sensor is sensitive to white reflective patches on the wheels, wheel a circle evenly with article 6 reflective, wheel on a business trip, there will be six level sensor output
*/
AttachInterrupt (digitalPinToInterrupt (interruptPin), count_pulse, FALLING);
Begin_time=millis ();
Serial.begin(9600);
}

Void count_pulse () {
Serial. Println (" I am being called ");
N +=1;
}

Void loop () {
Dt=millis () - begin_time;
/* every 500 ms output rotational speed, and start calculating the speed of the next 500 ms */
If (dt & gt; 1000.0) {
//every circle has six pulse, the return value of the following for RPM
//delayMicroseconds (50);
Serial. Print (" pulse number: ");
Serial. Println (n);
Serial. Print (" dt: ");
Serial. Println (dt);
Serial. Print (" speed, ");
Speed=(n/6)/(dt/60000);
Serial. Println (Speed);
N=0;
Begin_time=millis ();
Serial. Flush ();//sure have to print out

}
}


 0:08:40. 398 - & gt; Dt: 1001.00 
10:08:40. 398 - & gt; Speed:
0.0010:08:41. 358 - & gt; Pulse number: 0.00
10:08:41. 398 - & gt; Dt: 1001.00
10:08:41. 398 - & gt; Speed:
0.0010:08:41. 518 - & gt; I am being called
10:08:41. 518 - & gt; I am being called
10:08:41. 558 - & gt; I am being called
10:08:41. 558 - & gt; I am being called
10:08:41. 598 - & gt; I am being called
10:08:41. 638 - & gt; I am being called
10:08:41. 678 - & gt; I am being called
10:08:41. 678 - & gt; I am being called
10:08:41. 718 - & gt; I am being called
10:08:41. 718 - & gt; I am being called
10:08:41. 758 - & gt; I am being called
10:08:41. 758 - & gt; I am being called
10:08:41. 877 - & gt; I am being called
10:08:41. 917 - & gt; I am being called
10:08:41. 917 - & gt; I am being called
10:08:41. 957 - & gt; I am being called
10:08:41. 957 - & gt; I am being called
10:08:42. 198 - & gt; I am being called
10:08:42. 198 - & gt; I am being called
10:08:42. 198 - & gt; I am being called
10:08:42. 238 - & gt; I am being called
10:08:42. 238 - & gt; I am being called
10:08:42. 358 - & gt; I am being called
10:08:42. 398 - & gt; I am being called
10:08:42. 478 - & gt; Pulse number: 24.00
10:08:42. 478 - & gt; Dt: 1001.00
10:08:42. 518 - & gt; Speed:
239.7610:08:42. 557 - & gt; I am being called
10:08:42. 557 - & gt; I am being called
10:08:42. 597 - & gt; I am being called
10:08:42. 597 - & gt; I am being called
10:08:42. 637 - & gt; I am being called
10:08:42. 637 - & gt; I am being called
10:08:42. 677 - & gt; I am being called
10:08:42. 677 - & gt; I am being called
10:08:42. 717 - & gt; I am being called
10:08:43. 118 - & gt; I am being called
10:08:43. 158 - & gt; I am being called
10:08:43. 158 - & gt; I am being called
10:08:43. 198 - & gt; I am being called
10:08:43. 198 - & gt; I am being called
10:08:43. 238 - & gt; I am being called
10:08:43. 238 - & gt; I am being called
10:08:43. 278 - & gt; I am being called
10:08:43. 358 - & gt; I am being called
10:08:43. 398 - & gt; I am being called
10:08:43. 398 - & gt; I am being called
10:08:43. 438 - & gt; I am being called
10:08:43. 438 - & gt; I am being called
10:08:43. 478 - & gt; I am being called
10:08:43. 478 - & gt; I am being called
10:08:43. 518 - & gt; I am being called
10:08:43. 598 - & gt; Pulse number: 25.00
10:08:43. 638 - & gt; Dt: 1001.00
10:08:43. 638 - & gt; Speed:
249.7510:08:43. 678 - & gt; I am being called
10:08:43. 678 - & gt; I am being called
10:08:43. 718 - & gt; I am being called
10:08:43. 718 - & gt; I am being called
10:08:43. 758 - & gt; I am being called
10:08:44. 638 - & gt; Pulse number: 5.00
10:08:44. 638 - & gt; Dt: 1001.00
10:08:44. 678 - & gt; Speed:
49.9510:08:45. 638 - & gt; Pulse number: 0.00
10:08:45. 678 - & gt; Dt: 1001.00
10:08:45. 678 - & gt; Speed:
0.0010:08:46. 638 - & gt; Pulse number: 0.00

Output pulse number obviously false, and to identify the white sticker, continuous output pulse,
But by view of waveform, the data acquisition card in a week to identify the rotation of the wheels of six white paper, just six pulse output, and the arduino output pulse number as shown above, is not high and low level change and add 1
I would appreciate your request great god give advice or comments!
  • Related