Albert library
Here is my Albert library which I use in all my Arduino programs, download HERE at GitHub.
It contains useful macros and functions.
For troubleshooting see HERE.
Serial vs SerialUSB
Most Arduino sketches, which are written for the Arduino Uno, use the Serial.print() to print to the serial monitor. The Arduino Zero, which uses the SAMD21, requires SerialUSB.print() instead, if you want to print to the native USB port.
This little code helps, you can use Serial.print() for the Arduino Zero too. You don't have to modify Arduino sketches Zero anymore for using it on the SAMD21.
#if defined(__arm__) #define Serial SerialUSB #endif
maxLoops() to stop the Arduino
Arduino formerly contained a stop button to stop the serial monitor. This button is gone unfortunately, so now you have to pull out the USB plug to stop the Arduino sending continuously data to the serial monitor. With the function maxLoops you can specify the maximum number of loops, so the serial monitor doesn't fills up anymore.
void loop(void) { maxLoops(3); Serial.print("\nbla"); }
SimpleSoftPWM
A PWM output can be created without the use of a hardware timer, which is convenient because the number of timers is limited. The output frequency is just 50 Hz, and it can therefore be used for applications which do not require high-speed PWM, such as LED or heating control.
const byte LedPin = 12; SimpleSoftPWM pwm1; void setup(void) { } void loop(void) { digitalWrite(LedPin, pwm1.pwm(180)); // dimmed LED 180/255 }
blinkLed
blinkLed(ledPin, 3, 1); // blinks 3x
The LED blinks standard 3 times.
Arduino open drain output
openDrain(byte pin, bool value);
The Arduino has no open drain or open collector outputs but these are easy to create by software. When the output is high, the impedance must be high; this is done by making the pinmode INPUT. When the output is low, it must be 0V; this is done by making the pinmode OUTPUT and the output level LOW.
availableRAM
availableRAM();
Calculates the available RAM in the Atmega.
Print operator ,
Arduino programs can be debugged by printing values to the serial monitor:
Serial << i << ", " << j << ", " << k;
Now we can use a comma instead of " " which requires less typing work:
Serial << i, j, k;
Using a macro to determine the length of an array
Generally, a constant value is used to hold the size of an array. However, this is risky. The value can be wrong, which cause nasty bugs:
const int wattTable[] = {0, 125, 250}; for(int i=0; i<3; i++) {} // this is risky
It is safer to calculate the array size with sizeof instead of using a constant. Because the calculation is done during compile-time, no extra code is generated.
#define arrayLenght(array) sizeof(array)/sizeof(array[0]) // compile-time calculation for(int i=0; i<arrayLenght(wattTable); i++) {}
Examples Arduino library Albert
#include <Streaming.h> #include "Albert.h" const byte ledPin = 3; const byte openDrainPin = 2; int array[10]; SimpleSoftPWM pwm1; void setup(void) { Serial.begin(9600); // Serial instead of SerialUSB for SAMD21 #if defined(__arm__) while(!Serial); // only for SAMD2, open Serial Monitor first #endif Serial << F("available RAM = ") << availableRAM(); Serial << "\ntest comma use , instead of << \" \" << ", 1234, 48, 0, 89; Serial << "\narrayLenght = " << arrayLenght(array); blinkLed(ledPin, 3, 0); // blinks 3x onLevel=0 openDrain(openDrainPin, 0); } void loop(void) { static int i; //maxLoops(3); // stop after 3 loops Serial << "\n" << i++; digitalWrite(ledPin, pwm1.pwm(180)); // LED is dimmed 180/255 }