Reverse loop for model railway`s

hi all,
Luke here,

new to all of this programming,

I am trying to set up my Audino to use an automatic switch, a switch, on my layout.
i am using standard insulfrog switch.
I am using a mega 2560 with l298p motor shield.
also using 2 infra-red sensors with breadboard and a DR60030 servo.
I have tried to configure it with ai sketches and i till cannot get it to work.
is there someone able to help me with this project and the correct way of putting it together.
Please email me.
I have all pos and negatives in the correct places,
I have servo on digital pin 9
I have ir sensor on digital pins 3 and 4
and it still not working correctly can anyone help please.

and the code that i am using is below.

#include <Servo.h>

// Constants for infrared sensor pins
const int IR_SENSOR_1_PIN = 2;
const int IR_SENSOR_2_PIN = 3;

// Constants for servo control
const int SERVO_PIN = 4;
const int SERVO_ANGLE_CLOSED = 0;
const int SERVO_ANGLE_OPEN = 90;

// Variables to store infrared sensor states
int irSensor1State = 0;
int irSensor2State = 0;

// Initialize servo object
Servo trackSwitch;

void setup() {
// Initialize infrared sensor pins as inputs
pinMode(IR_SENSOR_1_PIN, INPUT);
pinMode(IR_SENSOR_2_PIN, INPUT);

// Initialize servo pin
trackSwitch.attach(SERVO_PIN);

// Set initial position of the track switch
trackSwitch.write(SERVO_ANGLE_CLOSED);
}

void loop() {
// Read the state of infrared sensors
irSensor1State = digitalRead(IR_SENSOR_1_PIN);
irSensor2State = digitalRead(IR_SENSOR_2_PIN);

if (irSensor1State == HIGH && irSensor2State == HIGH) {
// Both sensors detect a train, switch the track
trackSwitch.write(SERVO_ANGLE_OPEN);
delay(1000); // Adjust the delay time to ensure the servo has enough time to switch the track
} else {
// No train detected, close the track
trackSwitch.write(SERVO_ANGLE_CLOSED);
delay(1000); // Adjust the delay time to ensure the servo has enough time to switch the track
}
}

Not the way that it works here. Here, you should post your code (if you have it), a schematic or circuit diagram, tell us what the code does and what the code is supposed to do and you can get help here. That way others may benefit.

That is the purpose of the forum and the many volunteers that are here to answer questions. Give us the information that we need and we will go out of our way to try to get your project to work. It will be a group effort.

Please read the how to get the most from the forum post. The post contains information on what we need to know in order to help you.

Oh, and the AI code writers do not have a stellar reputation here at this time.
With a better understanding of your project we can point you to tutorials and such so that you can write the code with our help (if needed).

I moved your topic to an appropriate forum category @ash37.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

#include <Servo.h>

// Define IR sensor pins
#define IR_SENSOR1_PIN A0
#define IR_SENSOR2_PIN A1

// Define servo pin
#define SERVO_PIN 9

// Define relay module pins
#define RELAY_IN1_PIN 10
#define RELAY_IN2_PIN 11

// Create servo instance
Servo servo;

void setup() {
// Set relay module pin modes
pinMode(RELAY_IN1_PIN, OUTPUT);
pinMode(RELAY_IN2_PIN, OUTPUT);

// Attach servo to pin
servo.attach(SERVO_PIN);

// Initialize Serial communication if needed
Serial.begin(9600);
}

void loop() {
// Read IR sensor values
int irSensor1Value = analogRead(IR_SENSOR1_PIN);
int irSensor2Value = analogRead(IR_SENSOR2_PIN);

// Check if obstacles are detected by IR sensors
if (irSensor1Value < 100 || irSensor2Value < 100) {
// Activate relay module to reverse the train
digitalWrite(RELAY_IN1_PIN, HIGH);
digitalWrite(RELAY_IN2_PIN, LOW);

// Rotate servo to change the railway direction
servo.write(45);

} else {
// Deactivate relay module to allow the train to proceed forward
digitalWrite(RELAY_IN1_PIN, LOW);
digitalWrite(RELAY_IN2_PIN, LOW);

// Rotate servo to set the railway straight
servo.write(0);

}

// Print IR sensor values to Serial monitor
Serial.print("IR Sensor 1: ");
Serial.print(irSensor1Value);
Serial.print(" | IR Sensor 2: ");
Serial.println(irSensor2Value);

// Add a delay if needed to control the loop frequency
delay(100);

this is the code that i am using

Now a track plan with all the information is still missing.

Pin 9 and pin11 is already used as Brake_A and PWM_B for that L298 motor shield.
So are A0 and A1, and maybe more.

If you have a Mega, then use pins that are not covered by the shield.

How do you power the (unspecified) servo. The Mega is not a power supply.
Leo..

servo is in digital pin 9
and the relay is in digital pins 10 and 11
ther servo is powerd on the bread board in which the breadboard is powered by the pins 5v and ground

Your code is incomplete and still inserted incorrectly.
Please return and edit your message, insert whole code, using the code tags.

By 5v and GND pins of Mega? And how powered the Mega itself?

hi,
may i ask what do you mean as i am not a programmer the code was automatically generated by the ai program for me

My standard advice:

Keep asking the robot until he/she/it gives you the correct result you expect.

You should also ask for a connection, wiring and track diagram.

One answer could probably be 42.

Your code has incorrect syntax, it breaks abruptly and cannot be compiled.

I doubt that any of the programmers will want to fix the code generated by the AI. I think you need to contact the robot again and ask to correct the mistakes

2 Likes

Hi, @ash37
To add code please click this link;

Please add it in a NEW post, don't go back and edit old posts.

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

hi tom,
i am new to all of this and trying to wrap my head around what everything means, the code you see was generated by the ai system

luke

hi paul,
how many sketches would i need in the mega for one to work.

The code you posted is incomplete and missing code tags.

I'm not sure what you mean, but the possible number of states representable by the flash memory of a Mega comfortably exceeds the number of atoms in the observable universe.

2 Likes

Sorry, I am not talking about your code, but how it is presented in your post.
Code tags are for the forum. All you have to do is highlight the code and click on the </> at the top of the post dialog .

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Do you actually have the hardware?
If so, then you would have seen that pin 9, 10, 11, A0, A1 are completely covered by that shield.
So why didn't you ask how to connect all the parts.
As said, use different pins (hardware and code) that are not covered by the shield.

Maybe you should start with a few simple projects. Flash an LED, use a button, etc. There are basic examples for that in the IDE. Thinks become a lot easier when you understand the basics.

Using AI with no previous experience, to see the blunders, will lead to frustration.
Currently Ai has the knowledge of a highschool student and the experience of a 7 year old.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.