Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1171

Help using the TOF10120 sensors to trigger my model railroad crossing

$
0
0

So I think I know how to wire this up, just not sure where to put the TOF10120 pins in this sketch. I want to use these sensors to trigger the sketch

Here is what I got off of Google code generator thing...

Any help would be greatly appreciated. Thank you in advance.

Certainly! Below is an Arduino code that uses two TOF10120 sensors to control a model railroad crossing gate with an SG90 servo motor and two flashing LEDs. The LEDs will flash when the first sensor is triggered, and the gate will lower after 3 seconds. The LEDs will continue to flash until the second sensor is triggered, after which the gate will rise after a 30-second delay, and the LEDs will stop flashing.

#include <Wire.h>
#include <Servo.h>

#define TOF1_ADDR 0x52 // Address of the first TOF10120 sensor
#define TOF2_ADDR 0x53 // Address of the second TOF10120 sensor
#define LED1_PIN 8
#define LED2_PIN 9
#define SERVO_PIN 10

Servo gateServo;
bool gateLowered = false;

void setup() {
  Wire.begin();
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  gateServo.attach(SERVO_PIN);
  gateServo.write(90); // Initial position (gate up)
}

void loop() {
  if (isSensorTriggered(TOF1_ADDR)) {
    flashLEDs(3000); // Flash LEDs for 3 seconds
    gateServo.write(0); // Lower the gate
    gateLowered = true;
    while (!isSensorTriggered(TOF2_ADDR)) {
      flashLEDs(500); // Continue flashing LEDs
    }
    delay(30000); // Wait 30 seconds
    gateServo.write(90); // Raise the gate
    gateLowered = false;
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, LOW);
  }
}

bool isSensorTriggered(uint8_t sensorAddr) {
  Wire.beginTransmission(sensorAddr);
  Wire.write(0x00); // Command to read distance
  Wire.endTransmission();
  Wire.requestFrom(sensorAddr, 2);
  if (Wire.available() == 2) {
    int distance = Wire.read() << 8 | Wire.read();
    return distance < 100; // Adjust threshold as needed
  }
  return false;
}

void flashLEDs(int duration) {
  unsigned long startTime = millis();
  while (millis() - startTime < duration) {
    digitalWrite(LED1_PIN, HIGH);
    digitalWrite(LED2_PIN, HIGH);
    delay(250);
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, LOW);
    delay(250);
  }
}

Explanation:

  1. Setup:
  • Initialize the I2C communication for the TOF10120 sensors.
  • Set the LED pins as outputs.
  • Attach the servo motor to the specified pin and set its initial position (gate up).
  1. Loop:
  • Continuously check if the first sensor is triggered.
  • If triggered, flash the LEDs for 3 seconds and then lower the gate.
  • Continue flashing the LEDs until the second sensor is triggered.
  • Once the second sensor is triggered, wait for 30 seconds, then raise the gate and stop flashing the LEDs.
  1. Helper Functions:
  • isSensorTriggered: Reads the distance from the specified TOF10120 sensor and checks if it is below a certain threshold.
  • flashLEDs: Flashes the LEDs for a specified duration.

Feel free to adjust the sensor addresses, pin numbers, and distance thresholds as needed for your specific setup.

4 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 1171

Latest Images

Trending Articles



Latest Images