turning a button into a switch - beginner


hi,

i'm new arduino , trying finish project school. i'm using sound recording module, potentiometer , button 4 leds. sound module shows music levels thought leds, potentiometer increases brightness , button makes leds fade. when pressed should make them fade , when pressed again should go 'showing' music.

right turns leds on , off. also, components may 5v. leds blink rather staying on.

i've found tutorials , tried combine them i'm lost.

thank help!

code: [select]
int potpin= a0;  //declare potpin analog pin a0
int ledpin1= 9;  // declare ledpin 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 led is
int fadeamount = 5;    // how many points fade led by
int readvalue;  // use variable read potentiometer
int writevalue; // use variable writing led

 
void setup() {
  pinmode(potpin, input);  //set potentiometer pin input
  pinmode(ledpin1, output); //set ledpin output
  pinmode(ledpin2, output); //set ledpin output
  pinmode(ledpin3, output); //set ledpin output
  pinmode(ledpin4, output); //set ledpin 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 sound values , sends them leds
 digitalwrite(ledpin1, soundvalue);
 digitalwrite(ledpin2, soundvalue);
 digitalwrite(ledpin3, soundvalue);
 digitalwrite(ledpin4, soundvalue);
 serial.print("value of sound module:");
 serial.println(soundvalue);
 
 readvalue = analogread(potpin);  //read voltage on potentiometer
 writevalue = (20./1023.) * readvalue; //calculate write value led
 analogwrite(ledpin1, writevalue);      //write led
 analogwrite(ledpin2, writevalue);      //write led
 analogwrite(ledpin3, writevalue);      //write led
 analogwrite(ledpin4, writevalue);      //write led
 serial.print("potentiometer's value: ");  //for debugging print values
 serial.println(writevalue);

 buttonstate=debounce(previousbuttonstate);
 if (previousbuttonstate == low && buttonstate == high) { //when button pressed first, leds start fade until button pressed again
  ledon= !ledon;
  digitalwrite(ledpin1, brightness);
  digitalwrite(ledpin2, brightness);
  digitalwrite(ledpin3, brightness);
  digitalwrite(ledpin4, brightness);
   brightness = brightness + fadeamount;    //the fade example basics arduino
    if (brightness <= 0 || brightness >= 255) {
    fadeamount = -fadeamount;
  }
 }
 previousbuttonstate= buttonstate;
digitalwrite(ledpin1, ledon);
digitalwrite(ledpin2, ledon);
digitalwrite(ledpin3, ledon);
digitalwrite(ledpin4, ledon);
 
  delay(30);

}

the thing gotta understand of stuff in loop going happen on course of few hundred microseconds , end on part writes 4 leds to  ledon , wait 30 milliseconds , run through rest of again @ speed of light. 

the part change whether ledon true or false based on button.

how button wired?  aren't using internal pull-up resistors , seem expect read backwards , high when pressed.  have external pull-down resistors?  more common wire buttons between pin , ground read low when pressed , use built-in pull-up resistors don't need parts , buttons read right way round. 

then need think structuring little.  decide want have happen when , put code logical order.


Arduino Forum > Using Arduino > Programming Questions > turning a button into a switch - beginner


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???