# I´m missing something switch --\> toggle switch

**URL:** <https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271>\
**Category:** Programming\
**Created:** [June 3, 2023, 4:15pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271 "2023-06-03T16:15:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_masked\_beginner](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@the\_masked\_beginner](https://forum.arduino.cc/u/the_masked_beginner)\
**Post date:** [June 3, 2023, 4:15pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/1 "2023-06-03T16:15:09Z")

</div>

Hello. I´m new here. I´m from Spain and I don´t speak english very well, please, excuse me if I make mistakes.

I have started learning arduino and I see this code to use a switch as a toogle switch. The idea is to use pull-up resistor to get in pin 7 a HIGH value all the time and when I push the button get LOW and a moment later HIGH again. I have tried it and it works.

```arduino
int LEDs=0;
int new;
int old=1;
int led=4;
void setup() {
pinMode(7,INPUT_PULLUP);
pinMode(4,OUTPUT);
}

void loop() {
new=digitalRead(7);		
  delay(100);
if (old==0 && new==1) {      	
  if (LEDs==0)			
  { digitalWrite(4,HIGH);	
  LEDs=1;}				
  else  				
  {digitalWrite(4,LOW);		
  LEDs=0;}				
}
old=new;				
}

```

What I am missing is how the code know I pushed the button, I mean, the condition is that it has to read a LOW or 0 value in pin 7, but I don´t see where it do it. I think it something related to "old" but I can´t figure out.

I really appreciate any help or advice. For sure there will be easier wways to make this, but I really like to understand whats going on, at least, this is my way to learn, being sure that I understand everything.

Thank you!

Best regards

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 3, 2023, 4:23pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/2 "2023-06-03T16:23:30Z")

</div>

Welcome to the forum

- The previous value read from the switch pin is saved in the value named old at the end of the loop() function

```arduino
old=new;

```

- the current value is read and put into the new variable at the start of loop()

```arduino
new=digitalRead(7);	

```

- the two values are compared and if old equals 1 (ie not pressed) and new equals zero (ie pressed) then you know that the switch has become open

```arduino
if (old==0 && new==1)

```

If you wish to test whether the switch has become closed then use

```arduino
if (old==1 && new==0)

```

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [June 3, 2023, 4:24pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/3 "2023-06-03T16:24:37Z")

</div>

'new' is a keyword and should not be redefined as a variable name...

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 3, 2023, 4:26pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/4 "2023-06-03T16:26:20Z")

</div>

Good point

Personally I would use currentValue and previousValue as the variable names and HIGH and LOW instead of 1 and 0 in the tests

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [June 3, 2023, 4:33pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/5 "2023-06-03T16:33:44Z")

</div>

```arduino
const Button_pin = 4;
const OUTPUT_pin = 7;
#define isButPres (digitalRead(7)==LOW)

void setup() {
  pinMode(Button_pin, INPUT_PULLUP);
  pinMode(OUTPUT_pin, OUTPUT);
}

void loop() {
  static bool oldValue = false;
  if (!oldValue && isButPres) {
    digitalWrite(OUTPUT_pin, !digitalRead(OUTPUT_pin));
    delay(20);
    while(isButPres);
    oldValue = true;
  }
  if (oldValue && !isButPres)oldValue = false;
}
```

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [June 3, 2023, 4:42pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/6 "2023-06-03T16:42:37Z")

</div>

> [@the\_masked\_beginner](#):
>
> I mean, the condition is that it has to read a LOW or 0 value in pin 7, but I don´t see where it do it.

You read the input pin and if it returns zero then the button is pushed. If it reads HIGH the the button is not being pressed.

What you code is doing is comparing the current state of the switch with what is was the last time you read it. If it is now HIGH (variable new) and last time it was read it was LOW (variable old) then the action of lighting the LEDS is undertaken.

**BUT** this results in changing the LEDs when the push button switch is released not when it is pressed.

---

<div class="post-metadata">

**Author:** ![the\_masked\_beginner](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@the\_masked\_beginner](https://forum.arduino.cc/u/the_masked_beginner)\
**Post date:** [June 3, 2023, 7:22pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/8 "2023-06-03T19:22:04Z")

</div>

Thank you all for your answers! The trees didn´t let me the forest.

By the way, could you recommend me a free course of arduino? I was following a guy in youtube, but he doesn´t really explain the things, it only write the code and explain the surface...

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [June 3, 2023, 7:33pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/9 "2023-06-03T19:33:55Z")

</div>

Hello

Take view here:

> **[Arduino Tutorial for Beginners | Starting Electronics](https://startingelectronics.org/beginners/arduino-tutorial-for-beginners/)**
>
> Arduino tutorial for beginners in electronics. How to get started with Arduino building circuits on solderless breadboard and programming.

hth

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 3, 2023, 9:22pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/10 "2023-06-03T21:22:07Z")

</div>

If you want to start at **programming** the programming-tutorial starts here

> **[Arduino Programming Course Contents](https://startingelectronics.org/software/arduino/learn-to-program-course/)**
>
> Learn to program the Arduino course contents. Learn the Arduino programming language and start writing your own sketches.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [June 4, 2023, 3:29am UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/11 "2023-06-04T03:29:15Z")

</div>

And as [languages reference](https://www.learncpp.com/cpp-tutorial/introduction-to-cplusplus/) you might use:

[https://www.learncpp.com/cpp-tutorial/introduction-to-cplusplus/](https://www.learncpp.com/cpp-tutorial/introduction-to-cplusplus/)

---

<div class="post-metadata">

**Author:** ![the\_masked\_beginner](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@the\_masked\_beginner](https://forum.arduino.cc/u/the_masked_beginner)\
**Post date:** [June 4, 2023, 8:17pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/12 "2023-06-04T20:17:51Z")

</div>

Thank you all a lot for yours answers and help!

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 4, 2023, 11:06pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/13 "2023-06-04T23:06:38Z")

</div>

> [@the\_masked\_beginner](#):
>
> course of arduino?

Free... Youtube's Programming Electronics Academy  
https://www.youtube.com/embed/TItCq9deSr0?feature=oembed&wmode=opaque&list=PLYutciIGBqC34bfijBdYch49oyU-B_ttH

https://www.youtube.com/embed/QtSxTtMUtfc?feature=oembed&wmode=opaque&list=PLYutciIGBqC2JEEnl2wAJY_6hTgtc96GY

[edit] adding one more ... DroneBotWorkshop - very thorough, very clear, some advanced.

> **[DroneBot Workshop](https://www.youtube.com/c/Dronebotworkshop1)**
>
> Welcome to the Workshop!
> 
> The DroneBot Workshop is the place where I work with Arduino, Raspberry Pi, ESP32, and lots of other cool microcontrollers, components, and sensors.
> 
> This is a channel for learning and understanding. While I do build...

---

<div class="post-metadata">

**Author:** ![the\_masked\_beginner](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@the\_masked\_beginner](https://forum.arduino.cc/u/the_masked_beginner)\
**Post date:** [June 5, 2023, 9:27pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/14 "2023-06-05T21:27:37Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 2, 2023, 9:27pm UTC](https://forum.arduino.cc/t/i-m-missing-something-switch-toggle-switch/1134271/15 "2023-12-02T21:27:50Z")

</div>

Closed on December 2, 2023, 9:27pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
