turning a button into a switch - beginner


I’m new to Arduino and trying to finish a project for school. I’m using a sound recording module, a potentiometer and a button with 4 leds. The sound module shows the music levels thought the leds, the potentiometer just increases the brightness and the button makes the leds fade. When pressed it should make them fade and when pressed again it should go back to ‘showing’ the music.

Right now it just turns the leds on and off. Also, all the components may be too much for the 5v. The leds blink rather than just staying on.

I’ve found tutorials and tried to combine them but I’m really lost.

Thank you for your help!

int potPin= A0;  //Declare potPin to be analog pin A0
int LEDPin1= 9;  // Declare LEDPin to be arduino pin 9
int LEDPin2= 10;
int LEDPin3 = 11;
int LEDPin4 = 5;
int button= 2;
boolean buttonstate= LOW;
boolean previousbuttonstate = LOW; 
boolean ledon= false;
int sound = 13;
int soundvalue= 0;
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED

void setup() {
  pinMode(potPin, INPUT);  //set potentiometer Pin to be an input
  pinMode(LEDPin1, OUTPUT); //set LEDPin to be an OUTPUT
  pinMode(LEDPin2, OUTPUT); //set LEDPin to be an OUTPUT
  pinMode(LEDPin3, OUTPUT); //set LEDPin to be an OUTPUT
  pinMode(LEDPin4, OUTPUT); //set LEDPin to be an OUTPUT
  pinMode(button, INPUT);
  pinMode(sound, INPUT);
  Serial.begin(9600);      // turn on Serial Port
 boolean debounce(boolean last) // 
  boolean current = digitalRead(button);
  if (last !=current)
{ delay(5);
 current= digitalRead(button);
 return current;

void loop() {

 soundvalue= digitalRead(sound);  //reads the sound values and sends them to the leds
 digitalWrite(LEDPin1, soundvalue);
 digitalWrite(LEDPin2, soundvalue);
 digitalWrite(LEDPin3, soundvalue);
 digitalWrite(LEDPin4, soundvalue);
 Serial.print("Value of sound module:");
 readValue = analogRead(potPin);  //Read the voltage on the Potentiometer
 writeValue = (20./1023.) * readValue; //Calculate Write Value for LED
 analogWrite(LEDPin1, writeValue);      //Write to the LED
 analogWrite(LEDPin2, writeValue);      //Write to the LED
 analogWrite(LEDPin3, writeValue);      //Write to the LED
 analogWrite(LEDPin4, writeValue);      //Write to the LED
 Serial.print("Potentiometer's value: ");  //for debugging print your values

 if (previousbuttonstate == LOW && buttonstate == HIGH) { //when the button is pressed first, the leds start to fade until the button is pressed again
  ledon= !ledon;
  digitalWrite(LEDPin1, brightness);
  digitalWrite(LEDPin2, brightness);
  digitalWrite(LEDPin3, brightness);
  digitalWrite(LEDPin4, brightness);
   brightness = brightness + fadeAmount;    //the fade example from basics arduino
    if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
 previousbuttonstate= buttonstate;
digitalWrite(LEDPin1, ledon);
digitalWrite(LEDPin2, ledon);
digitalWrite(LEDPin3, ledon);
digitalWrite(LEDPin4, ledon);


The thing you gotta understand is that all of that stuff in your loop is going to happen over the course of a few hundred microseconds and it will end on the part that writes all 4 leds to ledon and then wait for 30 milliseconds and then run through all the rest of that again at the speed of light.

The only part that will change will be whether ledon is true or false based on the button.

How is the button wired? You aren't using the internal pull-up resistors and you seem to expect it to read backwards and be HIGH when pressed. Do you have external pull-down resistors? Much more common would be to wire the buttons between the pin and ground so they read LOW when they are pressed and use the built-in pull-up resistors so you don't need any extra parts and so the buttons read the right way round.

Then you need to think about structuring this a little. Decide what you want to have happen when and put the code into some logical order.