Playing with LEDs in Arduino Uno

Hi everyone, I am trying to learn arduino uno, so I have something in my mind which I want to do but am not sure how to do it and where to start. I am simulating first everything in and if it works then I will try it on a breadboard with my arduino uno. I already wrote the code for void setup so I need only void loop part.
So the idea behind my little project is that I have 4 LEDs connected to pins 2, 3, 4, 5 all set as digital OUTPUT in arduino sketch. Pins 6, 7, 8 are my control pins.

I want:
LED (pin2) light up everytime pin 6 and 7 are set to LOW,
LED (pin3) light up everytime pin 6 and 8 are set to HIGH,
LED (pin4) light up everytime pin 2 is HIGH and pin 4 is LOW,
LED (pin5) light up everytime pin 2 is LOW and pin 3 is HIGH

I still do not know how I will set my control pins to LOW or to HIGH for simulation my idea was just to write a couple of code line like "digitalWrite (6, LOW)" and "digitalWrite (7, LOW)" to light up the LED (pin2) when it works I will try to connect 5v source for each combination of pins to try it in real world.

Is there any way to do it efficiently but not super complex because I am a beginner and I want to understand what I am doing and not only copy paste code.
Thank you

And welcome to forum.
Take a view into the IDE. You will find a lot of examples to start.

perhaps you know which one will match my project the most?

Why not work through a few of them, and see if you learn anything new?

1 Like

My grandfather used to say:
"Selbst ist der Mann" :wink:

Or similar "Hilf dir selbst, dann hilft dir Gott." :grinning:

But I can give the @hekk_tech some hints

  • LED lights up translates to a digitalWrite to the respective OUTPUT pin
  • to get the current logical value on a pin, use digitalRead
  • if (conditon) { } can control the execution of the code in the braces
  • conditions often look like age == 21 or digitalRead(somePin) == LOW
  • conditions can be combined with a couple of operators which include and and or
  • actions that should be executed all over, belong in loop
1 Like

This is exactly what I understand on these kind of forums. I will figure this out myself it just will take me much longer. I wanted to save time but as your grandfather would probably say "Just do it yourself". :wink: the german way I guess

That helped me thank you Whandall, I am on my way to my goal now.

For pin4 to be HIGH, it has to be LOW, that is kind of problematic,
it's probably 'blinking' at full loop speed.

Ok I got something working here is the simulation with the code:

Now comes the question if there is other way to do the code more compact without all these repetitive if else statements? Any advice?

digitalWrite(pin, digitalRead(iPin1) && !digitalRead(iPin2));

this is the most compact way I can think of, some will claim that you have to use HIGH and LOW,

digitalWrite(pin, (digitalRead(iPin1)==HIGH)&&(digitalRead(iPin2)==LOW)?HIGH:LOW);

conditon ? value1 : value2 is the ternary operator,
&& is the normal way to write a logical and.


1 Like

Thank you Whandall! Your help and time is very appreciated!

Draw out a truth table, if the logic is static, and see if you can factor it better.

"Honi soit la vache qui rit", as my grandad never said.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.