Hi,
I'm having problems with a project I'm working on right now.
First of all I will explain what the project is about and what I'm trying to do:
I have built a plate witch six holes arranged like a pyramid in it. Every hole is surrounded by 4 LEDs of which two are serially connected (so they are just behaving as if it was one LED). Next to every hole there is a reflection light barrier. For a picture see attachment: plate prototype
What is the Goal?
Goal is to light up the 4 LEDs around a hole as soon as a plastic cup was placed in the hole. If the cup is beeing removed the 4 LEDs switch of again. This of course working for all of the six holes.
(This plate is a prototype and in the final version i will be having two pyramids with 10 holes each.)
The hardware part:
I'm using an Arduino Duemilanove.
Since the plate has 6 holes with two LEDs each (2x 2 serially connected) I would need 12 digital outputs to controll them all. And since i will be needing 20x2 outputs in the final version I'm using two 74HC595 shiftregister to multiplex the ouputs (so now I have 16 outputs but just using 12 of them).
I'm also using two ULN2803 to supply the LEDs with electrical power.
The two ICs are connected like this http://www.instructables.com/files/orig/F5U/CQS8/GWF5UFZU/F5UCQS8GWF5UFZU.png
But just with two 74HC595 and two ULN2803.
For checking if there is a cup I'm using reflection light barriers. These give me an analog signal which is very low if there is a cup and very high if there is no cup. For multiplexing the analog inputs I'm using a 74HC4051.
I dont think we need to worry about the hardware part unless of course if you have any genius suggestions for improvement But the hardware part is working very well. Means I'm able to control the LEDs as i want and I'm able to read out every single light barrier separately.
Picture: Attachment Hardware
So whats my problem then? Where am I at?
I have written a program that does the exact thing I have described in "What is the Goal" BUT the LEDs are not as bright as they should be. This is because in the beginning of the loop all the LEDs are being switched off and during the loop while the program is checking where a cup is it switches on the LEDs around that hole. As the program gets to the next hole it switches on the LEDs around that hole if there is a cup but also switches of all the other leds. That means all LEDs are constantly being switched on and off again which reduces their brightness extremely.
So what to do?
One thing I could theoretically do is defining every possible situation that could possibly occur and tell the programm what to do in which case. So: "If cup1 high ; cup2 high ;.....;cup 6 low" switch on that that and that LED. That would certainly work but since there are six holes there are 2^6 possibilities (in the final version 2^10) that would be just stupid.
The other possibility would be to instead of switching on only the leds around the hole the program is checking at the time and switching of all the other LEDs get the shiftregister to adjust its output somehow. But thats exactly my problem.
So lets say the program notices that there is a cup in hole one. It will send the first shift register a "0" and the second shift register a "3" because these are the value that will light up the LEDs around hole one.
Now the program gets to the second hole and notices that there is a cup.
The combonation to light up the LEDs around hole two is ShiftReg1 = 0 and ShiftReg2 = 12. But if I send this to the shiftRegisters it would switch of the LEDs around hole one again and again the LEDs would appear less bright.
So if there would be a way to to just "add" the second LEDs to the first ones without defining 2^6 possibilities that would be the solution. Something like: "oh there is a cup in hole two so we somehow take the ShiftReg1 = 0 and ShiftReg2 = 12 and add that to whatever the shiftRegister where before in this case ShiftReg1 = 0 and ShiftReg2 = 3 and magically we and up with ShiftReg1 = 0 and ShiftReg2 = 15 OK BAD EXAMPLE because in this case that wouldnt be too hard to do but i think that is because the two holes are the first two and they are next to each other. I dont think that would work for: "Light up Cup3 and Cup6"...
For your understanding here are the ShiftRegister values for every hole:
Hole 1: ShiftReg1 = 0 and ShiftReg2 = 3
Hole 2: ShiftReg1 = 0 and ShiftReg2 = 12
Hole 3: ShiftReg1 = 0 and ShiftReg2 = 48
Hole 4: ShiftReg1 = 3 and ShiftReg2 = 0
Hole 5: ShiftReg1 = 12 and ShiftReg2 = 0
Hole 6: ShiftReg1 = 0 and ShiftReg2 = 192
These will light up the 4 LEDs around the hole. (see the order attachment)
So here is the code that I'm using right now (the one that is working but not very bright )
As you can tell from the code I'm very new to programming and arduinos. Keep that in mind while trying to explain something to me
//defining pins
int S0 = 8;
int S1 = 9;
int S2 = 10;
int SensorPin = A0;
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
//defining variables
int sCup1 ;
int sCup2 ;
int sCup3 ;
int sCup4 ;
int sCup5 ;
int sCup6 ;
void setup()
{
//Configure each IO Pin
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(SensorPin, INPUT);
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
//read out sensor cup 1
digitalWrite (S0, LOW);
digitalWrite (S1, LOW);
digitalWrite (S2, LOW);
int sCup1 = analogRead (SensorPin);
//read out sensor cup 2
digitalWrite (S0, HIGH);
digitalWrite (S1, LOW);
digitalWrite (S2, LOW);
int sCup2 = analogRead (SensorPin);
//read out sensor cup 3
digitalWrite (S0, LOW);
digitalWrite (S1, HIGH);
digitalWrite (S2, LOW);
int sCup3 = analogRead (SensorPin);
//read out sensor cup 4
digitalWrite (S0, HIGH);
digitalWrite (S1, HIGH);
digitalWrite (S2, LOW);
int sCup4 = analogRead (SensorPin);
//read out sensor cup 5
digitalWrite (S0, LOW);
digitalWrite (S1, LOW);
digitalWrite (S2, HIGH);
int sCup5 = analogRead (SensorPin);
//read out sensor cup 6
digitalWrite (S0, HIGH);
digitalWrite (S1, LOW);
digitalWrite (S2, HIGH);
int sCup6 = analogRead (SensorPin);
//switch off all LEDs
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
//LEDs Cup1
if (sCup1 < 500)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 3);
digitalWrite(latchPin, HIGH);
}
//LEDs Cup2
if (sCup2 < 500)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 12);
digitalWrite(latchPin, HIGH);
}
//LEDs Cup3
if (sCup3 < 500)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 48);
digitalWrite(latchPin, HIGH);
}
//LEDs Cup4
if (sCup4 < 500)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 192);
digitalWrite(latchPin, HIGH);
}
//LEDs Cup5
if (sCup5 < 500)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 3);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
//LEDs Cup6
if (sCup6 < 500)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 12);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
}
Thank you in advance for your time and let me know if you need any more information