user13685: Hi i am writing a code where, i have to switch a variable called toggle from 0 to 1 to 0.... every time the button is pressed (the button is on a 2 pin). I am a little lost as I am new to arduino need some help. Thank you

user7727: Go here --> Built-In Examples. Read the examples for Button, Debounce, and Digital Input Pullup. Digital Input Pullup is the best single one of those 3 I think, but read them all.
Those 3 are key.
Also, I wrote a buttonReader (debouncing) library here.
Sounds like you're a beginner, so check out Adafruit's tutorials here.
And lastly, I've compiled links to all my knowledge at the bottom of my article here.

user10794: What you need is to detect a transition. That is, either:

It was closed and is now open.

Or:

It was open and is now closed.

To do that you need to "remember" the previous state of the switch and detect a change, like this:
const byte switchPin = 8;
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
bool toggle;

void setup ()
  {
  pinMode (switchPin, INPUT_PULLUP);
  }  // end of setup

void loop ()
  {
  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);

  // has it changed since last time?
  if (switchState != oldSwitchState)
    {
    oldSwitchState =  switchState;  // remember for next time 
    toggle = !toggle;   // toggle the variable
    delay (10);         // debounce
    }  // end of state change

  // other code here ...

  }  // end of loop


For more information see my post about switches.

13685: <p>Hi i am writing a code where, i have to switch a variable called toggle from 0 to 1 to 0.... every time the button is pressed (the button is on a 2 pin). I am a little lost as I am new to arduino need some help. Thank you</p>
 7727: <p>Go <a href="https://www.arduino.cc/en/Tutorial/HomePage" rel="nofollow">here</a> --> Built-In Examples. Read the examples for Button, Debounce, and Digital Input Pullup. Digital Input Pullup is the best single one of those 3 I think, but read them all.</p>

<p>Those 3 are key.</p>

<p>Also, I wrote a buttonReader (debouncing) library <a href="http://www.electricrcaircraftguy.com/2014/05/ercaguybuttonreader-library-for-arduino.html" rel="nofollow">here</a>.</p>

<p>Sounds like you're a beginner, so check out Adafruit's tutorials <a href="https://learn.adafruit.com/category/learn-arduino" rel="nofollow">here</a>.</p>

<p>And lastly, I've compiled links to all my knowledge at the bottom of my article <a href="http://www.electricrcaircraftguy.com/2014/01/the-power-of-arduino.html" rel="nofollow">here</a>.</p>
 10794: <p>What you need is to detect a <em>transition</em>. That is, either:</p>

<ul>
<li>It was closed and is now open.</li>
</ul>

<p>Or:</p>

<ul>
<li>It was open and is now closed.</li>
</ul>

<p>To do that you need to "remember" the previous state of the switch and detect a change, like this:</p>

<pre><code>const byte switchPin = 8;
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
bool toggle;

void setup ()
  {
  pinMode (switchPin, INPUT_PULLUP);
  }  // end of setup

void loop ()
  {
  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);

  // has it changed since last time?
  if (switchState != oldSwitchState)
    {
    oldSwitchState =  switchState;  // remember for next time 
    toggle = !toggle;   // toggle the variable
    delay (10);         // debounce
    }  // end of state change

  // other code here ...

  }  // end of loop
</code></pre>

<hr>

<p>For more information see my post about <a href="http://www.gammon.com.au/switches" rel="nofollow">switches</a>.</p>
