Go Down

Topic: LEDS and 74HC595 shift register (Read 11011 times) previous topic - next topic

John_Ryan

Fixed. The following code, reads a bunch of digital switches, if a switch is pressed, then it runs away and takes a reading from the Parallax. When it's got all ten numbers, it pairs the RFID tag with the number of the switch that was pressed, then sends it over to processing.

When it's not doing that, it's checking the values of the LED's - which is a bit glitchy, but I'll have that, and a rewrite of the php, fixed tomorrow.

For anyone interested, the Parallax works with an external coil wired to it, as does the Phidgets :)  

Adruino Code Pt.1

Code: [Select]

// Switches - LED's - RFID reader
int latchPin = 8;  //Pin connected to ST_CP of 74HC595
int clockPin = 12; //Pin connected to SH_CP of 74HC595
int dataPin = 11; //Pin connected to DS of 74HC595
int inplatchPin = 10; //Pin connected to ST_CP of 7021
int inpdataPin = 9; //Pin connected to SH_CP of 7021
int inpclockPin = 7; //Pin connected to DS of 7021
int rfidPin = 2; //Pin connected to PRFID
char code[10];
char pressed[1];
int bytesread = 0;
int checkRFID = 0;
int gotOne = 0;
int readingIt = 0;
int sentSwitch = 2;
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers
byte switchVar1 = 72;  //01001000
byte lowcBitNum = 7;
byte dBitNum = 6;
byte eBitNum = 5;
byte fBitNum = 4;
byte gBitNum = 3;
byte aBitNum = 2;
byte bBitNum = 1;
byte highcNum = 0;
boolean dBit;
long time = 0;
long debounce = 4000; // big long debounce
int firstByte;
int secondByte;
int thirdByte;
int val;
int serialInArray[4];
int serialCount = 0;

void setup() {
 pinMode(latchPin, OUTPUT);
 pinMode(inplatchPin, OUTPUT);
 pinMode(inpclockPin, OUTPUT);
 pinMode(inpdataPin, INPUT);
 pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
 Serial.begin(2400);
 digitalWrite(rfidPin, LOW);  // Activate the RFID reader
 digitalWrite(rfidPin, HIGH);  // Deactivate the RFID reader
}

void checkReader() {
 digitalWrite(rfidPin, LOW);  // Activate the RFID reader
 if(Serial.available() > 0) {  // if data available from reader
   if((val = Serial.read()) == 10) { // check for header
     bytesread = 0;
     while(bytesread < 10) {  // read 10 digit code
       if( Serial.available() > 0) {
         val = Serial.read();
         if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
           break;   // stop reading
         }
         code[bytesread] = val; // add the digit  
         bytesread++;  // ready to read next digit
       }
     }
     if(bytesread == 10) {  // if 10 digit read is complete
       Serial.print(pressed[0]);  // print the Switch number
       Serial.println(code); // print the TAG code
     }
     bytesread = 0;
     checkRFID = 0;
     digitalWrite(rfidPin, HIGH);   // Deactivate the reader
     delay(1000); // Have a rest
   }
 }
}

void doSwitches() {
 if (millis() - time > debounce) {
   pressed[0]='0';
   digitalWrite(inplatchPin,1);  //Pulse the latch pin / collect parallel data
   delayMicroseconds(20);
   digitalWrite(inplatchPin,0); //set it to 0 to transmit data serially
   //collect each shift register into a byte
   //checkRFID=0;
   switchVar1 = shiftIn(inpdataPin, inpclockPin);
   if (getBit(switchVar1, highcNum)) {
     //Serial.println("0");
     time = millis();
     checkRFID=1;
     pressed[0]='0';
   }
   if (getBit(switchVar1, bBitNum)) {
     //Serial.println("1");
     time = millis();
     checkRFID=1;
     pressed[0]='1';
   }
   if (getBit(switchVar1, aBitNum)) {
     //Serial.println("2");
     time = millis();
     checkRFID=1;
     pressed[0]='2';
   }
   if (getBit(switchVar1, gBitNum)) {
     //Serial.println("3");
     time = millis();
     checkRFID=1;
     pressed[0]='3';
   }
   if (getBit(switchVar1, fBitNum)) {
     //Serial.println("4");
     time = millis();
     checkRFID=1;
     pressed[0]='4';
   }
   if (getBit(switchVar1, eBitNum)) {
     //Serial.println("5");
     time = millis();
     checkRFID=1;
     pressed[0]='5';
   }
   if (getBit(switchVar1, dBitNum)) {
     //Serial.println("6");
     time = millis();
     checkRFID=1;
     pressed[0]='6';
   }
   if (getBit(switchVar1, lowcBitNum)) {
     //Serial.println("7");
     time = millis();
     checkRFID=1;
     pressed[0]='7';
   }
 }
}

