Chat Generative Pre-trained Transformer (ChatGPT) is a chatbot by OpenAI.
The project is to create a train control to allow trolleys approaching from opposite directions to pass via a siding.
The hardware consists an Arduino with a Relay shield to provide the outputs and opto isolators to provide the inputs.
The switch controls are wired together so that there are only two relay outputs required to create the two conditions (sw1, sw2). There is one relay output to control power (pwr) to the center rail insulated section to stop the first trolley until the second trolley passes and then allows the first trolley to proceed. There are three insulated outer rail sections for inputs to control the Arduino logic (inp1, inp2, inp3).
Track diagrams are shown below:
It took several tries to determine the correct wording for the logic input for ChatGPT to get the correct Arduino code. The final logic describing the ChatGPT input is as follows:
create an Arduino sketch
comment TOONERVILLE TROLLEY PASSING SIDING ver 1.5
comment David Nissen Oct 31,2023
comment This sketch generated by ChatGPT
three inputs with pullup on pins 4, 5, 6, inp1, inp2, inp3
three outputs on pins 7,8,9, sw1, sw2, pwr
initial all outputs low
comment CONDITION 1 (INPUT 1 FIRST)
if inp1 low
comment 0.5 second output to throw switches to position 1
sw1 high for 0.5 sec then low
comment Wait for input 3
wait for inp3 low
comment 0.5 second output to throw switches to position 2
sw2 high for 0.5 sec then low
comment Wait for 2 seconds then turn on power for 5 seconds
delay 2 sec
pwr high for 5 sec then low
comment Wait for 10 seconds before restarting the process
delay 10 sec
comment CONDITION 2 (INPUT 2 FIRST)
if inp2 low
comment 0.5 second output to throw switches to position 2
sw2 high for 0.5 sec then low
comment Wait for input 3
wait for inp3 low
comment 0.5 second output to throw switches to position 1
sw1 high for 0.5 sec then low
comment Wait for 2 seconds then turn on power for 5 seconds
delay 2 sec
pwr high for 5 sec then low
comment Wait for 10 seconds before restarting the process
delay 10 sec
Chat GPT created the following output Arduino Sketch:
Here's an Arduino sketch based on your requirements. This sketch controls three inputs with pull-up resistors on pins 4, 5, and 6, and three outputs on pins 7, 8, and 9. The outputs are used to control switches (sw1 and sw2) and power (pwr).
// TOONERVILLE TROLLEY PASSING SIDING ver 1.5
// David Nissen Oct 31, 2023
// This sketch generated by ChatGPT
int inp1 = 4;
int inp2 = 5;
int inp3 = 6;
int sw1 = 7;
int sw2 = 8;
int pwr = 9;
void setup() {
pinMode(inp1, INPUT_PULLUP);
pinMode(inp2, INPUT_PULLUP);
pinMode(inp3, INPUT_PULLUP);
pinMode(sw1, OUTPUT);
pinMode(sw2, OUTPUT);
pinMode(pwr, OUTPUT);
// Initial state: All outputs low
digitalWrite(sw1, LOW);
digitalWrite(sw2, LOW);
digitalWrite(pwr, LOW);
}
void loop() {
// CONDITION 1 (INPUT 1 FIRST)
if (digitalRead(inp1) == LOW) {
// 0.5 second output to throw switches to position 1
digitalWrite(sw1, HIGH);
delay(500);
digitalWrite(sw1, LOW);
// Wait for input 3
while (digitalRead(inp3) == HIGH) {
// Wait until inp3 goes low
}
// 0.5 second output to throw switches to position 2
digitalWrite(sw2, HIGH);
delay(500);
digitalWrite(sw2, LOW);
// Wait for 2 seconds
delay(2000);
// Turn on power for 5 seconds
digitalWrite(pwr, HIGH);
delay(5000);
digitalWrite(pwr, LOW);
// Wait for 10 seconds before restarting the process
delay(10000);
}
// CONDITION 2 (INPUT 2 FIRST)
if (digitalRead(inp2) == LOW) {
// 0.5 second output to throw switches to position 2
digitalWrite(sw2, HIGH);
delay(500);
digitalWrite(sw2, LOW);
// Wait for input 3
while (digitalRead(inp3) == HIGH) {
// Wait until inp3 goes low
}
// 0.5 second output to throw switches to position 1
digitalWrite(sw1, HIGH);
delay(500);
digitalWrite(sw1, LOW);
// Wait for 2 seconds
delay(2000);
// Turn on power for 5 seconds
digitalWrite(pwr, HIGH);
delay(5000);
digitalWrite(pwr, LOW);
// Wait for 10 seconds before restarting the process
delay(10000);
}
}
This sketch first sets up the pins and initializes them as specified. Then, it continuously checks the conditions for either INPUT 1 FIRST or INPUT 2 FIRST and performs the required actions accordingly. It uses delays for timing as you described.AI