How to have potentiometer separate from button actuation for LED on/off

const int ledPin = 12;
const int buttonPin = 2;


int buttonState = 0;


void setup() {

  Serial.begin (9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  // designate pin 13 as an OUTPUT pin for the LED
  pinMode (13, OUTPUT);

  // designate pin A0 as an Analog INPUT for the potentiometer
  pinMode (A0, INPUT);

  Serial.begin(9600);
  
  
}
void loop() {
  buttonState = digitalRead(buttonPin); 

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("LED ON +++++++++++");
    
  }
  else{
    digitalWrite(ledPin, LOW);
    Serial.println("LED OFF ---------");
  }
  
  Serial.println  (A0);

  int potValue = analogRead (A0);

  digitalWrite (13, HIGH);

  delay (potValue);

  digitalWrite (13, LOW);

  delay (potValue);
  
}

Im new to this!

So I want one LED to be controlled via a potentiometer, which works well.

The other LED is to be turned on/off with a button. However, it's behaving strangely - sometimes it turns on for a while, sometimes it doesn't respond to button being pressed. I know it has something to do with my code but i have no idea how to solve it.

Hi and welcome to the forum. +1 Karma for using code tags the first time you posted code.

lovehouseparadise:
I know it has something to do with my code

How do you know that? You seem very certain. My guess is not the code.

PaulRB:
Hi and welcome to the forum. +1 Karma for using code tags the first time you posted code.
How do you know that? You seem very certain. My guess is not the code.

Thank you for the welcome!

I am not certain it is my code...

I have the both LED's and buttons on a breadboard wired to the same GND/5V.

How exactly is the button wired? Do you have a pull-up or pull-down resistor on the pin it’s connected to or is it just left floating when the button isn’t pressed?

Steve

slipstick:
How exactly is the button wired? Do you have a pull-up or pull-down resistor on the pin it's connected to or is it just left floating when the button isn't pressed?

Steve

Button is wired with a 10k resistor to the GND side.

lovehouseparadise:
sometimes it turns on for a while, sometimes it doesn't respond to button being pressed.

Is this behavior affected in any way by the pot setting?

lovehouseparadise:
I have the both LED's and buttons on a breadboard wired to the same GND/5V.

5V and GND are not the same, which is it?

lovehouseparadise:
Button is wired with a 10k resistor to the GND side.

So the button and 10K are both wired to GND? You could be clearer this.

dougp:
Is this behavior affected in any way by the pot setting?

So yes the LED2(connected to button) responds well when pot is blinking LED1 at high rate. When the pot is set lower so LED1 is blinking slowly LED2 is responsive only when LED1 is on.

PaulRB:
5V and GND are not the same, which is it?So the button and 10K are both wired to GND? You could be clearer this.

Button is connected on one side to 5V and other side to GND. Button and 10k both wired to ground yes.

lovehouseparadise:
So yes the LED2(connected to button) responds well when pot is blinking LED1 at high rate. When the pot is set lower so LED1 is blinking slowly LED2 is responsive only when LED1 is on.

Ahh, right. I think you'll find LED2 changes only when LED1 changes. If LED1 blinks slowly, LED2 seems unresponsive. It only responds at the instant LED1 switches off. You need to learn & understand the "blink without delay" principle.

lovehouseparadise:
Button is connected on one side to 5V and other side to GND. Button and 10k both wired to ground yes.

Really? That will cause a damaging short circuit when you press the button... My point here is, every time you try to explain how you wired everything, you make a another mistake. This isn't because you are not good at English. It's because English is bad at explaining circuits.

PaulRB:
You need to learn & understand the "blink without delay" principle.

There's a demo at IDE > file/examples/digital/blinkwithoutdelay.

The following four examples are good knowledge, too.

The impetus for this is that during delay() the processor does nothing else, it's stuck there 'til the delay time finishes.

