#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128,64,&Wire,-1);
volatile uint32_t edge=0, period=0;
volatile bool seen=false;
float rpm=0;
int limitRpm=190, light=0;
bool automatic=true, lamp=false, alarm=false, pending=false, ready=false;
uint32_t brightAt=0;
struct Button {
uint8_t pin;
bool raw=HIGH, stable=HIGH;
uint32_t changed=0;
explicit Button(uint8_t p):pin(p) {}
bool pressed(uint32_t now) {
bool level=digitalRead(pin);
if(level!=raw){raw=level;changed=now;}
if (stable == raw || (raw == HIGH && now - changed < 30)) return false;
stable=raw;
return stable==LOW;
}
};
Button mode(PA8), limitKey(PB12), lampKey(PB13);
constexpr uint32_t LIMIT_HOLD_MS = 3000;
constexpr int MIN_LIMIT_RPM = 100;
constexpr int MAX_LIMIT_RPM = 290;
constexpr int LIMIT_STEP_RPM = 10;
int pendingLimitRpm = 190;
bool settingLimit = false;
void updateLimitSetting(uint32_t now) {
static uint32_t pressedAt = 0;
static bool tracking = false;
static bool parkedForPress = false;
const bool newPress = limitKey.pressed(now);
const bool moving = seen;
if (moving) settingLimit = false;
if (newPress) {
pressedAt = now;
tracking = true;
parkedForPress = !moving;
}
if (!tracking) return;
if (moving) parkedForPress = false;
if (limitKey.stable == HIGH) {
tracking = false;
if (limitKey.changed - pressedAt >= LIMIT_HOLD_MS) return;
if (moving && !parkedForPress && limitRpm < MAX_LIMIT_RPM) {
limitRpm += LIMIT_STEP_RPM;
} else if (parkedForPress && settingLimit) {
pendingLimitRpm = pendingLimitRpm >= MAX_LIMIT_RPM
? MIN_LIMIT_RPM : pendingLimitRpm + LIMIT_STEP_RPM;
}
} else if (limitKey.raw == LOW && now - pressedAt >= LIMIT_HOLD_MS) {
tracking = false;
if (!parkedForPress) return;
if (settingLimit) limitRpm = pendingLimitRpm;
else pendingLimitRpm = limitRpm;
settingLimit = !settingLimit;
}
}
void hall(){
uint32_t now=micros(), dt=now-edge;
if(seen && dt<2000)return;
period=seen && dt<2200000UL ? dt : 0;
edge=now; seen=true;
}
void speed(){
noInterrupts();
uint32_t age=micros()-edge, p=period;
if(age>=2200000UL){seen=false;period=0;}
interrupts();
rpm=p && age<2200000UL ? 10000000.0f/(p>age?p:age) : 0;
}
void lighting(uint32_t now){
light=analogRead(PA0);
if(!automatic)return;
if (light >= 1756) lamp = true; // About 150 lux in Wokwi.
if (!lamp || light > 1410) { pending = false; return; } // About 250 lux.
if(!pending){pending=true;brightAt=now;}
if(now-brightAt>=10000){lamp=false;pending=false;}
}
void outputs(uint32_t now){
static uint32_t beep=0;
bool old=alarm;
alarm = static_cast<int>(rpm + 0.5f) > limitRpm;
digitalWrite(PB10,alarm);digitalWrite(PB1,lamp);
if(alarm && (!old || now-beep>=1000)){beep=now;tone(PB0,2000,250);}
if(!alarm && old)noTone(PB0);
}
void displayStatus(){
if(!ready)return;
oled.clearDisplay();oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(2);oled.setCursor(0,0);oled.print(rpm,1);oled.println(" rpm");
oled.setTextSize(1);oled.setCursor(0,22);
oled.print(rpm*40.0f/300.0f,1);oled.println(" km/h");
oled.print("LIMIT ");oled.print(limitRpm);oled.println(alarm?" ALARM":" OK");
if (automatic && pending) {
const uint32_t elapsed = millis() - brightAt;
const uint32_t remaining = elapsed < 10000 ? 10000 - elapsed : 0;
oled.print("OFF IN ");
oled.print((remaining + 999) / 1000);
oled.println("s");
} else {
oled.print("LIGHT ADC ");
oled.println(light);
}
oled.print(automatic?"AUTO ":"MANUAL ");oled.println(lamp?"ON":"OFF");
if (settingLimit) {
oled.setCursor(0, 55);
oled.print("SET LIMIT ");
oled.print(pendingLimitRpm);
oled.print(" rpm");
}
oled.display();
}
void setup(){
pinMode(PA1,INPUT_PULLUP);pinMode(PA0,INPUT_ANALOG);
pinMode(PA8,INPUT_PULLUP);pinMode(PB12,INPUT_PULLUP);pinMode(PB13,INPUT_PULLUP);
pinMode(PB0,OUTPUT);pinMode(PB1,OUTPUT);pinMode(PB10,OUTPUT);
analogReadResolution(12);
attachInterrupt(digitalPinToInterrupt(PA1),hall,RISING);
Wire.setSCL(PB6);Wire.setSDA(PB7);Wire.begin();
ready=oled.begin(SSD1306_SWITCHCAPVCC,0x3c);
}
void loop(){
static uint32_t sp=0, lu=0, ds=0;
uint32_t now=millis();
if(mode.pressed(now)){automatic=!automatic;pending=false;}
if(lampKey.pressed(now) && !automatic)lamp=!lamp;
if(now-sp>=50){sp=now;speed();}
updateLimitSetting(now);
if(now-lu>=100){lu=now;lighting(now);}
outputs(now);
if(now-ds>=250){ds=now;displayStatus();}
}
仿真:前灯供电开关
实物:48V电池(最高60V)→ 保险丝 → 12V降压
12V供前照灯;另降至5V供STM32
前照灯开关(仿真)
Loading
ssd1306
ssd1306
Loading
stm32-bluepill
stm32-bluepill