How to get my buttons work properly??

Hi everyone,
I need to do a project on Arduino UNO but I am almost new into it so I am just trying to figure out step by step.
For now I want to make 3 buttons work like this:

1- Button Power up and down my arduino
I attached my 3 pin button to Vin, GND and 5V and it seems to work without any special programs..
If someone can explain it to me will be cool because I just copied from a picture

2- Button to reset Arduino
For this one I attached my 2 pin button to GND and Reset (work fine)

3- Button has to make my program start when I press and make stop program when I press again
I attached it to GND and Pin 12 and I am testing my code with a LED into Pin 13 for now but there is something strange happening...Light is not turning always off..Can someone help me with the code?

int buttonPin = 12; //whatever pin your button is plugged into
int ledPin = 13; // choose the pin for the LED

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP); // declare pushbutton as input
  pinMode(ledPin, OUTPUT); //declare LED as output

}

void loop()
{
  //check button pressed, if so enter program condition (inside if statement)
  if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
  {
     //put code you want to run on button press here.
     //if will run once and then when it's done it will exit the if statement and return to the top of the loop function and continually checking to see if the button has been pressed, if it hasn't it will keep checking, if it has it will run the program again and then once done continue to check after
     digitalWrite(ledPin, HIGH); //turn LED on
  }

  if(digitalRead(buttonPin) == LOW)
  {
    digitalWrite(ledPin, LOW);
  }
  
    //else will stop program when button is released
  //else 
  //{
    //digitalWrite(ledPin, LOW); //turn LED off
  //}
}

You check buttonPin twice in your loop(). One time when low you switch on the LED, the other time you switch it off. So that would give about 50% chance of the LED being on or off when you release the button (even a very short press can easily be hundreds of runs of loop()).

wvmarle:
You check buttonPin twice in your loop(). One time when low you switch on the LED, the other time you switch it off. So that would give about 50% chance of the LED being on or off when you release the button (even a very short press can easily be hundreds of runs of loop()).

Oh I see but no idea how else I can tell the program when I press again stop the LED?

That's a very basic form of finite state machine. Button press switches between states - an on state and an off state.

So you have to do state tracking of the button (as you want to react to a change - button being pressed - rather than steady state), and to make that work properly you'll also have to look into debouncing (i.e. ignore presses for some 50 ms after the first change was detected).

A bit of Googling will give you lots of examples for this.

Try this:

int buttonPin = 12; //whatever pin your button is plugged into
//int ledPin = 13; // choose the pin for the LED
// Whenever you intend to use the built-in LED, it's better to do it like this -- makes it more "portable"
// [which is a fancy way of saying, "make it work on ALL versions of the Arduino"]
int ledPin = LED_BUILTIN;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP); // declare pushbutton as input
  pinMode(ledPin, OUTPUT); //declare LED as output

}

void loop()
{
  //check button pressed, if so enter program condition (inside if statement)
  if(digitalRead(buttonPin) == LOW)
  {
     // Button is pressed, so turn the LED on
     digitalWrite(ledPin, HIGH);
  }
  else
  {
     // Button is NOT pressed, so turn the LED off
     digitalWrite(ledPin, LOW); //turn LED off
  {

  delay(100);  // Wait for bouncing [in the switch] to stop.  This is probably a rather conservative value.  50mS might be fine.

  // An easier way to "comment out" a block of code is with the /* */ operators:
/*  Everything inside here is commented out, and thus ignored by the compiler
else 
  {
    //digitalWrite(ledPin, LOW); //turn LED off
  } 
*/ 
}

And if you want it to toggle:

// This is a way to make your code easier to modify later.  For instance, suppose you want to change
// the ON state to be 0, instead of 1.  If you had "hardcoded" those values throughout your code, you would
// have to find every instance, and change it there.  This way, you only have to change it one, right here.
#define ON 1
#define OFF 0

int buttonPin = 12; //whatever pin your button is plugged into
int ledPin = LED_BUILTIN;
boolean toggle_state = OFF;
boolean is_toggled = true;  // So we can toggle only once per button press

void update_toggle_state()
{
  //check button pressed, if so enter program condition (inside if statement)
  if(digitalRead(buttonPin) == LOW)
  {
     // Button is pressed
     if (!is_toggled )
     {
       // Toggle hasn't happened yet, so do it!
       if (ON == toggle_state)
       {
         toggle_state = OFF;
         digitalWrite(ledPin, LOW); //turn LED off
         is_toggled = true;
       }
       else if (OFF == toggle_state)
       {
         toggle_state = ON;
         digitalWrite(ledPin, HIGH); //turn LED on
         is_toggled = true;
       }
       else
       {
         // Handle the error!
       } 
     } // endif (!is_toggled)
  }
  else  // Button is NOT pressed
  {
      // Reset for next button press
      is_toggled = false;
  }
}

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP); // declare pushbutton as input
  pinMode(ledPin, OUTPUT); //declare LED as output
  update_toggle_state(); // This initializes the Toggle State
}

void loop()
{
  update_toggle_state();
  delay(100);   // Wait for bouncing [in the switch] to stop
}

None of this is tested, BTW :wink:

In the toggle code you have to keep track of the previous state of the button, and only react on a state change (from unpressed to pressed):

currentButtonState = digitalRead(buttonPin);
if (currentButtonState == LOW && previousButtonState == HIGH) {
  // toggle LED
}
previousButtonState = currentButtonState; // remember the current state.

Otherwise you toggle every 100 ms…

wvmarle:
In the toggle code you have to keep track of the previous state of the button, and only react on a state change (from unpressed to pressed)
Otherwise you toggle every 100 ms...

You're right! I altered my code -- hopefully it correct, now.