I´m missing something switch --> toggle switch

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.

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

1 Like

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
old=new;
  • the current value is read and put into the new variable at the start of loop()
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
if (old==0 && new==1)

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

if (old==1 && new==0)
2 Likes

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

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

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;
}

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.

1 Like

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...

Hello

Take view here:

hth

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

And as languages reference you might use:

https://www.learncpp.com/cpp-tutorial/introduction-to-cplusplus/

Thank you all a lot for yours answers and help!

Free... Youtube's Programming Electronics Academy

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

thank you!

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