High School Coding Project

Hello all,

I'm trying to help my son with his project for school, but the coding bit is a little lost on me.

He needs to make a toll gate arm that will open automatically, but can be overridden by a manual switch. It also needs to have a red light when the arm is closed, a green light when it opens fully, and a kill switch.

I found projects online that closely resemble this one, so I figured I could use the code for those and add in the missing components (like the kill switch). The problem I'm having right now is getting even the base code moved over to the Arduino. I get an error message saying "redefinition of 'void setup()'". I can't figure out how to fix this issue, as the solutions I have found online don't seem to be matching my issue.

I'm using the following components:
Arduino Uno
Servo Motor
Ultrasonic Sensor
Red and Green LEDs
Breadboard

I need to figure out how to add two four pronged button as well. One for the kill switch and one for the override.

I have included the code below.

Any help would be amazing.

#include <Servo.h>

Servo myservo;

int pos = 0;

int cm = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)

{

pinMode(triggerPin, OUTPUT);

digitalWrite(triggerPin, LOW);

delayMicroseconds(2);

digitalWrite(triggerPin, HIGH);

delayMicroseconds(10);

digitalWrite(triggerPin, LOW);

pinMode(echoPin, INPUT);

return pulseIn(echoPin, HIGH);

}

void setup() {

digitalWrite(12,LOW);

myservo.attach(9);

Serial.begin(9600);

}

void loop() {

cm = 0.01723 * readUltrasonicDistance(6, 7);

if(cm<30){

Serial.print(cm);

Serial.println("cm");

for (pos = 0; pos <= 120; pos += 1) {

myservo.write(pos);

delay(15);

}

delay(500);

for (pos = 120; pos >= 0; pos -= 1) {

myservo.write(pos);

delay(15);

}

delay(5000); //add delay how much you want

}

}

If you connect the wires diagonally, orientation doesn't matter. When you set the pins, use INPUT_PULLUP and connect the other end to GND. In the code, look for a LOW state = button pressed. Remember that buttons need debounce.

Most likely, you have ‘extra’ old or unwanted c, cpp or ino files in the project folder.

When he graduates will you do his work as well?
Give him access to a computer, teach him how to use Google, and stand back.

3 Likes

Hi @blind-ram ,

Welcome to the forum..

That error has nothing to do with the code you posted and like already stated the folder where this ino resides must have other files in there, should only just be this one ino file, unless of course you are making a program that contains multiple file.

Your code compiles and does seem to work..
Your project in a simulator..

you need to lose the use of the delay function in order to add your buttons..

good luck.. ~q

I get the feeling that programming may not be part of your background, and that’s perfectly okay. However, without that experience, trying to guide him too closely might unintentionally slow down his learning process.

The best thing you can do is give him access to a computer with Firefox (or any browser) and let him explore. This is his starting point, and like any meaningful skill, he needs to learn the fundamentals on his own. Otherwise, he may struggle repeatedly or remain dependent on help.

Think back to when he first started crawling — long before he could run. Learning to program is no different. Encourage him to take those first small steps independently; it will build his confidence and competence over time.

1 Like

You (and child) can use an online simulator to learn without needing hardware.
Try Wokwi.com can with your program and the hardware.
A good resource for code to learn about a device is the "examples" folder in every library.
Realize that 100% of computer failures are human caused, and all will be well.