Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Noob Needs Help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Noob Needs Help

by ehearty on Tue Mar 10, 2020 4:41 pm

Hi guys, i'm in high school and trying to finish up the coding for this big school project. Just to give you a quick run down, i'm making a small soccer goal (12" X 20"), and I have a pair of 5mm break beam sensors in the goal post to detect when the ball passes into the goal (the sensors are spread out between the set so the ball cant get through without being detected by one of the sensors). I will than have 3 LED'S mounted on the goal post, and those indicate how many goals have been scored (I.E. 2 LED'S on mean there have been two goals scored). To do this, I have an arduino uno hooked up to a breadboard, with the inputs from the sensors going into pin 4, and than the 3 LED's hooked up to pin 11, 12, and 13. What i'm trying to accomplish is that when the arduino receives a "sensor=Low" from the sensors (meaning the beam was broken), it will turn on ONE led forever, and than when a second goal is scored it will turn on the second LED, but keeping on the first one. I also want to implicate a delay after the sensor is tripped (A.K.A a goal was scored), so that the player has time to take the ball out of the net without the sensor counting that as another goal (maybe a 15 second delay). I'm brand spanking new to coding, and have been teaching myself off YouTube videos and some lessons from Adafruit. The code I have attached below kinda works, in that it turns on all three LEDS when a goal is scored, but I only want it to turn on ONE LED when the sensors detect the break for the first time, and than when a second break is sensed (another goal is scored), it will turn on the second LED, but keeping the first LED on. Any help is greatly appreciated, as I've been trying to do this project myself but can't get my code to work.
Final.ino
The code I've written so far
(1.02 KiB) Downloaded 3 times

ehearty
 
Posts: 8
Joined: Tue Jan 07, 2020 12:49 am

Re: Noob Needs Help

by kcl1s on Tue Mar 10, 2020 7:47 pm

That is a good start.
One way to do this is to have a variable called score. Put this above void setup() int score = 0; then maybe something like this in the loop

Code: Select all | TOGGLE FULL SIZE
sensorState = digitalRead(SENSORPIN);
if (sensorState == LOW) {
  score++;  //increment score by one
}
if (score > 0) {              //at least 1
   digitalWrite(led11, HIGH);
}
if (score > 1) {              //at least 2
   digitalWrite(led12, HIGH);
}
if (score > 2) {              //score is 3
   digitalWrite(led13, HIGH);
}
if (sensorState == LOW) {
  delay(15000);    //if a score wait 15 seconds
}
//Do code to reset score to 0 or
//just use board reset to start over

Hope this helps. Good luck with the project and post a photo of the finished project.

Fellow hobbyist
Keith

kcl1s
 
Posts: 1511
Joined: Tue Aug 30, 2016 12:06 pm

Re: Noob Needs Help

by ehearty on Wed Mar 11, 2020 1:33 am

Hey Keith, thank you so much for the help! Never even knew a scoring function like that existed, albeit I've only been coding for 2 days. Thank you so much for the help, I will definitely being using it. I'm gonna add so more things in, such as making all three LED's flash when you score, but I should/ want to figure that out myself. Thank so so much again, appreciate what you do for me and other people on the forums.
-Ethan

ehearty
 
Posts: 8
Joined: Tue Jan 07, 2020 12:49 am

Re: Noob Needs Help

by kcl1s on Wed Mar 11, 2020 6:12 am

Score is just a variable, a place to store a value. We could have called it anything but coders try to make variables describe the data they hold. When we say score++ we are just telling the code to add 1 to the value of score.

All code languages give us a few building blocks to work with. If is the coders job to put these together to accomplish a task. Here are the basic blocks.
Variables - a place to store a bit of data
Math - calculations that usually get stored in a variable
Statements - tell the code to do something (digitalWrite, delay)
Conditionals - have the code make a true false decision (if)
Loops - Computers are good at repetitive tasks. (for loops) do whats between their curly brackets a set number of times, (while loops) do their code as long as a condition is true.

Coders use these block to do every thing from blinking a LED to sending a rocket into space. It is just the way you put them together.

Keith

kcl1s
 
Posts: 1511
Joined: Tue Aug 30, 2016 12:06 pm

Please be positive and constructive with your questions and comments.