#include "SevSeg.h"
// Define the pins for the seven segment display
SevSeg sevseg;
// Define the pins for the start and stop buttons
const int START_BUTTON_PIN = A1;
const int STOP_BUTTON_PIN = A2;
// Define the variables to store the count and the state of the buttons
int count = 0;
bool started = false;
void setup() {
// Initialize the seven segment display
sevseg.begin(COMMON_CATHODE, 4, 3, 5, 6, 7, 8, 9);
// Initialize the start and stop buttons as inputs with pull-up resistors
pinMode(START_BUTTON_PIN, INPUT_PULLUP);
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// Read the state of the start and stop buttons
bool startButtonState = digitalRead(START_BUTTON_PIN);
bool stopButtonState = digital…
[09.09, 24/3/2023] Nur Djaya Atmaja: #include "SevSeg.h"
// Deklarasi pin untuk tujuh segmen
#define SEVSEG_CLK 2
#define SEVSEG_DIO 3
// Deklarasi pin untuk tombol Start/Stop
#define BUTTON_PIN 4
// Deklarasi variabel untuk penghitung
volatile int count = 0;
// Deklarasi variabel untuk membaca status tombol
volatile bool buttonPressed = false;
// Inisialisasi objek SevSeg
SevSeg sevseg;
void setup() {
// Set up tujuh segmen
sevseg.begin(COMMON_CATHODE, 4, 3, 2, 1, 5, 6, 7, 8);
sevseg.setBrightness(50);
// Set up tombol Start/Stop
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISR, FALLING);
// Tampilkan nol pada tujuh segmen
sevseg.setNumber(0, 0);
}
void loop() {
if (buttonPressed) {
// Reset variabel tombol
buttonPressed = false;
// Mulai menghitung
while (true) {
// Tampilkan angka penghitung pada tujuh segmen
sevseg.setNumber(count);
// Tunggu 1 detik
delay(1000);
// Tambahkan penghitung
count++;
// Cek apakah tombol Start/Stop ditekan kembali
if (buttonPressed) {
// Reset variabel tombol
buttonPressed = false;
// Hentikan penghitungan dan tampilkan hasil akhir
sevseg.setNumber(count);
break;
}
}
}
}
// Interrupt Service Routine untuk tombol Start/Stop
void buttonISR() {
buttonPressed = true;
}