Go Down

Topic: New, Easy, Arduino to shift register LIBRARY (Read 1 time) previous topic - next topic

spycatcher2k

Apr 30, 2010, 02:06 am Last Edit: Apr 30, 2010, 09:17 pm by spycatcher2k Reason: 1
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
Code: [Select]

/*
 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

Code: [Select]

/*
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

Code: [Select]

#######################################
# Syntax Coloring Map For binstop
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

fade      KEYWORD1
allon      KEYWORD1
alloff      KEYWORD1
set      KEYWORD1


The example (For example sonly so not optimised)

Code: [Select]

/*

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.uk

This 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
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

spycatcher2k

ERROR _ ERROR _ ERROR :

// Lets get started :
//
// Include the required headers
//
#include "WProgram.h"
#include "binstop.h"         <--------- Should be #include "Binstop.h"
Binstop::Binstop(int latch,int clock,int data)
{


Sorry - Typo error on my part   :-[  
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

flyboy

That's pretty interesting.  I'm more interested in the library than the shift register, though.  I don't quite understand how all the files go together.  Where is a good place to get some hand-holding on how building a library is done?  I may want to try it some day.

spycatcher2k

Hi, flyboy, I'll try and knock up a mini tutorial on how I made this one. I used info from the internet (Arduino.cc , Google, and Dogpile) to learn how to create it, just got to get my notes sorted. Hopefully be done today (its 13:48 in the UK just now).

Cheers.

Spycatcher2k
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

spycatcher2k

#4
May 03, 2010, 07:24 pm Last Edit: May 03, 2010, 07:24 pm by spycatcher2k Reason: 1
Mini tutorial on making a library, we will make a mini tutorial to add two numbers
and flash an LED on selected pin.

*** I M P O R T A N T ***
Write the routine as a void [routine]() in Arduino IDE
Make sure it runs as you want it to.
(My son sat for 3 hours trying to get a library working with a bad routine!!!!!!!!!)

Make a .h header file e.g. addatobLIB.h
(I tend to use LIB at the end if the filename so I know its a LIBrary)
add these lines :

#ifndef [name of library]_h
#define [name of library]_h
#include "WProgram.h"
class [name of library]
{
public:
private:
};
#endif

So ours will be :

#ifndef addatob_h
#define addatob_h
#include "WProgram.h"
class addatobLIB
{
public:
private:
};
#endif

Now, define the variables we want to use, and define the library's command name :

I want to pass 2 int's,for easy use i'll call them apass and bpass.

#ifndef addatob_h
#define addatob_h
#include "WProgram.h"

class addatobLIB
{
public:
addatobLIB(int pin);                       //Our startup
int add(int apass,int bpass);      //int used because we are ruturning a value
int minus(int apass,int bpass);      //as above

private:
int _pin;
int _apass;
int _bpass;
int _total;
};
#endif

save this file as addatob.h in the library\addato folder in your arduino directory

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Now for the .cpp file

Create a file called addatob.cpp

Add these to our file :

#include "addatobLIB.h"
#include "WProgram.h"
addatobLIB::addatobLIB(int pin)
{
//Initialise the output pin for our routine
_pin = pin;
pinMode(pin, OUTPUT);
}

Now lets add the routines to add and minus :

#include "addatob.h"
#include "WProgram.h"
addatobLIB::addatobLIB(pin)
{
//Initialise the output pin for our routine
_pin = pin;
pinMode(_pin, OUTPUT);
}

//Almost a complete copy and paste from your working program

int addatobLIB::add(int apass,int bpass)        // This line was void add(int apass,int bpass)
                             // We use int here, as we will be returning _total later.
{
     _apass=apass;
     _bpass=bpass;
     _total=((_apass)+(_bpass));
for(int z=0:z<(_total):z++)
{
digitalWrite(_pin,HIGH);
delay(500);
digitalWrire(_pin,LOW);
delay(500);
}
return (_total);
}

int addatobLIB::minus(int apass,int bpass)// This line was void add(int apass,int bpass);
                             // We use int here, as we will be returning _total later.
{
     _apass=apass;
     _bpass=bpass;
     _total=((_apass) - (_bpass));
for(int z=0;z<(_total);z++)
{
digitalWrite(_pin,HIGH);
delay(500);
digitalWrite(_pin,LOW);
delay(500);
}
return (_total);
}

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Just an example to go :
(If you save this in a folder called examples inside your library's folder, it will
be added to the menu in the IDE)

#include <addatobLIB.h>  // Add the Library for use

addatobLIB mat(13);        // Start the library and create an object called mat, set pin 13 for output


void setup()
{
Serial.begin(57600);
}
void loop()
{
 int pool = mat.add(3,4);  // pool is equal to 3+4 using library function add

 Serial.println(pool);        // Output value to serial port
while(1){}                       // Stop here forever
}

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

What about adding keywords :

add a file called keywords.txt

add this to it :


#######################################
# Syntax Coloring Map For binstop
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

add      KEYWORD1
minus      KEYWORD1


(The space between add and minus & KEYWORD1 is a TAB)
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy