oh sorry about that, in the second caption I’m asking if "at that point, can I just push the button and make it turn off ? instead of activating the power first by pushing the button?” so basically, how can I turn off the LED by just pushing the button?
Sorry about that, in the second caption I’m asking if "at that point, can I just push the button and make it turn off ? instead of activating the power first by pushing the button?” so basically, how can I turn off the LED by just pushing the button?
I think you are trying to do too many things with just one button. You want it to enable/disable the pot and turn off/on an LED.
Does the LED go on when the pot is enabled and off when the pot is disabled?
to answer the question you deleted about why I use const byte instead of int for pin definition :
that's because a PIN number is a constant (hence the const), it's less than 255 and always positive, so an unsigned 8 bit variable will do ➜ that's what byte is.
Using an int means you could by mistake change it later in the code as it's not constant (the compiler won't bark at you if you do) and it will use more bytes than necessary (even though it's likely the optimizer will deal with that for you).
```cpp
const byte btnPin = 4;
const byte ledPin1 = 11;
const byte ledPin2 = 6;
const byte ledPin3 = 5;
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);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// not necessary, they will be LOW by default
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, 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.
analogWrite(ledPin2, brightness);
analogWrite(ledPin3, brightness);
}
}
```
const byte btnPin = 4;
const byte ledPin1 = 11;
const byte ledPin2 = 6;
const byte ledPin3 = 5;
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);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, 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);
analogWrite(ledPin2, brightness);
analogWrite(ledPin3, brightness);
}
else { // Jim added this
// Turn LED off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
}
Did you mean that if the pot is off and you push the button and the led will also go off? How will the program know to do that instead of switching the pot on again?
I guess you could use short-clicks and long-clicks?
Hi, it’s the code from PaulRB, I don’t think it’s AI since it seems that he has a lot experience with coding. Also, I think the cpp came from me copying the IDE
And yes, I didn’t understand the code fully, hence why I seek help here and will ask more questions to understand the code tags . I have zero experience in coding before the last 3 weeks.