void doLeds() {
 if (millis() - time > debounce) {
   if (Serial.available() > 0) {
     serialInArray[serialCount] = Serial.read();
     serialCount++;
     if (serialCount > 3 ) {
       firstByte = serialInArray[1];
       secondByte = serialInArray[2];
       thirdByte = serialInArray[3];
       digitalWrite(latchPin, 0); // ground latchPin and hold low during transmit
       shiftOut(dataPin, clockPin, firstByte);
       shiftOut(dataPin, clockPin, secondByte);
       shiftOut(dataPin, clockPin, thirdByte);
       digitalWrite(latchPin, 1); // return the latch pin high to signal chip that it no longer needs to listen for information
       serialCount = 0;
       sentSwitch = 2;
     }
   }
 }
}

void loop()  {
 if (checkRFID < 1) {
   doLeds();     //  Update LED's
   doSwitches(); //  Check any switches pressed
 }
 if (checkRFID > 0) {
   checkReader();  //  Do RFID scan
 }
}


John_Ryan

Arduino code Pt.2

Code: [Select]

////// ----------------------------------------shiftOut function
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
 int i=0;
 int pinState;
 pinMode(myClockPin, OUTPUT);
 pinMode(myDataPin, OUTPUT);
 digitalWrite(myDataPin, 0);
 digitalWrite(myClockPin, 0);
 for (i=7; i>=0; i--)  {
   digitalWrite(myClockPin, 0);
   if ( myDataOut & (1<<i) ) {
     pinState= 1;
   }
   else {
     pinState= 0;
   }
   digitalWrite(myDataPin, pinState);
   digitalWrite(myClockPin, 1);
   digitalWrite(myDataPin, 0);
 }
 digitalWrite(myClockPin, 0);
}

////// ----------------------------------------shiftIn function
byte shiftIn(int myinpDataPin, int myinpClockPin) {
 int inpi;
 int inptemp = 0;
 int inppinState;
 byte myinpDataIn = 0;
 pinMode(myinpClockPin, OUTPUT);
 pinMode(myinpDataPin, INPUT);
 for (inpi=7; inpi>=0; inpi--)
 {
   digitalWrite(myinpClockPin, 0);
   delayMicroseconds(0.2);
   inptemp = digitalRead(myinpDataPin);
   if (inptemp) {
     inppinState = 1;
     myinpDataIn = myinpDataIn | (1 << inpi);
   }
   else {
     inppinState = 0;
   }
   digitalWrite(myinpClockPin, 1);
 }
 return myinpDataIn;
}

////// ----------------------------------------getBit
boolean getBit(byte myVarIn, byte whatBit) {
 boolean bitState;
 bitState = myVarIn & (1 << whatBit);
 return bitState;
}

John_Ryan

This latest rendition, reads 8 switches, then if one is pressed, takes a reading from the Parallax RFID reader. When a tag is read, a LED corresponding to the switch is flashed. A TextString library is added to compare the tag against a string. If the tag is identified, all 8 LED's flash. This can be modified to check for any number of valid tags. The LED's can be replaced with 5v relays, or you can wire 5v relays over the top of the LEDs so long as you bypass the 220ohm resistors.

Part.1
Code: [Select]

#include <TextString.h>
// Switches - LED's - RFID reader

