Одновременно не работает таймер и счетчик нажатий на кнопку
- Войдите на сайт для отправки комментариев
Ср, 08/06/2022 - 23:55
Всем доброго времени суток. Дали интересный проект, как счетчик отжиманий на время, но столкнулся с одной проблемой: не работает одновременно таймер времени и счетчик нажатий на кнопку. по отдельности все прекрасно работает. Время вывожу на семисегментный дисплей TM1637, количество нажатий на жк дисплей 1602a.
https://github.com/johnrickman/LiquidCrystal_I2C это 1602 жк дисплей
https://github.com/avishorp/TM1637 это семисегментник
в ардуино я не бум-бум, пока начинаю, поэтому основной код взял с интернета. Если можно, то причесать код хоть чуть-чуть и помочь решить эту беду.
//*************************** LIBRARIES ********************
#include <TM1637Display.h> // Tm1637 module
#include <LiquidCrystal_I2C.h> //Display
#define _LCD_TYPE 1 // для работы с I2C дисплеями
#include <EncButton.h>
EncButton<EB_TICK, 3> btn(INPUT_PULLUP);
volatile int counter = 0;
//*************************** GLOBALS ********************
// TM1637 Module connection pins (Digital Pins)
#define CLK 5
#define DIO 4
TM1637Display display(CLK, DIO);
//buzzer pin
int buzzer = 9;//the pin of the active buzzer
// Button
#define buttonPin 2
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int count_start = 0;
// time calc
#define numberOfSeconds(_time_) ((_time_ / 1000) % 60)
#define numberOfMinutes(_time_) ((_time_ / 1000) / 60)
// hold time selected in ms.
unsigned long timeLimit = 0;
//pot pin Analog A0
int potPin = 0;
// Varibles to Sample and smooth the A0 input from pot to reduce gitter on display
// Define the number of samples to keep track of. The higher the number, the
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
//*************************** SETUP ************************
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
// button setup
// Setup pins for button enable internal pull-up resistors
digitalWrite(buttonPin, HIGH);
//display setup
display.setBrightness(0x0c);
display.showNumberDecEx(0, 0x40, true);
//buzzer
pinMode(buzzer, OUTPUT); //initialize the buzzer pin as an output
digitalWrite(buzzer, LOW);
// initialize all the readings in array to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
lcd.init(); // инициализация
lcd.backlight(); // включить подсветку
lcd.setCursor(1, 0); // столбец 1 строка 0
lcd.print("VF BSUIR");
delay(3500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("COUNT:");
pinMode(3, INPUT_PULLUP);
}
void btnIsr() {
counter++; // + нажатие
}
//******************* MAIN LOOP *****************
void loop() {
// read the pot
if (count_start == 0)
{
potRead();
}
// read and debounce push button.
int reading = digitalRead(buttonPin);
// If the switch changed?
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// start timer if the new button state is low
if (buttonState == LOW) {
TestButton();
}
}
}
lastButtonState = reading;
delay(5);
lcd.setCursor(8,1);
count();
}
// ********************* FUNCTIONS *************************
// TestButton: Function to handle button press
void TestButton()
{
count_start ++;
if (count_start == 1)
{
countdown();
my_buzzer();
count();
}
}
void count() {
btn.tick();
if (btn.isClick()) {
static int count = 0;
count++;
lcd.setCursor(4, 1); // столбец 4 строка 1
lcd.print(count);
}
}
// potRead: Function to read analog pin connected to pot and display it in 7-seg
// includes smoothing the analog sample by taking avergae of 10 readings
void potRead()
{
unsigned long average = 0;
// subtract the last reading:
total = total - readings[readIndex];
readings[readIndex] = map(analogRead(potPin), 0, 1023, 1, 99);
// increment total and array
total = total + readings[readIndex];
readIndex = readIndex + 1;
// check for end of array, wrap to start if yes
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
timeLimit = ((average * 60) * 1000);
average = average * 100 ;
display.showNumberDecEx(average, 0x40, true);
//delay(100);
}
//my_buzzer: function to switch on buzzer and display "done" on 7-seg
void my_buzzer (void)
{
unsigned char i;
unsigned char j;
const uint8_t SEG_DONE[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
display.setSegments(SEG_DONE);
// run for 250*1400ms = 3.5 minutes
for (j = 0; j < 250; j++)
{
//output an frequency
for (i = 0; i < 100; i++)
{
digitalWrite(buzzer, HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer, LOW);
delay(1);//wait for 1ms
}
//output another frequency
for (i = 0; i < 100; i++)
{
digitalWrite(buzzer, HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer, LOW);
delay(2);//wait for 2ms
}
}
}
// countdown: function to handle and display the countdown time
void countdown() {
// Calculate the time remaining
unsigned long runTime = 0;
runTime = millis();
unsigned long timeRemaining = timeLimit - (millis() - runTime);
while (timeRemaining > 50 )
{
// To display the countdown in mm:ss format, separate the parts
int seconds = numberOfSeconds(timeRemaining);
int minutes = numberOfMinutes(timeRemaining);
// This displays the seconds in the last two places
display.showNumberDecEx(seconds, 0, true, 2, 2);
// Display the minutes in the first two places, with colon
display.showNumberDecEx(minutes, 0x80 >> 3, true, 2, 0);
// Update the time remaining
timeRemaining = timeLimit - (millis() - runTime);
}
}
Один вызов my_buzzer() занимает 3.5 минуты.
Вы это серьезно?
Так ведь это время, которое динамик будет пищать, не столь значимо
Так в это время ничего другого работать не может.
Динамик пищит же в конце отсчёта заданного времени. Изменение этого цикла результата не принесло, лишь только уменьшилось количество времени, сколько динамик пищит
Ну тогда виноват цикл while в строке 204
Я когда замыкаю кнопку на начало отсчёта времени, перестаёт работать счётчик нажатий. Не особо наблюдаю связь с этим циклом. Либо это я глупый
Я бы с радостью, сижу теперь изучаю основы ардуино. Просто нужно было срочно закончить проект, за который мне обещали сессию автоматом закрыть на 4 :) и дали неделю сделать. Проблему решил, повесив ещё одну ардуину на подсчёт нажатий
ужас , а потом что бы вывести значение сердечного ритма , еще 1 экран и еще ардуинка понадобиться ?
еще ардуинка понадобиться ?
Я ж говорю, проект сдам, и начну переписывать код, пока что вот такие костыли))