Skip to content

Instantly share code, notes, and snippets.

@mattwilliamson
Created October 4, 2010 18:22
Show Gist options
  • Save mattwilliamson/610187 to your computer and use it in GitHub Desktop.
Save mattwilliamson/610187 to your computer and use it in GitHub Desktop.
#define CODE_COUNT 2
#define MIN_ROTATION 150
#define MAX_ROTATION 30
int val = 0;
char code[10];
int bytesread = 0;
int ledPin = 13;
int speakerOut = 5;
char *goodCode = "0F03042E80";
int Cnote = 1915;
int Dnote = 1000;
#include <Servo.h>
Servo servo;
void soundwave(int note, int howHard ) { // function that takes 2 parameters, the note we want to play and how hard the sensor has been hit
unsigned long endSoundWave = micros() + (35000 * howHard); // start a count from the microseconds currently registered since the program first ran. And add on an arbitary value multiplied
y howHard value
while(micros() < endSoundWave){ // while the count is not reached
analogWrite(speakerOut, 1023); // set the speaker to on/ high
delayMicroseconds(note); // wait the number of microseconds for the note
analogWrite(speakerOut, 0); // set the speaker to off/ low
delayMicroseconds(note); // wait for the same number again to complete our oscillation/ soundwave.
} // repeat for the length of time.
}
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID reader
pinMode(ledPin, OUTPUT);
servo.attach(3);
pinMode(speakerOut,OUTPUT);
analogWrite(speakerOut, 0);
}
void loop() {
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
//Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
boolean success = true;
for(int i=0;i<10;i++) {
if(goodCode[i] != code[i]) {
success = false;
break;
}
}
if(success){
digitalWrite(ledPin, HIGH);
soundwave(500, 5);
servo.write(MAX_ROTATION);
delay(1000);
analogWrite(speakerOut, 0);
}
else {
soundwave(2000, 20);
}
}
bytesread = 0;
delay(500);
}
}
digitalWrite(ledPin, LOW);
servo.write(MIN_ROTATION);
analogWrite(speakerOut, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment