So I've found a Parallax distributor and ordered a reader, I should have a chance to test the code in a couple of days. In the meantime, I'll start work on the "595 LED micro-boards", I'll post pic's once soldered up and working.
Parallax arrived. At the moment it's looking like I can't have my RFID and eat my LED's to.
But, the reader and RFID code works, and inputs too, so that's a good start
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
// 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
}
}
Arduino code Pt.2
////// ----------------------------------------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;
}
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
#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
}
}
}
Part.2
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;
}
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
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.
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
ok.i think i made a mistake :)....i typed the wrong part number 75LS595 has latches
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.
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.
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 !
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
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.
just wanted to say good job on all your work put into this. Keep it up
Ryan
Thanks a lot, it is cool if there is any software can produce it
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.
FYI, another way to draw these, if you have Visio, is to use the 'Freeform' tool to create the connecting wires.
just wanted to say good job on all your work put into this. Keep it up
Ryan
;D
Thanks, the project forked into something else quite a number of months ago but was useful for putting the 595 through its paces, now I look back on the code, it's really quite hard to follow.
So it's important to read this tutorial first:
http://www.arduino.cc/en/Tutorial/ShftOut22
There's code examples that explain how to start off with 1 register, then 2.
The key to lighting up all 16 LEDs, while really only having 8 on at a time, is this loop:
digitalWrite(latchPin, 0); // ground the latchpin
shiftOut(dataPin, clockPin, 255); // 1st register ignite all 8 LEDs
shiftOut(dataPin, clockPin, 0); // 2nd register turns off all 8 LEDs
digitalWrite(latchPin, 1); // return the latch pin high
digitalWrite(latchPin, 0); // ground the latchpin
shiftOut(dataPin, clockPin, 0); // 1st register turns off all 8 LEDs
shiftOut(dataPin, clockPin, 255); // 2nd register ignite all 8 LEDs
digitalWrite(latchPin, 1); // return the latch pin high
Because of the speed, the human eye will only see 16 LEDs turned on, while really there is only power going to 8 LEDs. Adding extra 595's to expand on the number of LEDs, while still only having 8 LEDs powered at any one time (thus not blowing up the Arduino), is done per:
digitalWrite(latchPin, 0); // ground the latchpin
shiftOut(dataPin, clockPin, 255); // 8 on
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 0); // 8 off
digitalWrite(latchPin, 1); // return the latch pin high
digitalWrite(latchPin, 0); // ground the latchpin
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 255); // 8 on
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 0); // 8 off
digitalWrite(latchPin, 1); // return the latch pin high
digitalWrite(latchPin, 0); // ground the latchpin
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 255); // 8 on
shiftOut(dataPin, clockPin, 0); // 8 off
digitalWrite(latchPin, 1); // return the latch pin high
digitalWrite(latchPin, 0); // ground the latchpin
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 0); // 8 off
shiftOut(dataPin, clockPin, 255); // 8 on
digitalWrite(latchPin, 1); // return the latch pin high
You can downsize this code using variables and counters.
I had 8 595's wired up to ultra high powered LEDs and this worked fine, I don't think there's a limit to the number of 595's you can piggy-back, so for cost its an economic solution to controlling large numbers of LEDs, you can buy 10 off a number of eBay sellers for just a few bucks
This is killing me... am I missing the serial library or something ?? (i don't think I am)
Trying to read an external .txt file just like Ryan's sketch but can't compile past this:
import processing.serial.*;
Serial port;
returning this error:
error: 'import' does not name a type
Any ideas?
This is killing me... am I missing the serial library or something ?? (i don't think I am)
Trying to read an external .txt file just like Ryan's sketch but can't compile past this:
import processing.serial.*;
Serial port;returning this error:
error: 'import' does not name a type
Any ideas?
What version of processing are you using? and what code snippet? This post is quite old, and I no longer have the code or processing for that matter.
Processing refers to this IDE.
I'm not sure if this is the same snippet.
/* http client
original code by Tom Igoe
Starts a network client that connects to a server on port 80,
sends an HTTP 1.1 GET request.
Sends an input value to php
php writes the value to a text file on a web server
*/
import processing.net.*;
Client client;
int inputValue = 33; // number representing the input switch - this would be determined by the value Arduino sends to processing
void setup()
{
// open a TCP socket to the host:
client = new Client(this, "yourserver.com", 80);
// send the HTTP GET request:
client.write("GET /~youraccount/switch.php?receivedValue=" + inputValue + " HTTP/1.1\n");
client.write("HOST: yourserver.com\n\n");
println("\n\nSend complete\n");
}