Hi, it's the first time for me with Arduino project. I want realize 5 led and 4 buttons.
i write code and I test. it's ok. only one question... why the last led are ever weak light?
in image an example with only 4 led and 4 button.
why the last led are ever weak light?
Because you made a mistake with your code or your wiring or components are faulty.
Which one is it?
Who knows when you don't show us anything about what you have actually done.
Please read this:-
How to use this forum
It will tell you how to ask a question here.
excuse me. i attach image but i think it down't work. i never use forum and i don't know. now i attach image again. i have the same situation but i use 5 led and 4 buttons.
for code i write:
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int puls1 = A0;
int puls2 = A1;
int val = 0;
void setup()
{
pinMode( puls1, INPUT );
pinMode( puls2, INPUT );
pinMode( led1, OUTPUT );
pinMode( led2, OUTPUT );
pinMode( led3, OUTPUT );
pinMode( led4, OUTPUT );
pinMode( led5, OUTPUT );
}
void loop()
{
if ( analogRead(puls1) > 1000 )
digitalWrite (led1, HIGH);
if ( analogRead(puls1) > 1000 )
digitalWrite (led2, LOW);
if ( analogRead(puls1) > 1000 )
digitalWrite (led3, LOW);
if ( analogRead(puls1) > 1000 )
digitalWrite (led4, HIGH);
if ( analogRead(puls1) > 1000 )
digitalWrite (led5, LOW);
else
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, LOW);
digitalWrite (led5, LOW);
if ( analogRead(puls2) > 1000 )
digitalWrite (led1, HIGH);
if ( analogRead(puls2) > 1000 )
digitalWrite (led2, HIGH);
if ( analogRead(puls2) > 1000 )
digitalWrite (led3, LOW);
if ( analogRead(puls1) > 1000 )
digitalWrite (led4, HIGH);
if ( analogRead(puls2) > 1000 )
digitalWrite (led5, LOW);
else
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, LOW);
digitalWrite (led5, LOW);
}
I only see 4 leds and 2 buttons in your code. 'led5' is not declared.
You need to learn a bit of C programming, especially the if instruction. Read the reference here at least:
Syntax
if (condition1)
{
// do Thing 1
}
else if (condition2)
{
// do Thing 2
}
else
{
// do Other Thing
}
You can use that to rewrite your code as:
if ( analogRead(puls1) > 1000 ) {
digitalWrite (led1, HIGH);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, HIGH);
digitalWrite (led5, LOW);
} else if ( analogRead(puls2) > 1000 ) {
digitalWrite (led1, HIGH);
digitalWrite (led2, HIGH);
digitalWrite (led3, LOW);
digitalWrite (led4, HIGH);
digitalWrite (led5, LOW);
} else {
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, LOW);
digitalWrite (led5, LOW);
}
baldosim:
Hi, it's the first time for me with Arduino project. I want realise 5 led and 4 buttons.
I write code and I test. it's ok. only one question... why the last led are ever weak light?
Well, given your diagram, you are using one series resistor in common to three LEDs. This means the current set by the resistor will be shared by however many LEDs you switch. Two LEDs lit will each be dimmer than one alone, and three LEDs together, somewhat dimmer again. If in fact you have 5 LEDs, then with all lit they will each see one fifth the current of one alone.
It is possible that one of the LEDs has a significantly different characteristic to the others, and will be dimmer when all are enabled, but you also have an effective 45 Ohm internal resistance in the output driver to each Arduino pin, so that will tend to balance out the current in the paralleled LEDs.
It would be preferable to connect each switch to ground, not 5 V and you could generally use the internal pull-up of the Arduino by setting pinMode to INPUT_PULLUP, instead of providing pull-up resistors.
You should go back and mark up your code as such using the </>
icon in the reply screen, not as a QUOTE.
I cannot imagine why you are using analogRead rather than digitalRead. For pushbuttons, that makes no sense.
And I also suspect the crazy logic structure of your code is actually what is responsible for the dim fifth LED. Since I cannot figure out what you are actually attempting to do, I cannot say much more.
Hello,
Is that 5 LED you turn on as one?
Have you tried changes the last LED with a new one?
So i suggest you to use if else command()
The reference is on this website
In your code
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, LOW);
digitalWrite (led5, LOW);
Will always be performed every time because you have no braces {} after the first else.
Plus what the others said, except nielyay who’s post doesn’t make any sense to me.