Sounding a Piezo Buzzer
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 again for the calculated delay value
}
}
Check Amount Of Free RAM
// this function will return the number of bytes currently free in RAM
int memoryTest() {
int byteCounter = 0; // initialize a counter
byte *byteArray; // create a pointer to a byte array
// More on pointers here: http://en.wikipedia.org/wiki/Pointer#C_pointers (external link)
// use the malloc function to repeatedly attempt
// allocating a certain number of bytes to memory
// More on malloc here: http://en.wikipedia.org/wiki/Malloc (external link)
while ( (byteArray = (byte*) malloc (byteCounter * sizeof(byte))) != NULL ) {
byteCounter++; // if allocation was successful, then up the count for the next try
free(byteArray); // free memory after allocating it
}
free(byteArray); // also free memory after the function finishes
return byteCounter; // send back the highest number of bytes successfully allocated
}
Display the value of a 10K potentimeter to the serial console.
/*
Display the value of a 10K potentimeter to the serial console.
Copyright: Systemchimp.com 2007
*/
/* The analog pin connect to pot. */
int p1 = 2;
int p1_val = 0;
void setup() {
pinMode(p1, INPUT);
Serial.begin(9600);
}
void loop() {
p1_val = analogRead(p1);
Serial.print("P1=");
Serial.println(p1_val, DEC);
delay(1000);
}
0 Comments