Here’s some helpful functions for Arduino that I use:
Blinking an LED
// this function blinks the an LED light as many times as requested, // at the requested blinking rate void blinkLED(byte targetPin, int numBlinks, int blinkRate) { for (int i=0; i < numBlinks; i++) { digitalWrite(targetPin, HIGH); // sets the LED on delay(blinkRate); // waits for blinkRate milliseconds digitalWrite(targetPin, LOW); // sets the LED off delay(blinkRate); } }
Blinking a Number (great for visually indicating version # strings)
// blinks a sequence to indicate a number visually void blinkNumber(char* numString) { int versLength = strlen(numString); delay(200); for (int i =0 ; i < versLength; i++) { int number = numString[i] -48; if (number == 0){ blinkLED(LED_A,1,20); delay(160); } if (number > 0 && number < 10) blinkLED(LED_A,number,200); delay(400); } }
Buzzing a Buzzer (see my post about this)
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 } }
Checking Free RAM (see my post about this)
// 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 // 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 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 }
Mapping Bigger Numbers (avoids errors when mapping large positive numbers)
// version of the map function capable of handling larger positive numbers // (but fails with negative ones) long mapBig(unsigned long x, unsigned long in_min, unsigned long in_max, unsigned long out_min, unsigned long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }
Pingback: Helpful code snippets for Arduino | Embedded projects from around the web