Give me an idea of how to build simple "Train Accident Prevention Project" using Arduino…
- User
Give me an idea of how to build simple "Train Accident Prevention Project" using Arduino Uno without relay module.I have, 6 LED bulbs 3 Buzzers Jumper wires Arduino Uno 4 wheels Ultrasonic sensor(HC-SRO4) 9V battery. Distance of full rails is 45cm. And I have 6 towers for 3 LEDs and Buzzer and another 3 LEDs.
- Assistant
In this simple "Train Accident Prevention Project" using Arduino Uno, the idea is to alert incoming trains of potential accidents by detecting any obstacles on the tracks using the ultrasonic sensor. We will have three LED indicators set up on the towers, each indicating different alert levels, and buzzers to provide an audible warning for train operators. Here's a step-by-step guide to achieve this using the components you have:
- User
can you give me a arduino code
- Assistant
Here's a simple Arduino code for the Train Accident Prevention Project described earlier:
// Pin definitions const int trigPin = 2; const int echoPin = 3; const int greenLED = 4; const int yellowLED = 5; const int redLED = 6; const int firstBuzzer = 7; const int secondBuzzer = 8; const int thirdBuzzer = 9; // Distance thresholds (in cm) const int safeDistance = 30; const int cautionDistance = 20; // Timing variables unsigned long previousMillis = 0; const long interval = 200; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(greenLED, OUTPUT); pinMode(yellowLED, OUTPUT); pinMode(redLED, OUTPUT); pinMode(firstBuzzer, OUTPUT); pinMode(secondBuzzer, OUTPUT); pinMode(thirdBuzzer, OUTPUT); Serial.begin(9600); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; int distance = measureDistance(); Serial.print("Distance: "); Serial.println(distance); if (distance >= safeDistance) { setWarningLevel(greenLED, false); } else if (distance >= cautionDistance) { setWarningLevel(yellowLED, true); } else { setWarningLevel(redLED, true); } } } int measureDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); int duration = pulseIn(echoPin, HIGH); int distance = duration * 0.034 / 2; return distance; } void setWarningLevel(int activeLED, bool activateBuzzers) { digitalWrite(greenLED, activeLED == greenLED); digitalWrite(yellowLED, activeLED == yellowLED); digitalWrite(redLED, activeLED == redLED); digitalWrite(firstBuzzer, activateBuzzers); digitalWrite(secondBuzzer, activateBuzzers && activeLED == redLED); digitalWrite(thirdBuzzer, activateBuzzers && activeLED == redLED); }