3 - Overview of the library (Part 1)

Submitted by Webbot on April 27, 2008 - 11:37am.


A class for defining input pins that can be either high or low.

You can create a new one at the start of your code by:

IN myPin('B',4,false); // creates an input on B4 with no pullup resistor

You test if a pin is low or high as follows:-


// The pin is high




// The pin is Low




A class for defining output pins that can be set high or low.

You can create a new one at the start of your code by:

OUT myPin('B',4,false); // creates an output on B4 and set it to low to start with

You can then change it to low or high with

myPin.SetLow(); // set it low

myPin.SetHigh(); // set it high

myPin.Set(true); // set it high

myPin.Set(false); // set it low

myPin.Toggle(); // If it was low set it high, if it was high then set it low



This is a simple extension of OUT that has more friendly methods On() and Off(). You can also use the methods from OUT ie Toggle() will change the state of the LED.



delay_cycles(volatile unsigned int cycles) is the same as the delay loop from the original tutorial in that it just pauses for a given delay.


int interpolate(int value, int minVal, int maxVal, int minRtn, int maxRtn) is a useful routine. This takes a 'value' that is known to be between 'minVal' and 'maxVal' and then returns a number in the range 'minRtn' and 'maxRtn'. So if you wanted to convert a number which is in the range 0 to 100 into a matching number in the range -255 to 255 you could just do:

int answer = interpolate(value, 0, 100, -255, 255);


int clamp(int value, int minVal. int maxVal); Will limit a value to the range minVal to maxVal. This is the same as writing:

if(value < minVal) value = minVal;

if(value > maxVal) value = maxVal;


The macros 'CRITICAL_SECTION_START' and 'CRITICAL_SECTION_END' can be used to surround a piece of time critical code. Since it disables interrupts then it shouldn't be used if the surrounded code takes a long time to run as you may loose some other critical interrupts.



You will normally create a timer at the start of your main code - see my Photovore code. And you should only create one of them! This implementation uses Timer2 and provides the following methods:-

unsigned long MsCur(); returns the number of milliseconds since power was switched on. Since an unsigned long can store a high value then it provides an effective 'time of day' clock. NB If your robot can be left switched on for a long period then you will have to take into account that this value may wrap around to 0.


bool HasElapsed(unsigned long msStart, unsigned in msWait); This will test if a given amount of time has passed. 'msStart' is a value you saved previously by calling MsCur() and 'msWait' is the number of milliseconds you want to check to see if they have passed. The method will return immediately with either 'false' if the period hasn't elapsed or 'true' if it has. This is useful with sensors such as sonars which should not be called without a reasonable delay between samplings so as to avoid ghost echoes. You can guard against this by saving the time when the sonar was last sampled. The next time it is called you can check that the minimum amount of time has elapsed since the last call and, if not, you could return the previously returned value.


void WaitMs(unsigned int ms); This will not return until the given number of milliseconds has elapsed. Note that this may be out by 1ms either way so it's not good for small numbers.


void WaitMs(unsigned int ms, unsigned long msStart); is similar to 'HasElapsed' except that it will not return until the given period has elapsed.



This is an extension of IN but for input pins that can do analogue to digital conversion. To create a new input pin on pin 4 just use:

A2D myIn(4);

NB This assumes port A (which is fine for Mega8 controllers) so attach your device to A4.


You can then read an 8 bit value via:

unsigned char value = myIn.ReadByte();


or a 10 bit value via:

unsigned short value = myIn.ReadShort();


Notice that you DON'T need to initialise the A2D convertor at all. Look at the file you will see the Init method that, by default, is called with some default values. If you want to use other values then you can call this Init method yourself BEFORE you create any A2D inputs. Since this is a static method it can be done by writing:



If you need to save power and turn off the A2D function then call:


You can turn it back on later by calling A2D.Init again.