Push button and RGB LED switch


I am a new coder and I am trying to configure a program that allows me to change the brightness of one colour on the RGB LED using a potentiometer, but I can switch between the 3 colours using a push button.

The catch is that, when I push the button, the previous colour is still visible (and even mixes with the new new colour).

For example, it starts on red and I move the potentiometer to make it a light-red, when I push the button to change it to green, it is a mix between a light red and a green.

This is for a school project and I have no idea how to make this work.

Does anyone have any pointers or ways to make this work?

I will post my link below to tinkercad, but I will also paste my code here too!

I am a new coder, so I know it will probably be completely wrong and have a ton of mistakes, so take it easy! I'm trying to learn because I do not understand Arduino's or breadboards very well, along with the coding that is involved.

My code:

void level3(){

if (digitalRead(buttonPin1)==HIGH)){

data= analogRead(rotationPin);
data = map(data,0,1023,0,255);

digitalRead(buttonPin1)== LOW;
if (digitalRead(buttonPin1)==HIGH)){

data= analogRead(rotationPin);
data = map(data,0,1023,0,255);

digitalRead(buttonPin1)== LOW;

if (digitalRead(buttonPin1)== HIGH)){

 data= analogRead(rotationPin);
 data = map(data,0,1023,0,255);




What is the point of this?

That's a very bad, and very long way of writing

data = analogRead(rotationPin) /4;

If you read what I wrote above, I am brand new to coding and this is for a project. That is how our professor wrote the code so we can visually see what is happening in the code, again, because we are new to coding.

I am asking for some help for the solution, not to be bashed on how bad the code is or how long it is. Obviously as time goes on and I become more familiar with the coding language I will be able to shorten it and make it more efficient, but that is not why I made this post!

I am aware that it is bad, and I even wrote that above, if you cared to read it, but I am asking for help on a solution to my problem, not how to improve the code and make it shorter.

data = analogRead(rotationPin) >>2;

Yeah, no need to make it more obscure - that's what the compiler will do anyway.

The prof wrote a naked condition, with no outcome and no comment why?

Time to find a new course prof.

Please remember to use code tags when posting all of your code.

(You're confusing questioning for criticism)

Hint: if the switch is pressed for the first LED, there's a good chance it is still pressed ten or so milliseconds later.


Nevermind, You're still not understanding that I asked for a solution (or help) to one problem and you're giving me a solution to a completely different issue!

I definitely understand the difference between questioning and criticism, and I completely embrace criticism because I am new to coding, especially Arduino, but I'm less open to it if it's completely irrelevant to my original problem! lol

For instance, the hint that you gave me was probably the most useful piece of information that you have posted through this entire comment chain, and I GREATLY appreciate that!

Also, telling me that I should have added or kept my code tags or comments is also a great tip that I will remember for future posts! That was a foolish and rookie mistake because there were a load of comments that I excluded to try and keep the code looking cleaner. So, thank you for that!

But just telling me that my code is "...very bad, and long way of writing" does nothing for me other than basically insult me; a brand new coder! Of course I'm going to suck, of course it's going to be longer, of course it will be less efficient; I'm learning!

And my prof is very helpful, considering that it is remote learning, but he did include a ton of comments to the code, which I just finished thanking you for reminding me that I should have included, so that is not my profs fault!

Anyway, to finish my reply, thank you for the comment tip and the hint! Both of those were helpful!

Of course, you could always ask me why I consider the map long-winded and incorrect, but you're clearly not interested in hearing.
Maybe your prof can explain it.

I make comments to provoke reactions and gauge experience, so I know how to pitch answers - many people write "I'm new to xyz", but may actually have considerable expertise in other, related fields or languages. If I don't ask, I'm never going to find out.

Remember: the First Rule of the Dunning-Kruger Club is, you don't know you're a member of the Dunning-Kruger Club.

Another hint is to always use the IDE's auto format tool - it provides useful clues about program structure.

Simply turn off the led you were setting the brightness of upon a button push. And then proceed with setting the next led's value.

...and this requires you to read the IDE's state change example code.

So... I'm not sure how you're going to take this, but may I suggest not starting out by looking how to complete example code that you don't understand (yet)? Sometimes this works and sometimes it doesn't, but it feels more like job interview questions or exam questions than actual programming exercises.

If I read you correctly, leaving out the fact that we're talking about 3 values, pot switches and RGB leds, you're basically trying to do something like this, formulated more abstractly and functionally:

  • Control n variables by a single controller.
  • Only 1 variable at a time can be manipulated by that controller.
  • Toggling to the next variable by button press means that we'll have to keep in memory some pointer that remembers which of the n variables we're controlling.
  • A single output needs to be generated from all n variables, meaning we'll have to keep them all in memory even when we're not controlling them.
  • As someone else hinted, button presses can take a long time, but the button should trigger only 1 change to the next controlled variable

Here are two general aspects of your code that are surprising to more experienced coders. Just food for thought or maybe for discussion with your prof. We're not trying to insult but to inspire.

  • There is duplicate code that does exactly the same thing, just for another color and pin. Understanding why this is dangerous, error-prone and wasteful and how to optimize this is, in my opinion, the biggest learning opportunity here.
  • The "==" implies a comparison. The only reason to do comparisons is to base choices on, like "either do something or don't", or "if that, then do that, else do that". If no code is depending on the outcome of the comparison, the comparison is simply useless.

Not that it will break by doing it like that, but it makes your solution more cumbersome and error-prone than necessary.

Hopefully this helps and wasn't too much of a wall-of-text :smiley: Good luck.

Here are a few hints:

  1. If you want the push of a single button to rotate between LED colors you are only interested in when the button BECOMES pushed (i.e. when it goes from not pushed to pushed). This tutorial should help you: StateChangeDetection
  2. Use a counter that goes from 0 to 2. Every time the button BECOMES pushed increment the counter.
  3. Determine which color you will adjust based on the counter (example, Red = 0, Green = 1, Blue = 2)
  4. Read the analog and set the intensity of the color corresponding to the counter.

What YOU said was not insulting at all! This was EXACTLY what I was hoping for! You are helping provide solutions to the issues THAT I WAS ASKING FOR, and I truly and greatly appreciate it! And I am aware that I will have mistakes and I have no issue with being told where those mistakes are!

If I am being honest, Arduino/Internet of Things is not really a major passion of mine and I do not plan on pursuing it as a career path, but I do find it interesting, I just can't seem to understand it as well as other programming languages. It is a requirement for my degree to take this course, so I GREATLY appreciate the help in trying to help me understand this language and its functions!

You could be a teacher with how well you wrote that out and how easy it was to understand! Just giving me a solution doesn't necessarily help me understand the code (or solution) that you are providing to me, but you actually DESCRIBED and TAUGHT a solution to me and I honestly could not be more appreciative for that, so THANK YOU!!

That was easily above and beyond what I was asking for! Unfortunately, the assignment has since past and I can't apply this to it, but, I can at least understand where I went wrong and understand this whole concept in general!

Thank you again! You do understand and know how to write a useful response and solution!

Same goes for your comment as well, this was extremely helpful! Thank you for the hints! This is what I was looking for! As I said to the other commenter, unfortunately this lab has since past and I can't apply any of this to the code, but I truly thank you for the information and the help!

This is how respective coders and people should interact when looking for help or solutions!

So, thank you again for the hints, the help and the comment!

Well... The assignment being over doesn't mean you can't still complete it for the sake of practicing, right? :slight_smile:

I challenge you to try and complete your solution anyway. It's actually quite small in scope (should be doable with just a handful of lines of code), while it holds learning opportunities that may well prove essential to your future approach to logic problems in general. Especially reading that you use other programming languages too.

A general suggestion about your communication: you use many exclamation marks! Almost all your sentences end in them! That may come across as more aggressive than you intend! Try to cut down on them! It may well lead to easier conversations! :smiley: