I wrote the below sketch to cause one of three LEDs to light, depending on the position of the potentiometer. The sketch works, but the LEDs are so dim they can hardly be seen. I wrote a similar sketch that causes the LEDs to flash at different rates, with the speed dependent on the potentiometer and it works fine, the LEDs are at normal brightness. The hardware setup for both sketches is identical, so I am thinking that the problem is in the sketch, but I haven’t a clue what it might be. Any help would be appreciated.
int arrayMatrix[] = {9, 10, 11};
int val;
int i;
void setup()
{
for (i = 0; i < 4; i++)digitalWrite(arrayMatrix[i], OUTPUT);//set pins to OUTPUT
Serial.begin(9600);
}
void loop()
{
val = analogRead(A0);//get Potentiometer reading
Serial.println(val);
if ( val <=255)
{
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
else if ( val > 255 && val < 510)
{
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}
else if ( val >= 510 && val < 765)
{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
else
{
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}
}
int arrayMatrix[] = {9, 10, 11};
int val;
int i;
void setup()
{
for (i = 0; i < 3; i++)pinMode(arrayMatrix[i], OUTPUT);//set pins to OUTPUT
Serial.begin(9600);
}
void loop()
{
val = analogRead(A0);//get Potentiometer reading
Serial.println(val);
if ( val <=255)
{
Serial.println("in the range val <=255");
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
else if ( val > 255 && val < 510)
{
Serial.println("in the range val >= 510 && val < 765);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}
else if ( val >= 510 && val < 765)
{
Serial.println("in the range val >= 510 && val < 765");
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
else
{
Serial.println("in the else at the end all high");
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}
}
int arrayMatrix[] = {9, 10, 11};
// number of items in an array
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))
...
for (i = 0; i < NUMITEMS (arrayMatrix); i++)
pinMode (arrayMatrix [i], OUTPUT);
When I have a problem with powering things, I always try to use a power transistor. With LEDs, though, power shouldn't be a problem, and a transistor might overload the LED and damage it. If I knew more about the exact type of light you are using, I might be more helpful.
If they can handle 5V, try three power transistors wired like this -
Again, I don't think this is the problem, but you might as well give it a try as long as it doesn't damage anything.