I'm using the 74hc595 serial to parallel quite a bit in various projects (Great for giving loads of outputs) in connection with LEDs, for indication of status, or via transistor/diode/relay for mains & other high voltage(i.e. over 5 volt.). To save re-writing the same piece of code time and time again, I learned how to create a library. So here it is :
4 parts (3 for library, to go into a folder called Binstop in your libraries folder) and an example.
Binstop.h
/*
Binstop (Binary Serial TO Parallel) Library
Copyright (C) 2010 Drew Anderson All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
*/
#ifndef Binstop_h
#define Binstop_h
#include "WProgram.h"
class Binstop
{
public:
Binstop(int latch,int clock, int data);
void fade(int bin3,int bin4,int counter);
void allon();
void alloff();
void set(int bin5);
private:
int _latch;
int _clock;
int _data;
int _bin3;
int _bin4;
int _bin5;
int _time;
int _counter;
};
#endif
Binstop.cpp
/*
Library for output control of 74HC595 Serial to
parallel shifter.
Binstop (Binary Serial TO Parallel) Library
Copyright (C) 2010 Drew Anderson All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
Commands
Binstop ***(latch,clock,data);
***.fade(mask,end,time);
***.allon();
***.alloff();
***.set(mask);
All self evident
Released to the wilds, use and abuse any way you want.
*/
// Lets get started :
//
// Include the required headers
//
#include "WProgram.h"
#include "binstop.h"
Binstop::Binstop(int latch,int clock,int data)
{
_latch = latch;
_clock = clock;
_data = data;
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, OUTPUT);
//
//Fault Finding
//
//Serial.begin(9600);
//Serial.println("library started");
}
//
// Turn on bits to supplied mask
//
void Binstop::set(int bin5)
{
_bin5 = bin5;
shiftOut(_data, _clock, MSBFIRST, _bin5);
digitalWrite(_latch, LOW);
digitalWrite(_latch, HIGH);
}
//
// Turn all 8 bits LOW
//
void Binstop::alloff()
{
shiftOut(_data, _clock, MSBFIRST, 0);
digitalWrite(_latch, LOW);
digitalWrite(_latch, HIGH);
}
//
// Turn all 8 bits HIGH
//
void Binstop::allon()
{
shiftOut(_data, _clock, MSBFIRST, 255);
digitalWrite(_latch, LOW);
digitalWrite(_latch, HIGH);
}
//
// Fade DOWN Routine
//
void Binstop::fade(int bin3,int bin4,int counter)
{
_bin3=bin3;
_bin4=bin4;
_counter=counter;
//Fade Down Startup Routine
for(int x=0;x<(_counter);x++)
{
for(int y=0;y<4;y++)
{
shiftOut(_data, _clock, MSBFIRST, _bin3);
digitalWrite(_latch, LOW);
digitalWrite(_latch, HIGH);
delay(((_counter)-1)-x);
shiftOut(_data, _clock, MSBFIRST, _bin4);
digitalWrite(_latch, LOW);
digitalWrite(_latch, HIGH);
delay(x);
}
}
}
keywords.txt
#######################################
# Syntax Coloring Map For binstop
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
fade KEYWORD1
allon KEYWORD1
alloff KEYWORD1
set KEYWORD1
The example (For example sonly so not optimised)
/*
Example program demonstrating the effects package for the 74HC595
chip (can use other shifters).
Syntax :
Binstop ***(Latch pin,Clock pin, Data pin)
Initalise the library and pass pin out.
***.fade(From pattern,To pattern, cycles);
fade between 2 values, over a number of cycles.
***.allon();
Turn on all outputs to HIGH
***.alloff();
Turn on all outputs to LOW
***.set(data);
Set output to supplied mask, e.g. B00100110
would turn on bits 3,6 and 7.
*/
#include <Binstop.h>
Binstop led(2,3,4);
int x=7; // Strobing Speed
void setup()
{
led.fade(0,255,20); //Fade up all LEDs
led.fade(255,0,20); //Fade down all LEDs
delay(500);
}
void loop()
{
led.fade(B00000011,B00000001,x);
led.fade(B00000011,B00000010,x);
led.fade(B00000110,B00000100,x);
led.fade(B00001100,B00001000,x);
led.fade(B00011000,B00010000,x);
led.fade(B00110000,B00100000,x);
led.fade(B01100000,B01000000,x);
led.fade(B11000000,B10000000,x);
led.fade(B11000000,B01000000,x);
led.fade(B01100000,B00100000,x);
led.fade(B00110000,B00010000,x);
led.fade(B00011000,B00001000,x);
led.fade(B00001100,B00000100,x);
led.fade(B00000110,B00000010,x);
}
Again, if you download this and use it please leave a reply, or
E-Mail me
arduino02@badnetwork.co.ukThis will only be used for keeping a count, or you can also email me
at that address, if you have any questions regarding my code.
Cheers Arduinees
Spycatcher2k