from machine import Pin, PWM, ADC
import time
# ==========================================
# INPUTS
# ==========================================
# Potentiometer for speed
speed_pot = ADC(26)
# Slide switches
low_oil_switch = Pin(2, Pin.IN)
overheat_switch = Pin(3, Pin.IN)
# ==========================================
# OUTPUTS
# ==========================================
# Warning LED
led = Pin(15, Pin.OUT)
# Buzzer
buzzer = PWM(Pin(14))
buzzer.duty_u16(0)
# ==========================================
# BUZZER FUNCTION
# ==========================================
def beep():
buzzer.freq(1200)
buzzer.duty_u16(30000)
time.sleep(0.2)
buzzer.duty_u16(0)
time.sleep(0.15)
# ==========================================
# VOICE GUIDANCE FUNCTION
# ==========================================
def voice_guidance(warning):
print()
if warning == "low_oil":
print("VOICE: Low oil pressure detected.")
print("VOICE: Reduce speed and stop the vehicle safely.")
print("VOICE: Check the engine oil level.")
elif warning == "engine_overheat":
print("VOICE: Engine overheating detected.")
print("VOICE: Reduce your speed.")
print("VOICE: Stop the vehicle safely.")
print("VOICE: Allow the engine to cool.")
print()
# ==========================================
# CAN ENCODING FUNCTION
# ==========================================
def encode_vehicle_data(
speed,
low_oil_pressure=0,
check_engine=0,
brake_system=0,
charging_fault=0,
low_oil_level=0,
abs_fault=0,
low_tire_pressure=0,
airbag=0,
power_steering=0,
stability_control=0,
awd=0,
engine_overheat=0
):
# Byte 0 = Speed
byte0 = speed
# Byte 1 = warning group 1
byte1 = (
(low_oil_pressure << 0) |
(check_engine << 1) |
(brake_system << 2) |
(charging_fault << 3) |
(low_oil_level << 4) |
(abs_fault << 5) |
(low_tire_pressure << 6) |
(airbag << 7)
)
# Byte 2 = warning group 2
byte2 = (
(power_steering << 0) |
(stability_control << 1) |
(awd << 2) |
(engine_overheat << 3)
)
return byte0, byte1, byte2
# ==========================================
# PREVIOUS STATES
# ==========================================
last_low_oil = 0
last_overheat = 0
# ==========================================
# START MESSAGE
# ==========================================
print("================================")
print("VEHICLE SYSTEM STARTED")
print("================================")
# ==========================================
# MAIN LOOP
# ==========================================
while True:
# --------------------------------------
# READ SPEED
# --------------------------------------
raw_speed = speed_pot.read_u16()
# Convert ADC value 0-65535 to 0-255 km/h
speed = int(raw_speed * 255 / 65535)
# --------------------------------------
# READ WARNING SWITCHES
# --------------------------------------
low_oil_pressure = low_oil_switch.value()
engine_overheat = overheat_switch.value()
# --------------------------------------
# CREATE CAN PAYLOAD
# --------------------------------------
can_data = encode_vehicle_data(
speed=speed,
low_oil_pressure=low_oil_pressure,
engine_overheat=engine_overheat
)
# --------------------------------------
# DISPLAY CAN INFORMATION
# --------------------------------------
print()
print("--------------------------------")
print("Speed:", speed, "km/h")
print("CAN ID: 100")
print(
"CAN DATA: {:02X} {:02X} {:02X}".format(
can_data[0],
can_data[1],
can_data[2]
)
)
# --------------------------------------
# WARNING LOGIC
# --------------------------------------
warning_active = False
# LOW OIL PRESSURE
if low_oil_pressure:
print("WARNING: Low Oil Pressure")
warning_active = True
# Only activate when switch changes OFF -> ON
if last_low_oil == 0:
beep()
beep()
voice_guidance("low_oil")
# ENGINE OVERHEAT
if engine_overheat:
print("WARNING: Engine Overheat")
warning_active = True
# Only activate when switch changes OFF -> ON
if last_overheat == 0:
beep()
beep()
voice_guidance("engine_overheat")
# --------------------------------------
# LED CONTROL
# --------------------------------------
if warning_active:
led.value(1)
else:
led.value(0)
# --------------------------------------
# SAVE CURRENT SWITCH STATES
# --------------------------------------
last_low_oil = low_oil_pressure
last_overheat = engine_overheat
# --------------------------------------
# WAIT
# --------------------------------------
time.sleep(1)