GP2Y0A21YK0F Sharp IR Detector Question

Hello, I am building model railroad crossing gates, and have a working prototype. My Question is this, Will the GP2Y0A21YK0F Sharp IR Analog Distance Sensor work with the same code as the HiLetgo 10pcs IR Infrared Obstacle Avoidance Sensor? It seems there are three wires, one Ground, VCC and I believe the other would be the wire going to the AO pin on the UNO.

// Example Level Crossing Control Driving Common Cathode LEDs
// Version 1.4  Geoff Bunza 2018
//
#include <Servo.h>
Servo gate1servo;  // create servo object to control crossing gate 1
                   //ASSUMPTION Gate is down at position 30 and up at position 120
Servo gate2servo;  // create servo object to control crossing gate 2
                   //ASSUMPTION Gate is down at position 30 and up at position 120
int sensor1 = 5;   // IR sensor pin Assumption== Pin goes LOW when train detected
int sensor2 = 6;   // IR sensor pin Assumption== Pin goes LOW when train detected
int led1 = 9;      // Led 1 pin first alternating flasher
int led2 = 10;      // Led 2 pin first alternating flasher
int led3 = 11;      // Led 3 pin second alternating flasher
int led4 = 12;     // Led 4 pin second alternating flasher
int led5 = 8;       // Led 5 is for the light at the end of the crossing arm
int gatelow = 5;        // variable to store the servo low gate stop position
int gatehigh = 110;    // variable to store the servo high gate stop position
int gateposition = 110;    // variable to store the servo gateposition
int entering_sensor = 5;       //this will tell which sensor was triggered first
int leaving_sensor = 6;        // this will tell which sensor shows train leaving
int gates_started = 0;         // this says if the crossing is active
int flash_state = 0;
long flash_time = 0;
long  flash_interval = 900;    // time in milliseconds between alternating flashes
int sensor_count = 0;
void setup()
{
  gate1servo.attach(3);  // attaches the servo on pin 3 to the servo object
  gate2servo.attach(4);  // attaches the servo on pin 4 to the servo object
  gate1servo.write(gateposition);  //start assuming no train
  gate2servo.write(gateposition);  //start assuming no train
  pinMode(sensor1, INPUT);  
  pinMode(sensor2, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  digitalWrite(led1, LOW);  // Start with all flashers off
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  flash_time = millis();
}
void loop()
{
  if ((digitalRead (sensor1)==LOW)&& (gates_started==0)) {
    gates_started = 1;
    leaving_sensor = sensor2;
    starting_sequence();
  }
  if ((digitalRead (sensor2)==LOW)&& (gates_started==0)) {
    gates_started = 1;
    leaving_sensor = sensor1;
    starting_sequence();
  }
  if (gates_started) flash_leds();   //gates are down continue flashing
  if ((digitalRead(leaving_sensor)==LOW)&&(gates_started==1)) {   //train is starting to leave
   
  //as long as the leaving sensor is active the train is still in the crossing
  while (gates_started==1)  {   //now check if train is REALLY gone
    sensor_count = 0;
    for (int i=1; i< 40; i++)  {
      if (digitalRead(leaving_sensor)==LOW) sensor_count++;
          delay (30);
                  flash_leds();
                  }
    if (sensor_count==0) gates_started=0;
    flash_leds();
    }
    // we only get here if the train has really left the crossing
                ending_sequence();
  }
}
void starting_sequence()  {
  long wait_time;
  flash_time = millis();
  wait_time = millis()+3000;
  while (wait_time> millis())  flash_leds();  //flash before dropping gates
  for(gateposition = gatehigh; gateposition> gatelow; gateposition-=1)  // goes from gatehigh degrees to gatelow degrees
  {                               
    gate1servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    gate2servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    flash_leds();                    // keep flashing leds
    delay(70);                       // waits 40ms to slow servo
  }
}
void ending_sequence()  {
    for(gateposition = gatelow; gateposition< gatehigh; gateposition++)   // goes from gatelow degrees to gatehigh degrees
  {                               
    gate1servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    gate2servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    flash_leds();                    // keep flashing leds
    delay(70);                       // waits 40ms to slow servo
  }
  digitalWrite(led1, LOW);  //  flashers completely off
  digitalWrite(led3, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led4, LOW); 
  digitalWrite(led5, LOW);
  delay(30000);             // 30 second delay to account for the train passing the starting entry sensor
}
void flash_leds()  {
  if (flash_time> millis()) return;
  flash_state = ~flash_state;
  digitalWrite(led1, flash_state);  // Alternate flashers
  digitalWrite(led3, flash_state);
  digitalWrite(led2, ~flash_state);
  digitalWrite(led4, ~flash_state);
  digitalWrite(led5, 1);
  flash_time = millis()+flash_interval;
}

Posting links to the datasheets would likely help. Else You can sit, wait and hope for a helper knowing those exact sensors.

Here is a code generated by AI. I think I understand how to set it up, except for the
#define TOF1_ADDR 0x52
#define TOF2_ADDR 0x53

Which pin would I attach the wires to on the Nano?

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:

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).

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.

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.

I have the code written for my crossing gate project that works fine with the regular cheap IR sensors... just need to figure out where the pins are attached. Thank you for your help

Why not ask AI how it is thinking?