Tips-n-tricks for the Arduino
I have put together some useful snippets of information from experiments and crawling the web for ideas and solutions to problems I have encountered on various projects.. enjoy!
Checking Memory usage
Including the following code above the void setup() method allows you to get back the free memory available. I usually use a Serial.println() command to dump it out so I can see whats happening memory wise as things are running.
The Code:
extern int __bss_end;
extern void *__brkval;
int memoryFree()
{
int freeValue;
if((int)__brkval == 0)
freeValue = ((int)&freeValue) – ((int)&__bss_end);
else
freeValue = ((int)&freeValue) – ((int)__brkval);
return freeValue;
}
Using It:
void setup()
{
Serial.begin(9600);
Serial.print(“MEM (before): “);
Serial.print(memoryFree()); // print the free memory
Serial.println(” bytes”);
….
}
Four useful links for the nRF24L01+
The nRF24L01+ is a versatile RF transceiver operating in the ISM band, here are 4 useful links to sites with information from beginner to advanced.
- http://arduino-info.wikispaces.com/nRF24L01-RF24-Examples
- http://playground.arduino.cc/InterfacingWithHardware/Nrf24L01
- http://maniacbug.github.io/RF24/
- http://tmrh20.github.io/RF24/ <- new improved library compared to the Maniacbug imlementation
DF-Robot LCD Initialization
I found that the standard LCD initialization code does not work on my DF robot LCD. The correct constructor is:
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
Selective header file import based on IDE version
Simple MACRO to include correct header:
#if defined(ARDUINO) && ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif
Basic Input de-bouncing
Simple digital input de-bounce code, locks the program into a loop during debounce so its useable in simple apps only. A better version would be to enter on a 1ms timer, check and inc counters and then recheck on next 1ms entry. 15-20ms of stable state would be considered a correct and stable switch state. Also, ditch the boolean return and make it void, use a getState() method to give you the current state (not raw state from the switch).
const int inputPin = 2; // the number of the input pin const int debounceDelay = 20; // milliseconds to wait until stable // debounce returns the stable switch state boolean debounce(int pin) { boolean state; boolean previousState; previousState = digitalRead(pin); // store switch state for(int counter=0; counter < debounceDelay; counter++) { delay(1); // wait for 1 millisecond state = digitalRead(pin); // read the pin if( state != previousState) { counter = 0; // reset the counter if the state changes previousState = state; // and save the current state } } return state; }
Input de-bounce using interupt
User submitted code to do it via an interrupt, could have more checks and a stable counter logic but the concept of using an ISR and entering on an interrupt should be expanded on.
/* * ISR Debounce */ // use 2ms debounce time #define DEBOUNCE_TICKS (word)microsecondsToClockCycles(2000) extern volatile unsigned long timer0_overflow_count; word keytick; // record time of keypress void setup() { attachInterrupt(0,KeyPress,FALLING); } void KeyPress() { keytick=(word)timer0_overflow_count; } // returns true if key pressed boolean KeyCheck() { if(keytick!=0) { if (((word)timer0_overflow_count-keytick)>DEBOUNCE_TICKS) { keytick=0; return true; } } return false; } // TEST CODE void loop() { if (KeyCheck()) { // do something useful } }
Enjoy!
-oOo-