int latchPin = 8;  //Pin connected to ST_CP of 74HC595
int clockPin = 12; //Pin connected to SH_CP of 74HC595
int dataPin = 11; //Pin connected to DS of 74HC595
int inplatchPin = 10; //Pin connected to ST_CP of 7021
int inpdataPin = 9; //Pin connected to SH_CP of 7021
int inpclockPin = 7; //Pin connected to DS of 7021
int rfidPin = 2; //Pin connected to PRFID
char code[10];
TextString strCom = TextString(10);
char pressed[1]; //
int bytesread = 0; // counter
int checkRFID = 0; // arguement
int gotOne = 0;
int readingIt = 0;
int sentSwitch = 2;
int led1 = 0; // 3rd LED array
int led2 = 0; // 2nd LED array
int led3 = 0; // 1st LED array
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers
byte switchVar1 = 72;  //01001000
byte lowcBitNum = 7;
byte dBitNum = 6;
byte eBitNum = 5;
byte fBitNum = 4;
byte gBitNum = 3;
byte aBitNum = 2;
byte bBitNum = 1;
byte highcNum = 0;
boolean dBit;
long time = 0;
long debounce = 4000; // big long debounce
int firstByte;
int secondByte;
int thirdByte;
int val;
int serialInArray[4];
int serialCount = 0;
void setup() {
 pinMode(latchPin, OUTPUT);
 pinMode(inplatchPin, OUTPUT);
 pinMode(inpclockPin, OUTPUT);
 pinMode(inpdataPin, INPUT);
 pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
 Serial.begin(2400);
 digitalWrite(rfidPin, LOW);  // Activate the RFID reader
 digitalWrite(rfidPin, HIGH);  // Deactivate the RFID reader
 // turn the LED's off
 digitalWrite(latchPin, 0); // ground latchPin and hold low during transmit
 shiftOut(dataPin, clockPin, led1);
 shiftOut(dataPin, clockPin, led2);
 shiftOut(dataPin, clockPin, led3);
 digitalWrite(latchPin, 1); // return the latch pin high to signal chip that it no longer needs to listen for information

}

void flashLed() {
 digitalWrite(latchPin, 0); // ground latchPin and hold low during transmit
 shiftOut(dataPin, clockPin, led1);
 shiftOut(dataPin, clockPin, led2);
 shiftOut(dataPin, clockPin, led3);
 digitalWrite(latchPin, 1); // return the latch pin high to signal chip that it no longer needs to listen for information
}
void checkReader() {
 digitalWrite(rfidPin, LOW);  // Activate the RFID reader
 if(Serial.available() > 0) {  // if data available from reader
   if((val = Serial.read()) == 10) { // check for header
     bytesread = 0;
     while(bytesread < 10) {  // read 10 digit code
       if( Serial.available() > 0) {
         val = Serial.read();
         if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
           break;   // stop reading
         }
         code[bytesread] = val; // add the digit
         strCom.setCharAt(bytesread, char(val)); // test the TextString functions - convert dec to char, add char to strCom
         bytesread++;  // ready to read next digit
       }
     }
     if(bytesread == 10) {  // if 10 digit read is complete
       Serial.print(pressed[0]);  // print the Switch number
       Serial.println(code); // print the TAG code
       if (strCom.equals("3300960185")) { // does strCom - 3300960185
         led1=255; // blink all the LED's if above condition true
       }
       flashLed(); // turn on the LED corresponding to the Switch
     }
     bytesread = 0;
     checkRFID = 0;
     strCom.clear();
     digitalWrite(rfidPin, HIGH);   // Deactivate the reader
     delay(1000); // Have a rest
     led1=0;
     flashLed(); // turn the LED off again
   }
 }
}


 

John_Ryan

Part.2

Code: [Select]

