Blink without delay & debounce button

Does anyone know how you could merge blink without delay and debounce button code? or have some kind of tutorial for it.

Blink without delay

const int ledPin =  11;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;

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

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

Debounce

const int buttonPin = 8;
const int ledPin = 11;

int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
}

I'm trying but i'm stuck, because both of them use millis(). I don't know how i could fix this.

Both those examples are just reading from / comparing the current value of Millis(), they are not changing it in any way, so you can run both functions without any problem.

You might find Robins code example of help.

robin.ino (8.86 KB)

ricky101:
Both those examples are just reading from / comparing the current value of Millis(), they are not changing it in any way, so you can run both functions without any problem.

You might find Robins code example of help.

Although i'm having a hard time understanding some of it. I'll try thanks.

rephrase your question.
what is it you are trying to do or to achieve ?
combing two things that do not result in answering your needs is pointless.

google xy problem

Muridious:
I'm trying but i'm stuck, because both of them use millis(). I don't know how i could fix this.

There is nothing to fix. They can be placed one after the other inside the loop() function. You might have to change the initial "grab" of the millis value so it does it only once.

Just a guess, you want the button push to influence the blink in some way? You will also have to add logic to connect the two.

See:
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Well i plan on using the button to turn off all the functions that don't involve the button itself so that it is basically an off switch for the program.
I need them combined so that i can stop using delay() to make a led flicker. Which is needed to give off a approximate range with a ultrasonic sensor. it basically should flicker faster and faster the closer you get to an object.

ricky101:
Both those examples are just reading from / comparing the current value of Millis(), they are not changing it in any way, so you can run both functions without any problem.

You might find Robins code example of help.

EXAMPLE: previousButtonMillis += buttonInterval;
what does += do in this line?

It adds buttonInterval to previousButtonMillis, and assigns the result to previousButtonMillis

Its the same as: previousButtonMillis = previousButtonMillis + buttonInterval;

search compound operators

Muridious:
Well i plan on using the button to turn off all the functions that don't involve the button itself so that it is basically an off switch for the program.
I need them combined so that i can stop using delay() to make a led flicker. Which is needed to give off a approximate range with a ultrasonic sensor. it basically should flicker faster and faster the closer you get to an object.

so, what you really want is to have an LED blink as a representation of the value taken from an ultrasonic sensor.
closer the object, the faster the blink.
you do not need debounce if you have an on/off switch. for a momentary, you could use it.
what type of switch are you using ?

Hutkikz:
Its the same as: previousButtonMillis = previousButtonMillis + buttonInterval;

search compound operators

Thanks, i was having trouble finding them

dave-in-nj:
so, what you really want is to have an LED blink as a representation of the value taken from an ultrasonic sensor.
closer the object, the faster the blink.
you do not need debounce if you have an on/off switch. for a momentary, you could use it.
what type of switch are you using ?

Yes, that is exactly what I want. I tried the button withour debounce before, but it didn't seem to work very well. So someone recommended i used debounce.

I'm using a button as a switch.
when you press it it has to turn off all the calculations for measuring the distance.
when you press it again it should turn on all the calculations for measuring distance.

This is where I'm at now, starting from scratch and using robin.ino from earlier in this topic as an example.

Note: in this the led has been replaced by a buzzer to make this useable for blind people. So instead of flashing a light faster and faster it should make a sound faster and faster, though the name won't make any difference I thought I should clarify.

const int trigger = 13;
const int echo = 12;
const int buzzer = 11;
const int button = 8;

const int buttonInterval = 300;
const int blinkDuration = 500;

int buttonState;
unsigned long previousButtonMillis;

