-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-contador-flexoes-tfm.ino
127 lines (100 loc) · 2.52 KB
/
arduino-contador-flexoes-tfm.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <LiquidCrystal.h>
#define BUTTON_RESET_PIN 6
#define ECHO_PIN 7
#define TRIGGER_PIN 8
#define LED_RED_PIN 9
#define LED_GREEN_PIN 10
#define BUZZ_PIN 13
uint32_t timer = 0;
//Define os pinos que serão utilizados para ligação ao display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int flexao = 0;
bool contando = false;
// variáveis limites de altura da distância
int dist_abaixo_cm = 5;
int dist_acima_cm = 25;
void setup()
{
setandoPinos();
Serial.begin(9600);
}
void loop()
{
long dist = (0.01723 * readUltrasonicDistance(TRIGGER_PIN, ECHO_PIN));
contandoFlexoes(dist);
Serial.println(dist);
exibindoTexto(dist);
resetContagem();
}
void resetContagem()
{
if (!digitalRead(BUTTON_RESET_PIN)){
flexao = 0;
digitalWrite(LED_GREEN_PIN, LOW);
digitalWrite(LED_RED_PIN, LOW);
contando = false;
}
}
void contandoFlexoes(long distancia)
{
noTone(BUZZ_PIN);
if (distancia < dist_abaixo_cm)
{
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_GREEN_PIN, LOW);
contando = true;
} else if (distancia > dist_acima_cm && contando)
{
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_GREEN_PIN, HIGH);
if (contando)
{
flexao++;
tone(BUZZ_PIN, 500);
delay(250);
}
contando = false;
}
}
void setandoPinos()
{
pinMode(BUTTON_RESET_PIN, INPUT_PULLUP);
digitalWrite(BUTTON_RESET_PIN, HIGH);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIGGER_PIN, LOW);
pinMode(BUZZ_PIN, OUTPUT);
noTone(BUZZ_PIN);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
lcd.begin(16, 2); // Define o display com 16 colunas e 2 linhas
lcd.clear(); // limpa a tela do display
}
void exibindoTexto(long texto)
{
lcd.setCursor(0, 0); // Define o cursor na posição de início
lcd.print("Qtd: ");
lcd.print(flexao);
lcd.print(" ");
if (millis() - timer >= 1000)
{
lcd.setCursor(0, 1); // Define o cursor na posição de início
lcd.print("Dist: ");
lcd.print(texto);
lcd.print(" ");
timer = millis(); // Atualiza a referência
}
}
long readUltrasonicDistance(int trigger, int echo)
{
pinMode(trigger, OUTPUT); // Clear the trigger
digitalWrite(trigger, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
pinMode(echo, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echo, HIGH);
}