Advertisement
Claudio_Junior

Projeto de sensor de proximidade utilizando infravermelho

May 16th, 2013
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. // Fonte do codigo http://labdegaragem.com/
  2. #include <Tone.h> //Include the library
  3.  
  4. int IRpin= A0; //IR's pin
  5. int IRval=0; //The value of IR
  6. int threshold=100; //The threshold to activate the IR
  7. int error = 10; //The error of IR
  8. int temp[20]={0}; //Store the samples
  9. int x=0; //Counter to temp
  10. int y=0; //Counter to approaching
  11. int z=0; //Counter to go away
  12. int last = 0; //The last sample
  13. int derivative = 0; //The derivative of samples
  14. Tone player; //To play different tones
  15. int note[]={NOTE_A3,NOTE_G4}; //The notes to play
  16.  
  17. void setup()
  18.  
  19. {
  20.     Serial.begin(9600);
  21.     pinMode(2, OUTPUT);
  22.     pinMode(3, OUTPUT);
  23.     player.begin(11);
  24. }
  25.  
  26. void loop()
  27. {
  28.     x=0;
  29.     y=0;
  30.     z=0;
  31.     if(analogRead(IRpin)>=threshold){
  32.         do{
  33.                 IRval=analogRead(IRpin);
  34.                 derivative = IRval - last;
  35.                 last = IRval;
  36.                 temp[x]=derivative;
  37.                 Serial.println(temp[x]);
  38.                 //Serial.println(IRval);
  39.                 x++;
  40.                 delay(75);
  41.             }while(x<20);
  42.         x=0;
  43.         for(x=0;x<20;x++){
  44.                 if(temp[x]>=error)
  45.             {
  46.         y=y+1;
  47.             }
  48.                 if(temp[x]<-error){
  49.                 z=z+1;
  50.             }
  51.             }
  52.                 Serial.print("approaching y=");
  53.                 Serial.println(y);
  54.                 Serial.print("go away z=");
  55.                 Serial.println(z);
  56.                 if(y>z && y>3){
  57.         Serial.println("Approaching");
  58.         digitalWrite(2, HIGH);
  59.         player.play(note[0]);
  60.         delay(200);
  61.         player.play(note[1]);
  62.         delay(200);
  63.         player.stop();
  64.         }
  65.         else if(z>y && z>3){
  66.             Serial.println("Go away");
  67.             digitalWrite(3, HIGH);
  68.             player.play(note[1]);
  69.             delay(200);
  70.             player.play(note[0]);
  71.             delay(200);
  72.             player.stop();
  73.         }
  74.     delay(200);
  75.     digitalWrite(2, LOW);
  76.     digitalWrite(3, LOW);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement