Vehicle indicator control system

Vehicle indicator control system

Main Diagram

HARDWARE REQUIRED:

  • PICUNO Microcontroller board
  • 2 × LEDs
  • 2 × 220Ω resistors
  • 2 × Push Button (4-pin or 2-pin)
  • 2 × 10kΩ resistors
  • Breadboard
  • Jumper wires
  • USB cable

DESCRIPTION:

This project simulates the full indicator and hazard light logic of a vehicle.

- Pressing and holding the left button for 1 second toggles the left indicator.

- Pressing and holding the right button for 1 second toggles the right indicator.

- If both buttons are held simultaneously for 1 second → hazard lights are turned ON.

- Pressing and holding any one button (left or right) for 1 second will deactivate hazard mode.

- All active indicator states blink ON-OFF every 300 ms.

CIRCUIT DIAGRAM:

[Fritzing image to be added here]

  • Connect the PICUNO board to the computer using a USB cable.
  • Connect the anode (longer leg) of the left LED and right LED to GPIO 8 and 9 respectively through a 220Ω resistor.
  • Connect the cathodes (short leg) of the LED to GND.
  • Connect one leg of the both push buttons to 3.3 V.
  • Connect the other leg of the Left button and Right button to GPIO 6 and 7 respectively.
  • Connect one end of the 10kΩ resistors to GPIO 6 and 7 respectively and other end to GND.

SCHEMATIC:

Left Indicator LED: Anode → 220Ω → GPIO 8 → Cathode → GND

Right Indicator LED: Anode → 220Ω → GPIO 9 → Cathode → GND

Left Button: One side → 3.3V, other side → GPIO 6 → 10kΩ → GND

Right Button: One side → 3.3V, other side → GPIO 7 → 10kΩ → GND

CODE -- C:

#include <Arduino.h>

const int LEFT_LED = 8;
const int RIGHT_LED = 9;
const int LEFT_BTN = 6;
const int RIGHT_BTN = 7;

bool left_on = false;
bool right_on = false;
bool hazard_on = false;
unsigned long blink_time = 0;
bool led_state = false;
unsigned long hold_left = 0;
unsigned long hold_right = 0;

void setup() {
  pinMode(LEFT_LED, OUTPUT);
  pinMode(RIGHT_LED, OUTPUT);
  pinMode(LEFT_BTN, INPUT);
  pinMode(RIGHT_BTN, INPUT);
}

void loop() {
  unsigned long now = millis();
  bool left_pressed = digitalRead(LEFT_BTN);
  bool right_pressed = digitalRead(RIGHT_BTN);

  // Track hold time
  if (left_pressed) {
    if (hold_left == 0) hold_left = now;
  } else {
    hold_left = 0;
  }

  if (right_pressed) {
    if (hold_right == 0) hold_right = now;
  } else {
    hold_right = 0;
  }

  // Activate hazard if both held for 1 second
  if (left_pressed && right_pressed &&
      hold_left > 0 && hold_right > 0 &&
      (now - hold_left >= 1000) && (now - hold_right >= 1000)) {
    hazard_on = true;
    left_on = false;
    right_on = false;
    hold_left = 0;
    hold_right = 0;
    delay(300);
  }

  // Deactivate hazard if either button held for 1 second
  else if (hazard_on) {
    if (left_pressed && hold_left > 0 && (now - hold_left >= 1000)) {
      hazard_on = false;
      hold_left = 0;
      delay(300);
    } else if (right_pressed && hold_right > 0 && (now - hold_right >= 1000)) {
      hazard_on = false;
      hold_right = 0;
      delay(300);
    }
  }

  // Toggle left indicator
  else if (left_pressed && hold_left > 0 && (now - hold_left >= 1000)) {
    if (right_on) {
      right_on = false;
      left_on = true;
    } else {
      left_on = !left_on;
    }
    hold_left = 0;
    delay(300);
  }

  // Toggle right indicator
  else if (right_pressed && hold_right > 0 && (now - hold_right >= 1000)) {
    if (left_on) {
      left_on = false;
      right_on = true;
    } else {
      right_on = !right_on;
    }
    hold_right = 0;
    delay(300);
  }

  // Blinking logic every 300 ms
  if (now - blink_time >= 300) {
    blink_time = now;
    led_state = !led_state;
    if (hazard_on) {
      digitalWrite(LEFT_LED, led_state);
      digitalWrite(RIGHT_LED, led_state);
    } else {
      digitalWrite(LEFT_LED, (left_on ? led_state : LOW));
      digitalWrite(RIGHT_LED, (right_on ? led_state : LOW));
    }
  }

  delay(20); // Equivalent of time.sleep(0.01)
}
unsigned long blink_time, hold_left, hold_right - Variables used to track time for blinking intervals and how long each button has been held down.

setup() - Initializes the GPIO pins --- buttons as inputs, and LEDs as outputs.

millis() is called to get the current time in milliseconds.

digitalRead() is used to check if a button is currently pressed.

CODE -- PYTHON:

from machine import Pin
import time

left_led = Pin(8, Pin.OUT)
right_led = Pin(9, Pin.OUT)
left_btn = Pin(6, Pin.IN)
right_btn = Pin(7, Pin.IN)

left_on = False
right_on = False
hazard_on = False
blink_time = 0
led_state = False
hold_left = None
hold_right = None

while True:
    now = time.ticks_ms()
    left_pressed = left_btn.value()
    right_pressed = right_btn.value()

    # Track hold time
    if left_pressed:
        if hold_left is None:
            hold_left = now
    else:
        hold_left = None

    if right_pressed:
        if hold_right is None:
            hold_right = now
    else:
        hold_right = None

    # Activate hazard if both held for 1 second
    if (left_pressed and right_pressed and hold_left and hold_right and
        now - hold_left >= 1000 and now - hold_right >= 1000):
        hazard_on = True
        left_on = False
        right_on = False
        hold_left = None
        hold_right = None
        time.sleep(0.3)

    # Deactivate hazard if either button held for 1 second
    elif hazard_on:
        if left_pressed and hold_left and now - hold_left >= 1000:
            hazard_on = False
            hold_left = None
            time.sleep(0.3)
        elif right_pressed and hold_right and now - hold_right >= 1000:
            hazard_on = False
            hold_right = None
            time.sleep(0.3)

    # Toggle left indicator
    elif left_pressed and hold_left and now - hold_left >= 1000:
        if right_on:
            right_on = False
            left_on = True
        else:
            left_on = not left_on
        hold_left = None
        time.sleep(0.3)

    # Toggle right indicator
    elif right_pressed and hold_right and now - hold_right >= 1000:
        if left_on:
            left_on = False
            right_on = True
        else:
            right_on = not right_on
        hold_right = None
        time.sleep(0.3)

    # Blinking logic every 300 ms
    if now - blink_time >= 300:
        blink_time = now
        led_state = not led_state
        if hazard_on:
            left_led.value(led_state)
            right_led.value(led_state)
        else:
            left_led.value(led_state if left_on else 0)
            right_led.value(led_state if right_on else 0)

    time.sleep(0.1)
blink_time and led_state - Used for LED blinking at regular intervals.

hold_left, hold_right - Store timestamps for when each button is pressed.

left_btn.value(), right_btn.value() - Read the state of each button.

if hold_* is None - Record the time when a button is first pressed.

now - hold_* >= 1000 - Check if the button has been held for 1 second.