int ledVal1 = analogRead(potPin) / 4; //Results in 0 to 255
. . .
analogWrite(ledPin1, ledVal1);
//ledPin1 gets a value of 0 to 255
//0 = OFF
//255 = ON
//127 = half brightness
Thank you for your respond Here's how my circuit look like. I’ve tried your code but the button doesn’t control the potentiometer. Is there anyway I could use the button to turn on the whole system, and have the potentiometer to control all the LEDs?
Have the button press detection code in your setup and wait for press . The code will be the stuck there until you press. Once you press the setup() terminates and the loop starts running and then in the loop use the code from @LarryD to read the pot and use PWM (analogWrite()) to set the led brightness
my bad. sorry for asking - I had the impression the code had changed
May be this will give you some ideas
const byte btnPin = 4;
const byte ledPin1 = 11;
const byte potPin = A0;
void setup() {
pinMode(btnPin, INPUT_PULLUP); // reading the button will be HIGH if not pressed
pinMode(ledPin1, OUTPUT);
// not necessary, they will be LOW by default
digitalWrite(ledPin1, LOW);
// busy loop waiting for button press
while (digitalRead(btnPin) == HIGH) yield();
}
void loop() {
int brightness = analogRead(potPin) / 4; // Results in 0 to 255
analogWrite(ledPin1, brightness); // works because ledPin1 is a PWM pin. Not all are.
}
I’ve tried your code, but I could control the potentiometer only if I press the button. My idea is to be able to press the button and let go (turning it on to “activate” the potentiometer), then use the potentiometer to handle the LEDs, and press the button again to turn it off.
const byte btnPin = 4;
const byte ledPin1 = 11;
const byte potPin = A0;
bool potOn = false;
int lastBtn = HIGH;
void setup() {
pinMode(btnPin, INPUT_PULLUP); // reading the button will be HIGH if not pressed
pinMode(ledPin1, OUTPUT);
// not necessary, they will be LOW by default
digitalWrite(ledPin1, LOW);
}
void loop() {
int btnNow = digitalRead(btnPin);
if (btnNow = LOW and lastBtn = HIGH) {
//Button has just been pressed
potOn = !potOn;
delay(30); //Wait for any button bouncing to finish
}
lastBtn = btnNow; //save button state to compare next time
if (potOn) {
int brightness = analogRead(potPin) / 4; // Results in 0 to 255
analogWrite(ledPin1, brightness); // works because ledPin1 is a PWM pin. Not all are.
}
}
Hope this works how you want. If so, please ask about any lines of code you can't understand. If not, describe what happens in detail and what you expected to happen.
Yes, you only read the last message which says "there was a problem with the code". You didn't read the previous message(s) which would tell what the problem was.
Please always use code tags when you post error messages in your form posts.
const byte btnPin = 4;
const byte ledPin1 = 11;
const byte potPin = A0;
bool potOn = false;
int lastBtn = HIGH;
void setup() {
pinMode(btnPin, INPUT_PULLUP); // reading the button will be HIGH if not pressed
pinMode(ledPin1, OUTPUT);
// not necessary, they will be LOW by default
digitalWrite(ledPin1, LOW);
}
void loop() {
int btnNow = digitalRead(btnPin);
if (btnNow == LOW and lastBtn == HIGH) {
//Button has just been pressed
potOn = not potOn;
delay(30); //Wait for any button bouncing to finish
}
lastBtn = btnNow; //save button state to compare next time
if (potOn) {
int brightness = analogRead(potPin) / 4; // Results in 0 to 255
analogWrite(ledPin1, brightness); // works because ledPin1 is a PWM pin. Not all are.
}
}