Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fonte do codigo http://labdegaragem.com/
- #include <Tone.h> //Include the library
- int IRpin= A0; //IR's pin
- int IRval=0; //The value of IR
- int threshold=100; //The threshold to activate the IR
- int error = 10; //The error of IR
- int temp[20]={0}; //Store the samples
- int x=0; //Counter to temp
- int y=0; //Counter to approaching
- int z=0; //Counter to go away
- int last = 0; //The last sample
- int derivative = 0; //The derivative of samples
- Tone player; //To play different tones
- int note[]={NOTE_A3,NOTE_G4}; //The notes to play
- void setup()
- {
- Serial.begin(9600);
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- player.begin(11);
- }
- void loop()
- {
- x=0;
- y=0;
- z=0;
- if(analogRead(IRpin)>=threshold){
- do{
- IRval=analogRead(IRpin);
- derivative = IRval - last;
- last = IRval;
- temp[x]=derivative;
- Serial.println(temp[x]);
- //Serial.println(IRval);
- x++;
- delay(75);
- }while(x<20);
- x=0;
- for(x=0;x<20;x++){
- if(temp[x]>=error)
- {
- y=y+1;
- }
- if(temp[x]<-error){
- z=z+1;
- }
- }
- Serial.print("approaching y=");
- Serial.println(y);
- Serial.print("go away z=");
- Serial.println(z);
- if(y>z && y>3){
- Serial.println("Approaching");
- digitalWrite(2, HIGH);
- player.play(note[0]);
- delay(200);
- player.play(note[1]);
- delay(200);
- player.stop();
- }
- else if(z>y && z>3){
- Serial.println("Go away");
- digitalWrite(3, HIGH);
- player.play(note[1]);
- delay(200);
- player.play(note[0]);
- delay(200);
- player.stop();
- }
- delay(200);
- digitalWrite(2, LOW);
- digitalWrite(3, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement