Utility - foreach

I will update this thread everytime I create a general purpose utility for the Arduino.


foreach

Ever wanted to simply make all elements in an array digital outputs?

Tired of using for loops every time you simply need to do something similar on all pins?

This is the answer.

http://www.arduino.cc/playground/Code/Utility - Download

Simple Blink Example:

#include <Utility.h>

const byte NUMBER_OF_PINS = 4;
byte ledPin[NUMBER_OF_PINS] = {10,11,12,13};

void setup(){
foreach(ledPin, NUMBER_OF_PINS, pinMode, OUTPUT); //set all pins as OUTPUT
}

void loop(){
foreach(ledPin, NUMBER_OF_PINS, digitalWrite, HIGH); //set all pins HIGH
delay(1000);

foreach(ledPin, NUMBER_OF_PINS, digitalWrite, LOW); //set all pins LOW
delay(1000);

//manipulate pins from index 1 to index 3, inclusive
foreach(ledPin, 1, 3, digitalWrite, HIGH);//set pin 11,12 and 13 HIGH
delay(500);

//manipulate pins from index 1 to index 2, inclusive
foreach(ledPin, 1, 2, digitalWrite, LOW);//set pin 11 and 12 LOW
delay(500);
}