void setup() {
  pinMode(echo, INPUT);
  pinMode(button, INPUT_PULLUP);
  pinMode(trigger, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin (9600);
  Serial.println("ultrasonicmeasuring.ino has been started.");
}

void loop() {
  currentMillis = millis();
  readButton();
  if(digitalRead(button) == HIGH) {
    measure();
  }
}

void readButton() {
  if (currentMillis - previousButtonMillis >= buttonInterval && digitalRead(button) == LOW) {
    buttonState = !buttonState;
    previousButtonMillis += buttonInterval;
  }
}

void measure() {

}

You using pullup for your button

pinMode(button, INPUT_PULLUP);

but you want measure() run in HIGH, which alway run.

  if(digitalRead(button) == HIGH) {
    measure();
  }

I removed INPUT_PULLUP and made it INPUT

const int trigger = 13;
const int echo = 12;
const int buzzer = 11;
const int button = 8;

const int buttonInterval = 300;
const int blinkDuration = 500;

unsigned long previousButtonMillis;
int buttonState;

int buzzerState = LOW;


void setup() {
  pinMode(echo, INPUT);
  pinMode(button, INPUT);
  pinMode(trigger, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin (9600);
  Serial.println("ultrasonicmeasuring.ino has been started.");
}

void loop() {
  currentMillis = millis();
  readButton();
  if(digitalRead(button) == HIGH) {
    measure();
    displayMeasurements();
  }
}

void readButton() {
  if (currentMillis - previousButtonMillis >= buttonInterval && digitalRead(button) == LOW) {
    buttonState = !buttonState;
    previousButtonMillis += buttonInterval;
  }
}

void measure() {
  long duration, distance;
  // trigger off, on, off
  duration = pulseIn(echo,HIGH);
  distance = (duration / 2) / 29.1;
}

void displayMeasurements() {
  
}

Where i wrote // tirgger off, on, off. Is this another case of blinking without delay? although it should be triggering without delay in this case.

What happens if you hold the button?

void readButton() {
  if (currentMillis - previousButtonMillis >= buttonInterval && digitalRead(button) == LOW) {
    buttonState = !buttonState;
    previousButtonMillis += buttonInterval;
  }
}

When i hold the button shortly it jumps on and off again until i release it. which is when it switches to on or off.

const int trigger = 13;
const int echo = 12;
const int buzzer = 11;
const int button = 8;

const int buttonInterval = 300;
const int blinkDuration = 500;

unsigned long currentMillis = 0;
unsigned long previousButtonMillis;
int buttonState;

int buzzerState = LOW;


void setup() {
  pinMode(echo, INPUT);
  pinMode(button, INPUT);
  pinMode(trigger, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin (9600);
  Serial.println("ultrasonicmeasuring.ino has been started.");
}

void loop() {
  currentMillis = millis();
  readButton();
  Serial.println(buttonState);
  if(digitalRead(button) == HIGH) {
    measure();
    displayMeasurements();
  }
}

void readButton() {
  if (millis() - previousButtonMillis >= buttonInterval && digitalRead(button) == LOW) {
    buttonState = !buttonState;
    previousButtonMillis += buttonInterval;
  }
}

void measure() {
  long duration, distance;
  // trigger off, on, off
  duration = pulseIn(echo,HIGH);
  distance = (duration / 2) / 29.1;
}

void displayMeasurements() {
  
}

LarryD:
What happens if you hold the button?

void readButton() {

if (currentMillis - previousButtonMillis >= buttonInterval && digitalRead(button) == LOW) {
    buttonState = !buttonState;
    previousButtonMillis += buttonInterval;
  }
}

I forgot to add some longs / int's up above so i added them and i made in print the buttonState to report what's happening, to you.

Muridious:
I removed INPUT_PULLUP and made it INPUT

Great. Did you add an external resistor to replace the internal pull up? How is your switch wired?

aarg:
Great. Did you add an external resistor to replace the internal pull up? How is your switch wired?

Well it's not the result i want yet. It should just turn on or off on a button press. I shouldn't have to hold it for practical reasons.

and yes i did put a resistor in there.

This is the wiring.

I'm also currently working on the blinking without delay, and hoping to be able to reproduce it somehow for the trigger, otherwise i'd have to use delayMicroseconds() in there.

Sadly, the example(s) of Blink Without Delay that are inside of robin.ino are too difficult for me to get out and use them myself.

It takes time to learn this stuff.