hi , i am doing a small but interesting project , will u help me.
i want that , when i press the push button the current flowing to led controlled by potentiometer should not not be changed after the push button is pressed .” In simple ways arduino should not take any order from potentiometer until the button is pressed again to give it back it's control .
But i don't know how to disables it's command for a while and give it back after the button is pressed.
please let me know how can i turn my code into what i told u.
#define LED 3
#define KNOB 0
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
// Read the value of the potentiometer knob
int knobValue = analogRead(KNOB);
// Map the potentiometer value to 1-255
int intensity = map(knobValue, 1, 1024, 1, 255);
// Output the respective value to the LED
analogWrite(LED, intensity);
}
Thanks for posting your code in tags, it makes a lot of difference.
What you need to add is an 'if" statement that when you press your button, analogReads the pot.
When you release the button the analogRead will not be accessed by the code.
Use the state change detection method to toggle the value of a flag the controls whether the pot can adjust the brightness. Tested on real hardware.
Example code:
const byte LED = 3; // I do not like define for pin numbers
const byte buttonPin = 4; // const because they won't change
const byte KNOB = A0; // use A notation for analog inputs
bool buttonState = 0; // current state of the button
bool lastButtonState = 0; // previous state of the button
bool adjustLED = true; // create the flag variable
void setup()
{
Serial.begin(115200);
Serial.println("toggle ability to adjust LED brightness");
pinMode(LED, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
readButton();
if (adjustLED == true)
{
// Read the value of the potentiometer knob
int knobValue = analogRead(KNOB);
// Map the potentiometer value to 1-255
int intensity = knobValue / 4; // corrected equation
// Output the respective value to the LED
analogWrite(LED, intensity);
}
}
void readButton()
{
static unsigned long timer = 0;
unsigned long interval = 50; // check switch 20 times per second
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the new buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
adjustLED = !adjustLED; // toggle value of adustLED
//Serial.println(adjustLED);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
const byte knob = 0;
const byte myButton = 2;
const byte LED = 3;
int knobValue = 0;
int intensity = 0;
bool buttonState = 0;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(myButton, INPUT_PULLUP);
}
void loop()
{
if(digitalRead(myButton) == LOW) // Button pressed?
{
delay(150); // Wait 150ms for bouncing to stop
if(digitalRead(myButton) == LOW) // Button STILL pressed?
{
buttonState = !buttonState; // Reverse the button state flag
delay(250); // 1/4 of a second to get your finger off the button
}
}
if (buttonState == HIGH) // if we're in a "go state" then
{
knobValue = analogRead(knob); // Read the value of the potentiometer knob
intensity = map(knobValue, 1, 1024, 1, 255); // Map the potentiometer value to 1-255
analogWrite(LED, intensity); // Output the respective value to the LED
}
else // If we're here then we're not in a "go state" so do nothing
{
}
}
Connect a switch/button between pin 2 and ground. When you press it once it should set buttonState to HIGH / 1 / True (etc). This is pretty crude; you'll have about a 1/4 second to get your finger off the button - and I haven't tested it - but (if nothing else) if might give you something to go on.
/*
Wire the button between pin 2 and gnd
*/
#define LEDPin 3
#define ButtonPin 2
#define KNOB A0
int intensity;
int knobValue;
bool ButtonState;
void setup() {
pinMode(LEDPin, OUTPUT);
pinMode(ButtonPin, INPUT_PULLUP);
}
void loop() {
ButtonState = digitalRead(ButtonPin); //Read the button
if (ButtonState == LOW) //check if button pressed, LOW
{
knobValue = analogRead(KNOB); // Read the value of the potentiometer knob
intensity = map(knobValue, 1, 1024, 1, 255); // Map the potentiometer value to 1-255
}
analogWrite(LEDPin, intensity); // Output the respective value to the LED
}
That's pretty much what I had originally - but I think he wants it to toggle - not just react when the switch is held down - so we immediately start getting into debounces and release delays etc.
Not sure what first prompted you, but I've found it to be a quick and easy way to debounce; it's higher than most people would probably use, but when I did some testing a couple of days ago with the crappiest of switches I was still getting bounces at 130ms ... and since I can't physically press a button more than 6 times a second using 150 became moot.
knobValue = analogRead(KNOB);
intensity = map(knobValue, 1, 1024, 1, 255);
// should be
knobValue = analogRead(KNOB);
intensity = map(knobValue, 0, 1023, 0, 255);
// everything can be condensed to a single line
void loop() {
if (!digitalRead(ButtonPin)) analogWrite (LEDpin, analogRead(KNOB) << 2;
}
I think he wants the button as a toggle so it "stops doing it's thing" when he presses/releases once and "resumes doing it's thing" when he presses/releases it again.