Skip to content

Instantly share code, notes, and snippets.

@jcastaneyra
Created January 3, 2012 19:50
Show Gist options
  • Save jcastaneyra/1556573 to your computer and use it in GitHub Desktop.
Save jcastaneyra/1556573 to your computer and use it in GitHub Desktop.
Seeeduino Stalker Xbee Router
int buzzPin = 11;
void setup() {
Serial.begin(57600);
pinMode(buzzPin, OUTPUT);
}
void loop() {
if(Serial.available() > 0) {
char c = Serial.read();
if(c == '1') {
buzz(buzzPin, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
Serial.flush();
delay(2000); // wait a bit between buzzes
}
}
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment