Help me understand the logic here

Try this, and try to make sense of it:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(2, INPUT_PULLUP);   // will read HIGH unless... 

  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);       // something drags it LOW
}

void loop() {
  if  (digitalRead(2) == LOW) {
    myFunction();
  }
}

void myFunction(){
  digitalWrite(13, HIGH);
  delay(500);  
  digitalWrite(13, LOW); 
  delay(500); 
}

So now pin 2 will read HIGH unless you ground it with a switch, like normal people, or are experimenting and use a logically LOW output pin 3 and connect 2 to 3 with a jumper.

And as pointed out, as long as anything is making the input read LOW, the build in LED will blink. And blink. And blink…

HTH

a7

Resistor.
Value ? No idea. workshop guy said 10k or 100k.
What dictates the value of the resistor in this instance ?

Both values will work, it’s not too critical.


The input impedance of an input pin by itself is ~ 100 mega ohms.

As a result, a pull-up or pull-down can be quite large.


The smaller this resistor is the less susceptible the input is to electrical noise around your project.

A 10k resistor is a good trade off and most Arduino kits supply them.

If the input was experiencing noise interference when the pull-up/down was 10k, you can lower it to 1k which makes it a stronger pull-up/down.


Some times we need to add a parallel capacitor (100nF) across these resistors to further stop noise interference.

Watch this:

If you use a pin to read a switch, use INPUT_PULLUP mode.
The pin gets a low 5V current through 20K to 50K resistance, not enough power to burn the pin out. Jumper that to GND for a switch, don't use an IO pin to ground another.

My test button is a jumper I touch to the grounded USB port box to "press".

I wanted to use piezo disks as tap and force sensors, I ended up filling a wire with charge proportional to the tap.
The wire holds that charge until digital reads drains it... digital pins 'eat' 1 microamp per readin a loop that reads 1 million times a second until the pin goes LOW and the count is an analog measure of the tap force.

1 Like

I got 10K to 5V and it seems to work but in reverse.
I tried to GND and it does not work. Not sure how to reverse the reverse effect.

Anyway i ran into another problem not sure if its a hardware or a software issue.
I am trying to get the LED blink few times and then stay ON as long as i hold the button.
But sometimes when i let go (sometimes) code will run again and blink button few times instead of just turning it off.

Here i my code.

int ledPin = 3;
int buttonPin = 2;

void setup(){
  Serial.begin(9600);
  pinMode (ledPin,OUTPUT);
  pinMode (buttonPin, INPUT);
}

void loop(){
  Serial.println(digitalRead(buttonPin));
  if (digitalRead(buttonPin) == LOW) {
    Serial.println("button pressed");
    digitalWrite(ledPin,HIGH);
    delay (100);
    digitalWrite (ledPin,LOW);
    delay (60);
    digitalWrite(ledPin,HIGH);
    delay (100);
    digitalWrite (ledPin,LOW);
    delay (60);
    digitalWrite(ledPin,HIGH);
    delay (100);
    digitalWrite (ledPin,LOW);
    delay (60);
    digitalWrite(ledPin,HIGH);
    while (digitalRead(buttonPin) == LOW);
  }
  else
  digitalWrite (ledPin,LOW);
}

Detect when button becomes pressed, not when button is pressed. It’s called edge detection. You can detect either edge as required. Look up debouncing

Ok i will. Thanks !

I tried different approach but the loop keeps exiting on its own.
Have a look please.
Instead of this line

while (digitalRead(buttonPin) == LOW);

i made a function which is:

void myFunc(){
  loop();
    {
      Serial.println("looping");
      delay (1000);
    }
}

It should loop infinitely once entered the function but it does not. It loops exactly as many times as the main loop has looped and exits back to main loop for some reason.
How is this even possible ?
It makes no sense from a logic perspective. It should loop forever until condition is......there is no condition, i just want it to loop.

Why ?
I need LED to stay ON as long as i hold the button.
I mean with the pattern i have, LED will blink few times and stay ON while i hole the button.
I dont know of any other way to make it happen other then enter a loop that keepts the LED on HIGH, but it does not work. It keeps blinking OVER AND OVER AND OVER


#define PUSHED         LOW
#define RELEASED       HIGH

const byte buttonPin = 2;
const byte ledPin    = 3;

byte lastState       = RELEASED;

//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode (ledPin, OUTPUT);

  pinMode (buttonPin, INPUT_PULLUP);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //******************************************          buttonPin
  byte currentState = digitalRead(buttonPin);

  //was there a switch change in state ?
  if (lastState != currentState)
  {
    //update to the new state
    lastState = currentState;

    //is the switch pushed now
    if (currentState == PUSHED)
    {
      Serial.println("Button is pushed.");

      digitalWrite(ledPin, HIGH);
      delay (100);
      digitalWrite (ledPin, LOW);
      delay (60);
      digitalWrite(ledPin, HIGH);
      delay (100);
      digitalWrite (ledPin, LOW);
      delay (60);
      digitalWrite(ledPin, HIGH);
      delay (100);
      digitalWrite (ledPin, LOW);
      delay (60);
      digitalWrite(ledPin, HIGH);
    }

    //the switch was released
    else
    {
      Serial.println("Button is released.");

      digitalWrite (ledPin, LOW);
    }

  } //END of   buttonPin

  //******************************************


} //END of   loop()

I will have to dig into your example to understand how it works. Thank you it does work.
For now, i opted out to do this instead, something i can understand at my monkey level.

while (1){
      Serial.println("Holding");
      if (digitalRead(buttonPin) == HIGH) {
        break; 
      }
    }

Not sure what are the side effects of this are other then your code is 41 lines and mine is 35 but it behaves just like yours.
Thanks !

The code offered to you is NON BLOCKING CODE
i.e. other things can go on while you are pushing/holding the switch.

In your code, the while ( . . . ) loop is blocking code,
i.e. it freezes other code from running when you are in the while loop.

I see.
Thank you for clarifying it to me in a way i can understand.
There is allot for me to learn here for sure.
I used to program allot using language called AutoIT for windows, some html, and php once in a blue moon.

I plan on making a break light blinker for my car. I wan to use a mosfet to blink 12v going into the light bulbs of the break lights using arduino.
Some have done it already but i dont like their approach. I want my arduino powered all the time so i will likely step down from 12v (after ignition) to 5v, that way arduino is ON as long as the car key is turned ON.

I suppose sky is the limit after that :laughing:

It seems like a lot but the basics are the basics and they are difficult until you know how. The basic concepts you should know to do your project:

The loop function loops, faster is better, use non blocking code.
Edge detection
State machines
Millis timing (blink without delay example in ide)
Basic button hardware as above.
The arduino is NOT a power supply, it is a logic supply

1 Like

pinMode( buttonPin, INPUT_PULLUP );

That's the pin to digital read. A LOW means the switch is closed and the low current 5V from the chip is safely flowing to GND (ground). A HIGH means the switch is open, current can't flow.

Did you take physics in HS?

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