0% found this document useful (0 votes)
10 views5 pages

Arduino

The document is an Arduino code for controlling vehicle headlights using relays based on input from a push button and ECM signals. It defines pin setups for low and high beams, as well as the main relay outputs, and includes logic for turning the lights on or off based on button presses and ECM input. The code also features a function to turn off all lights when necessary.

Uploaded by

Hardjoko Games
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Arduino

The document is an Arduino code for controlling vehicle headlights using relays based on input from a push button and ECM signals. It defines pin setups for low and high beams, as well as the main relay outputs, and includes logic for turning the lights on or off based on button presses and ECM input. The code also features a function to turn off all lights when necessary.

Uploaded by

Hardjoko Games
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// === Pin Setup ===

#define PIN_LOW A0 // Input dari PC817 low beam (6V ECM)

#define PIN_HIGH A1 // Input dari PC817 high beam (12V ECM)

#define PIN_KONTAK A3 // Input dari PC817 kunci kontak

#define PIN_BUTTON 2 // Tombol push on (pakai internal pullup)

// Relay output

#define RELAY_MAIN 8

#define RELAY_LOW 9

#define RELAY_HIGH 10

#define RELAY_KANAN 11

#define RELAY_KIRI 12

// === Variabel global ===

Int lampMode = 0; // 0 = off, 1 = kanan, 2 = kiri, 3 = kedua

Bool lampActive = false; // status lampu apakah sudah aktif

Unsigned long lastButtonPress = 0;

Bool buttonState, lastButtonState = HIGH;

Void setup() {

// Input

pinMode(PIN_LOW, INPUT);

pinMode(PIN_HIGH, INPUT);

pinMode(PIN_KONTAK, INPUT);

pinMode(PIN_BUTTON, INPUT_PULLUP);

// Output relay

pinMode(RELAY_MAIN, OUTPUT);

pinMode(RELAY_LOW, OUTPUT);
pinMode(RELAY_HIGH, OUTPUT);

pinMode(RELAY_KANAN, OUTPUT);

pinMode(RELAY_KIRI, OUTPUT);

// Semua relay mati saat awal

digitalWrite(RELAY_MAIN, LOW);

digitalWrite(RELAY_LOW, LOW);

digitalWrite(RELAY_HIGH, LOW);

digitalWrite(RELAY_KANAN, LOW);

digitalWrite(RELAY_KIRI, LOW);

Void loop() {

Int kontak = analogRead(PIN_KONTAK);

Int lowIn = analogRead(PIN_LOW);

Int highIn = analogRead(PIN_HIGH);

buttonState = digitalRead(PIN_BUTTON);

// === Cek kondisi kontak ===

If (kontak < 100) {

// Kontak mati, semua lampu off

allLampsOff();

lampActive = false;

return;

// === Cek tombol push on ===

If (buttonState == LOW && lastButtonState == HIGH) {


lastButtonPress = millis(); // mulai hitung waktu tekan

// Jika tombol dilepas

If (buttonState == HIGH && lastButtonState == LOW) {

Unsigned long pressDuration = millis() – lastButtonPress;

If (pressDuration > 2000) {

// Tekan lama >2 detik → matikan semua lampu

allLampsOff();

lampActive = false;

lampMode = 0;

} else {

// Tekan singkat → ganti mode lampu (kanan, kiri, kedua, mati)

lampMode = (lampMode + 1) % 4;

lampActive = true;

lastButtonState = buttonState;

// === Kendali lampu berdasarkan input ECM ===

Bool lowBeam = (lowIn > 100);

Bool highBeam = (highIn > 100);

If (lowBeam || highBeam) {

lampActive = true; // Lampu hidup normal dari ECM

// === Aktifkan relay sesuai kondisi ===


If (lampActive) {

digitalWrite(RELAY_MAIN, HIGH); // Main power ON

// Kendali low / high

digitalWrite(RELAY_LOW, lowBeam ? HIGH : LOW);

digitalWrite(RELAY_HIGH, highBeam ? HIGH : LOW);

// Kendali mode lampu kanan/kiri

If (lampMode == 1) { // Kanan

digitalWrite(RELAY_KANAN, HIGH);

digitalWrite(RELAY_KIRI, LOW);

} else if (lampMode == 2) { // Kiri

digitalWrite(RELAY_KANAN, LOW);

digitalWrite(RELAY_KIRI, HIGH);

} else if (lampMode == 3) { // Keduanya

digitalWrite(RELAY_KANAN, HIGH);

digitalWrite(RELAY_KIRI, HIGH);

} else { // Mati

digitalWrite(RELAY_KANAN, LOW);

digitalWrite(RELAY_KIRI, LOW);

} else {

allLampsOff(); // Proteksi awal, semua mati

// === Fungsi untuk matikan semua lampu ===

Void allLampsOff() {
digitalWrite(RELAY_MAIN, LOW);

digitalWrite(RELAY_LOW, LOW);

digitalWrite(RELAY_HIGH, LOW);

digitalWrite(RELAY_KANAN, LOW);

digitalWrite(RELAY_KIRI, LOW);

You might also like