void doSwitches() {
 if (millis() - time > debounce) {
   pressed[0]='0';
   digitalWrite(inplatchPin,1);  //Pulse the latch pin / collect parallel data
   delayMicroseconds(20);
   digitalWrite(inplatchPin,0); //set it to 0 to transmit data serially
   //collect each shift register into a byte
   //checkRFID=0;
   switchVar1 = shiftIn(inpdataPin, inpclockPin);
   if (getBit(switchVar1, highcNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='0'; // switch number
     led1=128; // dec value of LED to flash
   }
   if (getBit(switchVar1, bBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='1'; // switch number
     led1=64; // dec value of LED to flash
   }
   if (getBit(switchVar1, aBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='2'; // switch number
     led1=32; // dec value of LED to flash
   }
   if (getBit(switchVar1, gBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='3'; // switch number
     led1=16; // dec value of LED to flash
   }
   if (getBit(switchVar1, fBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='4'; // switch number
     led1=8; // dec value of LED to flash
   }
   if (getBit(switchVar1, eBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='5'; // switch number
     led1=4; // dec value of LED to flash
   }
   if (getBit(switchVar1, dBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='6'; // switch number
     led1=2; // dec value of LED to flash
   }
   if (getBit(switchVar1, lowcBitNum)) {
     time = millis();
     checkRFID=1; // get RFID tag
     pressed[0]='7'; // switch number
     led1=1; // dec value of LED to flash
   }
 }
}
void doLeds() {
 if (millis() - time > debounce) {
   if (Serial.available() > 0) {
     serialInArray[serialCount] = Serial.read();
     serialCount++;
     if (serialCount > 3 ) {
       firstByte = serialInArray[1];
       secondByte = serialInArray[2];
       thirdByte = serialInArray[3];
       digitalWrite(latchPin, 0); // ground latchPin and hold low during transmit
       shiftOut(dataPin, clockPin, firstByte);
       shiftOut(dataPin, clockPin, secondByte);
       shiftOut(dataPin, clockPin, thirdByte);
       digitalWrite(latchPin, 1); // return the latch pin high to signal chip that it no longer needs to listen for information
       serialCount = 0;
       sentSwitch = 2;
     }
   }
 }
}
void loop()  {
 if (checkRFID < 1) {
   //doLeds();     //  Update LED's
   doSwitches(); //  Check any switches pressed
 }
 if (checkRFID > 0) {
   checkReader();  //  Do RFID scan
 }
}
////// ----------------------------------------shiftOut function
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
 int i=0;
 int pinState;
 pinMode(myClockPin, OUTPUT);
 pinMode(myDataPin, OUTPUT);
 digitalWrite(myDataPin, 0);
 digitalWrite(myClockPin, 0);
 for (i=7; i>=0; i--)  {
   digitalWrite(myClockPin, 0);
   if ( myDataOut & (1<<i) ) {
     pinState= 1;
   }
   else {
     pinState= 0;
   }
   digitalWrite(myDataPin, pinState);
   digitalWrite(myClockPin, 1);
   digitalWrite(myDataPin, 0);
 }
 digitalWrite(myClockPin, 0);
}
////// ----------------------------------------shiftIn function
byte shiftIn(int myinpDataPin, int myinpClockPin) {
 int inpi;
 int inptemp = 0;
 int inppinState;
 byte myinpDataIn = 0;
 pinMode(myinpClockPin, OUTPUT);
 pinMode(myinpDataPin, INPUT);
 for (inpi=7; inpi>=0; inpi--)
 {
   digitalWrite(myinpClockPin, 0);
   delayMicroseconds(0.2);
   inptemp = digitalRead(myinpDataPin);
   if (inptemp) {
     inppinState = 1;
     myinpDataIn = myinpDataIn | (1 << inpi);
   }
   else {
     inppinState = 0;
   }
   digitalWrite(myinpClockPin, 1);
 }
 return myinpDataIn;
}
////// ----------------------------------------getBit
boolean getBit(byte myVarIn, byte whatBit) {
 boolean bitState;
 bitState = myVarIn & (1 << whatBit);
 return bitState;
}


mstanescu

Hi guys

I want to light  up 16 leds using one 74HC575. I want to connect 2 leds in parallel to the same shift register.

1 led + 1 resistor || 1 led + 1 resistor  ---to---> HC575 pin.

This means double current for the HC575 pin. Do you think it can take 20mA current per pin and 160mA current total?

Thanks
Mihai

John_Ryan

u mean 74HC595 ?

two leds is not a problem, you won't be needing the resistor, remember there's only ever 1 pin lit up at a time, so you could probably stack 10 LEDs on 1 pin x 8 pins (80) because there's only ever 10 LED's on at one time, going through pins in a cycle that's too fast for the human eye to discern.


mstanescu

Quote
u mean 74HC595 ?

two leds is not a problem, you won't be needing the resistor, remember there's only ever 1 pin lit up at a time, so you could probably stack 10 LEDs on 1 pin x 8 pins (80) because there's only ever 10 LED's on at one time, going through pins in a cycle that's too fast for the human eye to discern.



What do you mean by one at a time?.. 74HC595 has latch ...latch clock is different than serial input clock. I can load the register and then command output for all of them.

My parallel scheme was something like this


           -----LED-------
           |                  |
VCC --- |                  |-----RES---- 74HC595
           |                  |
           -----LED-------


Since one LED need 10mA the current flowing through 74HC595 will be 20mA.
I set all latch bits to 0V and all leds light up at a time

mstanescu

ok.i think i made a mistake :)....i typed the wrong part number 75LS595 has latches

John_Ryan

Quote
Quote
u mean 74HC595 ?

two leds is not a problem, you won't be needing the resistor, remember there's only ever 1 pin lit up at a time, so you could probably stack 10 LEDs on 1 pin x 8 pins (80) because there's only ever 10 LED's on at one time, going through pins in a cycle that's too fast for the human eye to discern.



What do you mean by one at a time?.. 74HC595 has latch ...latch clock is different than serial input clock. I can load the register and then command output for all of them.

My parallel scheme was something like this


           -----LED-------
           |                  |
VCC --- |                  |-----RES---- 74HC595
           |                  |
           -----LED-------


Since one LED need 10mA the current flowing through 74HC595 will be 20mA.
I set all latch bits to 0V and all leds light up at a time




There's never any more than 1 pin on at a time, so the total amps used is only the sum of 1 pin, of course you control all 8 of them, it's an 8-bit serial in serial or parallel out shift register.            

And you don't need the resistor if your using more than one LED, the resistors only there to stop the LED from frying out, with two LED's it's enough to not need the resistor.




           -----LED-------
           |                  |
GND --- |                  |------------ 74HC595
           |                  |
           -----LED-------


I've got 3 595's driving 24 LED's and 5 relays piggy-backed on top of 5 of the LED's, the LEDs over the relays don't need resistors, and I'm using those evil MCD 10000 high-bright green LED's, they shine as bright with 1 595 and 8 LEDs, or 3 of them and 24 LEDs plus the relays.

Cheater

Quote
There's never any more than 1 pin on at a time, so the total amps used is only the sum of 1 pin,

I thought the 595 had all on at the same time. Its not multiplexing afterall.

flamingoeda

Hi, John

Would you mind tell me how to draw the picture like this?



I think it is better than schematic to describe the design to artist ;-) Are you using some software to draw it? Thanks a lot !

Quote
Now I want to add the 4021's - can anyone see a problem with this layout ?



I want the board to multiplex inputs and outputs at the same time, and use a hacked version of the code from both examples, but, before I go destroying the board I thought I'd check with you guru's first :)


John_Ryan

Mac + Photoshop 7, the wires were done as paths, the rest was hacked from the tutorial images, not an off-the-shelf solution I'm afraid.

theoriginalryan

just wanted to say good job on all your work put into this.  Keep it up

Ryan

flamingoeda

Thanks a lot, it is cool if there is any software can produce it ;-)

Quote
Mac + Photoshop 7, the wires were done as paths, the rest was hacked from the tutorial images, not an off-the-shelf solution I'm afraid.


mem

FYI, another way to draw these, if you have Visio, is to use the 'Freeform' tool to create the connecting wires.

Go Up