-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
73 lines (59 loc) · 1.82 KB
/
code.cpp
File metadata and controls
73 lines (59 loc) · 1.82 KB
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
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
// Hardware Pin Definitions
#define LED PA6
#define SPEED_SENSOR PA0
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
// Timing and State Variables
unsigned long previousMillis = 0;
unsigned long lcdUpdateMillis = 0;
int blinkInterval = 500;
bool ledState = false;
// Initialize LCD
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("AUTOMOTIVE SYS V1.0");
}
void loop() {
unsigned long currentMillis = millis();
// 1. READ & MAP SPEED DATA
int potValue = analogRead(SPEED_SENSOR);
// Map 0-1023 to automotive blink range: 1000ms (60 BPM) to 250ms (240 BPM/Hyperflash)
blinkInterval = map(potValue, 0, 1023, 1000, 250);
// Calculate simulated speed (0-150 km/h) and BPM for the display
int simulatedSpeed = map(potValue, 0, 1023, 0, 150);
int currentBPM = 60000 / (blinkInterval * 2); // Standard BPM calculation
// 2. NON-BLOCKING BLINK LOGIC
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis;
ledState = !ledState;
digitalWrite(LED, ledState ? HIGH : LOW);
}
// 3. REFRESH LCD (Every 200ms to avoid flickering)
if (currentMillis - lcdUpdateMillis >= 200) {
lcdUpdateMillis = currentMillis;
lcd.setCursor(0, 1);
lcd.print("Speed: ");
lcd.print(simulatedSpeed);
lcd.print(" km/h "); // Spaces clear leftover digits
lcd.setCursor(0, 2);
lcd.print("Rate: ");
lcd.print(currentBPM);
lcd.print(" BPM ");
// Visual Dashboard Indicator
lcd.setCursor(0, 3);
if (ledState) {
lcd.print("<<< SIGNAL ON >>>");
} else {
lcd.print(" SIGNAL OFF ");
}
}
}