Can I use push button as...

Hello Aurduino social,,

I am a new student in this huge project,,
I have a question about push button,,
Can I use it as switch that means when I push it, the led turns on , and when I push it again , the led turn off..? If yes, How?

This is what I write in program

const int ledPin = 13;
const int pb = 2;
int val=0;
int vall=0;
void setup ( )

{
pinMode(ledPin, OUTPUT);
pinMode(pb, INPUT);

}

void loop()

{

val = digitalRead(pb);
vall=digitalRead(ledPin);

if (val ==HIGH)
{

if (vall==LOW)
{
digitalWrite(ledPin, HIGH);

}

else

{
digitalWrite(ledPin, LOW);
}

}

}

Thank you.

You need to detect when the button becomes pressed rather than when it is pressed and change the state of the LED when that happens. Look at the StateChangeDetection example in the IDE.

What you want is known as a T-FlipFlop. It looks at the input from the user like a button press and if it matches a set state, ie HIGH or LOW, then it toggles a flag or a single bit. This flag is basically what holds the state you want until the user presses the button again, in which case the flag is toggled again.

Ex. (This is just an example, it can be written much more compact)
Note this example does not prevent holding of the button

if( digitalRead(Pin_3) == HIGH ) // if the button's state is HIGH and matches the state the code is looking for, then it is true and allows it to go inside this block.
{
  static byte T_Flag = 0; // initialized to 0 once, then skipped over every time afterwards.
  
  T_Flag = !T_Flag; // this is where the flag is set to 1 or 0. If T_Flag = 1, then set it to 0, else if T_Flag = 0, then set it to 1

if( T_Flag == 1 ) // if T_Flag = 1, turn on the LED
  digitalWrite(LED_Pin, HIGH);
else              // T_Flag = 0, turn off the LED
  digitalWrite(LED_Pin, LOW);
}

Compact version:

if( digitalRead(Pin_3) == HIGH )
{
   digitalWrite(LED_Pin, !digitalRead(LED_Pin) ); // You are allowed read the LED pin's state even though it is set as an output. Rather convenient I would say.
  
  //Here we are reading the LED pin's state first then we are setting it to whatever the inverse of that state is. ! = logic NOT.
}

For a better example, look at the example provided with the Arduino IDE, StateChangeDetection

and try to keep your code tidy and consistently formatted. It makes it much easier to read, and find problems.

And post inside code tags (read the "how to post to this forum" link). Like this:

const int ledPin = 13;           
const int pb = 2;         
int val = 0;               
int valLedPin = 0;

void setup()
{
    pinMode(ledPin, OUTPUT); 
    pinMode(pb, INPUT);   
}

void loop()
{
    val = digitalRead(pb); 
    valLedPin = digitalRead(ledPin);

    if (val == HIGH)
    {
        if (valLedPin == LOW)
        {
            digitalWrite(ledPin, HIGH);             
        }
        else
        {
            digitalWrite(ledPin, LOW);
        }            
    }  
}

Thank you all for answer of my inquiry, and I am going to read more about StateChangeDetection.
I will keep it next time , MR arduinodlb

Thank you again.