There are two issues in your code:

  1. Button Input pin SHOULD be pull-up or pull-down See How to use button with pull-up/pull-down

  2. It had better to use PWM to fade an LED Arduino - potentiometer fade LED tutorial

And see Arduino - Button Control LED tutorial

Have tried this.. same results:

//potentiometer
int potentiometer = A0;
int potRead = 0;
int redLED = 13;
int isOn = 1;
long now = 0;

//button
byte led2 = 10;
byte button2 = 7;

void setup() {

// set the digital pin as output:
//potentiometer
  pinMode(redLED,OUTPUT);
  pinMode(potentiometer,INPUT);
  digitalWrite(redLED,HIGH);

// button
  pinMode (led2, OUTPUT); // pin to LED anode, LED cathode to resistor, resistor to Gnd
  pinMode (button2, INPUT_PULLUP); // button connects pin to Gnd when pressed

 } // end setup

 void loop()

 {

// blink the LED.
   potRead = analogRead(potentiometer);
  if(millis() > now + potRead){
    digitalWrite(redLED,isOn);
    isOn = 1 - isOn;
    now = millis();

 if (digitalRead(button2) == LOW) {

 digitalWrite (led2, HIGH);

 }

 else {

 digitalWrite (led2, LOW);

 }
  }
} // end loop

Yup, because you got a } in the wrong place.

If you use Auto-Format, this will fix the indentation of your code. That doesn't change how the code works at all, but makes it easier to spot this kind of mistake.

PaulRB:
Yup, because you got a } in the wrong place.

If you use Auto-Format, this will fix the indentation of your code. That doesn't change how the code works at all, but makes it easier to spot this kind of mistake.

So apologies for my noob understanding. However, now the led only blinks super fast at the highest potentiometer level. When turned slightly down it just switches on with no blinking... (sometimes even just turns off completely..)

//potentiometer
int potentiometer = A0;
int potRead = 0;
int redLED = 13;
int isOn = 1;
long now = 0;

//button
byte led2 = 10;
byte button2 = 7;

void setup() {

  // set the digital pin as output:
  //potentiometer
  pinMode(redLED, OUTPUT);
  pinMode(potentiometer, INPUT);
  digitalWrite(redLED, HIGH);

  // button
  pinMode (led2, OUTPUT); // pin to LED anode, LED cathode to resistor, resistor to Gnd
  pinMode (button2, INPUT_PULLUP); // button connects pin to Gnd when pressed

} // end setup

void loop()

{

  // blink the LED.
  potRead = analogRead(potentiometer);
  if (millis() > now + potRead) 
    digitalWrite(redLED, isOn);
    isOn = 1 - isOn;
    now = millis();

    if (digitalRead(button2) == LOW) {

      digitalWrite (led2, HIGH);
   
    }
  
    else {

      digitalWrite (led2, LOW);


    }
   
}

Now you deleted a { you shouldn't have deleted! And deleted a } to make it compile again?

What I was suggesting was move a } not remove one.

PaulRB:
Now you deleted a { you shouldn't have deleted! And deleted a } to make it compile again?

What I was suggesting was move a } not remove one.

YIKES!

Haha cheers! It's flawless now.

Hello, please excuse me I don’t know English, I write using translator.
Here are two variants of my solutions. I tried to keep as much of the flood

program as possible, so I didn’t change, for example, digitalWrite ()

bitSet () to make the code smaller and much faster.
Also, I didn’t want to change the flood-defined types of variables. Since I

don’t have Arduino I did it in a simulation program with Arduimo Nano,
so I can’t check it in real.
Sincerely
bo_bo

pot-led-v0.ino (2.53 KB)

pot-led-v0.ino (2.53 KB)

Hello, I’m sorry, but I sent two identical files by mistake. Here is the right file containing version 1.
Sincerely
bo_bo

pot-led—v0.ino (1.47 KB)

Hello, I'm sorry, but I didn't notice
that it is already solved.
Please take my contributions and sent the files irrelevant.

Sincerely
bo_bo