adding Waits to a sketch

albireo13

Active member
I have a Teensy 4.0 which I am using to control some other HW through I2C (DACs and ctrl bits).
Currently, I have the code running which steps through different programmed states and loops. What I want to do is, make a few measurements after each state change using a DMM. I have added a 3sec delay after each step to do this.

What I'd prefer is to somehow, after each programmed state change, have the Teensy wait for a button push or something before stepping to the next state.
I have looked but don't seem to find any way to do this. Is this possible?
 
You might wire up a button to an IO pin and use something like the bounce library.
For example, suppose you wire up a button to Pin 2, and the other leg of the switch is wired to ground.
Initialize pin 2 as: INPUT_PULLUP
When the button is pressed doing a digitalRead(2) will return 0...

With your code, you then loop waiting for pin 2 to go low. But often times when you do something like this you may get
several HIGH/LOW changes as the button is pressed, so the bounce library is setup to handle the bouncing of the signal.

There are several ways of doing it, here is a quick and drity.

C++:
#include <Bounce.h>

#define BUTTON 2

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 );

void setup() {
  pinMode(BUTTON,INPUT_PULLUP);
}

void loop() {
  Do_step_one();

 Wait_for_button_press();

  Do_Next_step();
...
}

// wait for the a new button press... 
// could add lots of other tests and the like.
void Wait_for_button_press() {
  for(;;) {
    bouncer.update ( );
    if (bouncer.fallingEdge()) return;
  }
}

Probably lots of cleaner ways to write this, but hopefully at least shows one way.
 
If the Teensy is online to a computer with SerMon - showing the results?

The Teensy could then do a set of readings and wait for incoming USB entry to repeat?
 
Back
Top