Potentiometer switch not dimming lights

Hello my friend and I are working on a project by making holiday lights we have to have the potentiometer along with a push button to control the lights. The push button works fine and is able to switch between the two modes. The potentiometer is not dimming the lights but when we check serial monitor it is showing that the switch is being read and giving numbers all the way up to 1023.

int readValue = 0;
int writeValue = 0;

void setup() {
  // put your setup code here, to run once:
  
  pinMode(6, OUTPUT);  //green light
  pinMode(7, OUTPUT);  //red light
  pinMode(8, OUTPUT);  //green light
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);  //red light
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(2, INPUT);
  Serial.begin(9600);
}

void loop() {
  readValue = analogRead(A0);
  writeValue = readValue / 4;
  analogWrite(6, writeValue);
  analogWrite(7, writeValue);
  analogWrite(8, writeValue);
  analogWrite(9, writeValue);
  analogWrite(10, writeValue);
  analogWrite(11, writeValue);
  analogWrite(12, writeValue);
  analogWrite(13, writeValue);
  Serial.print(readValue);
  Serial.print(" : ");
  Serial.println(writeValue);
  delay(100);
  
  
  if (digitalRead(2) == LOW) {
    digitalWrite(6, LOW);       //green light off
    digitalWrite(7, LOW);        // red light on
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(6, HIGH);       //green light off
    digitalWrite(7, LOW);        // red light on
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(6, HIGH);       //green light off
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(6, HIGH);       //green light off
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(6, HIGH);       //green light off
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);  //green light off
    digitalWrite(13, LOW);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);  //green light off
    digitalWrite(13, HIGH);  
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(7, HIGH);        // red light on
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);  //green light off
    digitalWrite(13, HIGH);  
    digitalWrite(13, HIGH);
    delay(1000);
  }


if (digitalRead(2) == HIGH) {  //if the button is pressed:
  digitalWrite(6, HIGH);       //green light off
  digitalWrite(7, LOW);        // red light on
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);  //green light off
  digitalWrite(13, LOW);   // turn the red LED on pin 5 OFF
  delay(3000);             // wait for 3 seconds
  digitalWrite(6, LOW);    //green light off
  digitalWrite(7, HIGH);   // red light on
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(12, LOW);   //green light off
  digitalWrite(13, HIGH);  // turn the red LED on pin 5 OFF
  delay(1000);
  digitalWrite(6, HIGH);  //green light off
  digitalWrite(7, LOW);   // red light on
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);  //green light off
  digitalWrite(13, LOW);   // turn the red LED on pin 5 OFF
  delay(1000);             // wait for 3 seconds
  digitalWrite(6, LOW);    //green light off
  digitalWrite(7, HIGH);   // red light on
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(12, LOW);   //green light off
  digitalWrite(13, HIGH);  // turn the red LED on pin 5 OFF
  delay(50);
}
}

An UNO has the following PWM pins:

3, 5, 6, 9, 10 and 11

Your analogWrite( ) lines are soon over written by digitalWrite( )

Okay but I have to have both in order for the button to switch between modes. I see what you mean about PWM pins but I am not sure how to make both the push button and potentiometer to work together than?

In your code, the PWM values are only displayed for 100+ mS, immediately followed by the SW2 choices.

You don’t have a means to hold the PWM values on the outputs.

Sorry its late night and I am getting tired. So there is not a way to control the brightness and have it hold the analog write while running the push button code at the same time?

ok, I think I know what you’re asking…

Change all the digitalWrites to analogWrite(pin, writeValue);

The first block is irrelevant, and might be removed or // commented out because the LEDs are only lit for a tenth of a second.

But this will only control the brightness of the light.

I want to be able to control the brightness while 2 different modes run to make the lights blink in a different pattern.

This video was before we started messing with the dimmer switch, it shows that when we press the button it switches between a different blinking pattern. But we also want to control the brightness at the same time (if possible) with the potentiometer switch.

analogWrite pin, 0) will turn the LED off.
analogWrite (pin, writeValue) will turn it on to the value set by the potentiometer.

If you want something else, you’ll need to reword the question.

I am required to have both the push button and potentiometer switch. One is required to be hooked up to the analog pin and one to digital pin . Is there a way to make these both work at the same time?

So in the video and in the code you can see when the button is not pressed it runs a diffrent blinking pattern and when pressed it starts a other blinking pattern. This is done by digital reading the push button and digitally writing the to the lights.
So if i have to keep the code the same with how it is. Is there a way for the code to also analog write so we can control the brightness while digitally writing if the light is on and off.

Thank you for your help i am really appreciating it. We are going through the arduino lessons in class and we only know the code that they gave us. We have not ran into situation that we have to have both yet.

I should also say that both switchs got to have to do something on the breadboard

This sounds very much like homework or an assignment.
Can you post exactly the assignment as given to you. This will help us to help you to cheat and get a false mark to reflect your ability.

Do you know the meaning of the word irony?

I think I’m out, because I don’t really understand what you want.

If I was approaching your requirement, I’d probably be doing it a completely different way.

All I can see in your current model is the pot adjusts the brightness of the ON LEDs.

OFF LEDs are simply off.

The two sequences are chosen by the switch being ON or OFF.

Your first block as mentioned will serve no purpose as it’s only ON for a fraction of a second.
After that, you test the switch and turn the LEDs full on or full off.
You never use the pot/analog value again until the next pass of loop(), when they’ll be ‘dimmed’ for 100ms, then back into the full on/off sequence,

Once you get the basics going, there are several places you can remove unnecessary code - e.g. you only need to rewrite LEDs if they change state, likewise, you only test the potentiometer value once at the beginning of the loop(), so - brightness changes when you figure them out, can only be set once per loop() pass.

Ideally, you’d test the pot more frequently so you’ll see the changes immediately…

Finally, you’ll learn to NOT use delay(), and discover ‘states’, using millis() to step between them.

It’s a bit of work, and will look quite different, but will get top marks, and be easily adaptable to different patterns and timing.

Okay I get what you mean now thank you for your help

I am not looking to cheat, my friend and I been working on it for a week now and couldn't figure it out. And being on fall break we cant just show the teacher what we have.

Asking questions shouldn't be a reason to be called a cheater, when our code works as intended. Love the name Grumpy_Mike it really befits you.

Also i should state this is not full on coding class. We only learn the code that we had through the arudino website.

Thank you for your help. Had to take a short nap and I was able to figure it out. Its all working now :slight_smile:

Did you notice that I didn’t write the code for you, I pointed out how and what to change to arrive at what I thought you needed.

My goal was to make you look and understand what was happening,nand why !
As you pick up skills, this a great way to help,others, they don’t learn if we do the work for them !

I’m glad to help,
Now, if you have time, think about the other refinements I suggested, You shoukd look into naming your variables and pins to make the code more readable!

Fortunately in software there are always three or more ways to approach the same problem.
Some are better, some neater or more elegant.

Yes i seen what you mean. Thats all i was asking for was step in the right direction. I didnt necessary want the answer. I think it was a little sleep deprivation that was clouding my mind and took me so long to realize what you meant.

Again thank you!

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