Dear All, I just created this free library that allows you to have 16 inputs and/or outputs with 3 I/O pins and a few extra cheap chips.
Here's the download URL:
http://www.wusik.com/download/arduino/Multi_IO.zip
And here's the code: (header, there's no cpp file)
/*
www.Wusik.com - Created by WilliamK @ Wusik Dot Com (c) 2010
*/
#ifndef MULTIIO_h
#define MULTIIO_h
#include <inttypes.h>
#include "HardwareSerial.h"
// 16 Inputs with only 3 pins on the Arduino using the following chip: 74HC165N //
// http://www.sparkfun.com/products/9519 //
#define BTploadPin 25 // Buttons Input C165N - Connects to Parallel load pin the 165
#define BTdataPin 27 // Buttons Input C165N - Connects to the Q7 pin the 165
#define BTclockPin 26 // Buttons Input C165N - Connects to the Clock pin the 165
// 16 Outputs with only 3 pins on the Arduino using the following chip: 74HC595 //
// http://www.sparkfun.com/products/733 //
#define LEDlatchPin 22 // LEDs Output C595N //
#define LEDclockPin 24 // LEDs Output C595N //
#define LEDdataPin 23 // LEDs Output C595N //
class Buttons
{
public:
Buttons()
{
pinMode(BTploadPin, OUTPUT);
pinMode(BTclockPin, OUTPUT);
pinMode(BTdataPin, INPUT);
digitalWrite(BTclockPin, LOW);
digitalWrite(BTploadPin, HIGH);
memset(Clicked,false,sizeof(Clicked));
memset(prevHigh,false,sizeof(prevHigh));
}
bool Tick() // Returns True if something changed - check the Clicked[] variable and call Reset() after you do //
{
SomethingChanged = false;
digitalWrite(BTploadPin, LOW);
digitalWrite(BTploadPin, HIGH);
for(int i = 0; i < 16; i++)
{
tempButton = digitalRead(BTdataPin);
if (tempButton == LOW && prevHigh[15-i] == HIGH) { Clicked[15-i] = true; SomethingChanged = true; }
prevHigh[15-i] = tempButton;
digitalWrite(BTclockPin, HIGH);
digitalWrite(BTclockPin, LOW);
}
return SomethingChanged;
}
void Reset(void) { memset(Clicked,false,sizeof(Clicked)); }
boolean Clicked[16];
private:
boolean SomethingChanged;
unsigned int tempButton;
unsigned int prevHigh[16];
};
// ======================================================================================= //
class LEDs
{
public:
LEDs()
{
pinMode(LEDlatchPin, OUTPUT);
pinMode(LEDclockPin, OUTPUT);
pinMode(LEDdataPin, OUTPUT);
Reset();
}
void Tick(void) // use the Value[] variable to turn outputs on and off //
{
digitalWrite(LEDlatchPin, LOW);
digitalWrite(LEDdataPin, LOW);
digitalWrite(LEDclockPin, LOW);
for (int i=15; i>=0; i--)
{
digitalWrite(LEDclockPin, LOW);
if (Value[i]) digitalWrite(LEDdataPin, HIGH); else digitalWrite(LEDdataPin, LOW);
digitalWrite(LEDclockPin, HIGH);
digitalWrite(LEDdataPin, LOW);
}
digitalWrite(LEDclockPin, LOW);
digitalWrite(LEDlatchPin, HIGH);
}
void Reset(void) { memset(Value,false,sizeof(Value)); }
boolean Value[16];
};
#endif