I have this led dimmer circuit and I want to add a simple ON/OFF button.
int potPin= A0; //Declare potPin to be analog pin A0
int LEDPin= 9; // Declare LEDPin to be arduino pin 9
int readValue; // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
void setup() {
pinMode(potPin, INPUT); //set potPin to be an input
pinMode(LEDPin, OUTPUT); //set LEDPin to be an OUTPUT
Serial.begin(9600); // turn on Serial Port
}
void loop() {
readValue = analogRead(potPin); //Read the voltage on the Potentiometer
writeValue = (255./1023.) * readValue; //Calculate Write Value for LED
analogWrite(LEDPin, writeValue); //Write to the LED
Serial.print("You are writing a value of "); //for debugging print your values
Serial.println(writeValue);
}
How can I do that?
Thank you!
That program isn't to hard is it? You even found it fully documented! Have you actually programmed the Arduino before?
Easiest way, wrap the part for reading the pot, doing the calculations and setting the output in an if(). In which you check for the switch status. And an else in which you just turn it off.
Also:
writeValue = (255./1023.) * readValue; //Calculate Write Value for LED
//should read
writeValue = (255./1024.) * readValue; //Calculate Write Value for LED
//or even easier (without float crap)
writeValue = readValue >> 2;
//or more readable for newbies:
writeValue = map(readValue, 0, 1024, 0, 256);
I'm not a programmer, I'm just an amateur.
I was able to find code for dimmer or simple turn on/off but not both at the same time.
I get what you're saying but I'm not experienced enough to write that by myself.
I have this code now.
The potentiometer works, it changes the led brightness, but it's not listening to the on/off button.
All I know, is that if I change the
volatile byte ButtonState = HIGH;
to
volatile byte ButtonState = LOW;
the led will be turned off or on, depending on HIGH or LOW
How can I fix this to make it respond to the little push button?
int ButtonPin = 2;
int LedPin = 9;
int PotPin = A0;
volatile byte ButtonState = HIGH;
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(ButtonPin, INPUT);
pinMode(PotPin, INPUT);
attachInterrupt(digitalPinToInterrupt(ButtonPin), OnButtonPressed, FALLING);
}
void OnButtonPressed()
{
ButtonState = !ButtonState;
}
void loop()
{
int PotValue = analogRead(PotPin);
if(ButtonState)
{
PotValue = map(PotValue, 0, 1023, 0, 255);
analogWrite(LedPin,PotValue);
}
else
{
digitalWrite(LedPin, LOW);
}
}
Stop with the interrupt crap. Just use digitalRead() to read the button, will make your life a whole lot easier 
My button is connected to GND and D2.
I just tried this code and it seems to work just fine, but only if I add an additional wire from button to 5V. (the red wire from the attached picture)
int ledPin = 9;
int buttonPin = 2;
int potPin = A0;
int readValue;
int writeValue;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 0;
int ButtonPress = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(potPin, INPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
if (ButtonPress == 1)
ButtonPress = 0;
else
ButtonPress = 1;
}
}
}
lastButtonState = reading;
Serial.println(ButtonPress);
if (ButtonPress == 1)
{
readValue = analogRead(potPin);
writeValue = (255./1023.) * readValue;
analogWrite(ledPin, writeValue);
}
else
analogWrite(ledPin, 0);
}
Now, because the potentiometer and the button are used as midi controllers too, beside controlling the led and because for that purpose the button is connected only to GND and D2 for some reasons, my question is:
Is there any way to make this work without the additional 5v connection?
A small tip, press ctrl+T in the IDE, looks a lot better doesn't it?
And great using debounce although you didn't really needed it here.
Also, see the "also" of reply #1 