IR BreakBeam Count Up Timer

Hello everyone, I can't seem to get my basic code to stop looping.

I'm working on a countup timer that starts and stops with an Adafruit Breakbeam IR Sensor.

Right now I'm not worried about getting a colon to display or getting hours and minutes or even worried about a reset button.

I want the counter to start on the first break of the IR sensor, and then stop at the second break.

I am using an Arduino UNO, TM1637 7-Segment LED display, and an Adafruit Breakbeam IR Sensor with the Bounce2.h and TM1637.h libraries.

I have attached my code for anybody's perusal, the code starts just fine with the LED display off and the debouncer catches the instant the beam is broken and starts the clock.

What I can't seem to do is figure out how to get the counter to stop and display the count that was reached upon the stop.

I'm new to coding, but i have worked through 2 introduction books from Vilros and the official Arduino introduction book.

Any help with creating a function to stop the count and display the count would be greatly appreciated, thank you for your help.

LED_7_SEGMENT_W_BREAKBEAM2.ino (1.53 KB)

//Code to control a TM1637 display

#include <TM1637Display.h>
#include <Bounce2.h>


//CONSTANTS
const uint8_t OFF[] = {0, 0, 0, 0};
//In this library, the byte order is .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};

const int  breakBeam = 4;

Bounce beamDebouncer1 = Bounce();

Bounce beamDebouncer2 = Bounce();

const int resetButton = 13;

Bounce resetDebouncer2 = Bounce();

int sensorState = 0, lastState = 0;

//Create a display object, specifying parameters (Clock pin, Data pin)
TM1637Display display(2, 3);

void setup() {
  //Set brightness of LED
  display.setBrightness(4);
  //Clear the display
  display.setSegments(OFF);

  pinMode(breakBeam, INPUT);

  beamDebouncer1.attach(breakBeam);
  beamDebouncer1.interval(5);

  beamDebouncer2.attach(breakBeam);
  beamDebouncer2.interval(5);

  digitalWrite(breakBeam, HIGH);

  pinMode(resetButton, INPUT);

  resetDebouncer2.attach(resetButton);
  resetDebouncer2.interval(5);

  Serial.begin(9600);
}

void countup() {
  for(int i=0; i<100; i++) {
    //showNumberDec is a function for displaying numeric values
    display.showNumberDec(i, true, 4, 0);
    delay(80);
  }
}
void displayPlay() {
  display.setSegments(PLAY);
  delay(500);
}

bool beamState = LOW;
bool resetState = LOW;

void loop() {


  beamDebouncer1.update();

  if(beamDebouncer1.fell()){
    beamState = !beamState;
  }
  if(beamState){
  displayPlay();
  countup();
  display.setSegments(OFF);
  delay(120);
  }
  

}
//Code to control a TM1637 display

#include <TM1637Display.h>
#include <Bounce2.h>


//CONSTANTS
const uint8_t OFF[] = {0, 0, 0, 0};
//In this library, the byte order is .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};

const int  breakBeam = 4;

Bounce beamDebouncer1 = Bounce();

Bounce beamDebouncer2 = Bounce();

const int resetButton = 13;

Bounce resetDebouncer2 = Bounce();

int sensorState = 0, lastState = 0;

//Create a display object, specifying parameters (Clock pin, Data pin)
TM1637Display display(2, 3);

void setup() {
  //Set brightness of LED
  display.setBrightness(4);
  //Clear the display
  display.setSegments(OFF);

  pinMode(breakBeam, INPUT);

  beamDebouncer1.attach(breakBeam);
  beamDebouncer1.interval(5);

  beamDebouncer2.attach(breakBeam);
  beamDebouncer2.interval(5);

  digitalWrite(breakBeam, HIGH);

  pinMode(resetButton, INPUT);

  resetDebouncer2.attach(resetButton);
  resetDebouncer2.interval(5);

  Serial.begin(9600);
}

void countup() {
  for(int i=0; i<100; i++) {
    //showNumberDec is a function for displaying numeric values
    display.showNumberDec(i, true, 4, 0);
    delay(75);
  }
}
void displayPlay() {
  display.setSegments(PLAY);
  delay(500);
}

bool beamState = LOW;
bool resetState = LOW;

void loop() {
  
  beamDebouncer1.update();

  if(beamDebouncer1.fell()){
    beamState = !beamState;
  }
  if(beamState){
  displayPlay();
  countup();
  delay(120);
  } 
  beamState = LOW;
  display.setSegments(OFF);
  }

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