Albert library

Here is my Albert library which I use in all my Arduino programs, download HERE.
For troubleshooting see HERE

analogReadfast

See Fast-10-bit-adc.

adcin

Equal to analogread() but with averaging.

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");
}

heartbeat

This is copied from the example ArduinoISP. With a too small LED resistance we can't see the heartbeat effect, take about 500Ohm.

blinkLed

blinkLed(byte pin, int n=3);
The LED blinks 3 times.

Arduino open drain output

Opencollector
Arduino open collector 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 = 9;
const byte openDrainPin = 3;
int i, j, k;

void setup(void) 
{ Serial.begin(9600); 
  Serial << F("Available RAM: ") << availableRAM(), i, j, k; // result: "Available RAM: 1789 0 0 0"
  blinkLed(LedPin);
  openDrain(openDrainPin, LOW);
}

void loop(void) 
{ heartbeat(LedPin);
  maxLoops(1000); // stop at 1000 loops
  Serial << endl, adcIn(0, 100); 
}

 

Do you have any comments? Please let me know.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.