Basket Arcade Project

Hi everyone.
I state that I am new and it is the first time that I try myself in this world of arduino

I found a project on the internet to create an arcade basketball game.
The object is to make as many baskets in a set amount of time as possible. You then get tickets or bragging rights based on your score.
the project should go like this:

Code Flow

  • Wait in loop until start button is pressed. An ambient light cycle will run on the RGB while waiting.
  • Start button will set gameActive variable to true, which runs the game functions and begins the timer.
  • The ultrasonic sensor will read the distance within each loop iteration to detect a ball within 10 cm.
  • If a ball is present, the score is incremented, the LED changes to green and the display is updated.
  • If 30 points have been reached in this session, an additional 30 seconds will be added to the game time. It will repeat the cycle if an additional 30 points has been scored in the extended time.
  • A session counter will be updated and displayed.
  • When the game ends, the score will be compared against the existing high score. If it is exceeded, the top score will be updated and celebration function will run to update the lights and displays.

this does not happen however.
When I press the reset button on the displays the score is correctly 0 but when I press the start button the displays turn off and nothing happens

I am attaching my project and I hope that, thanks to your experience, I will be able to build this project

#include <elapsedMillis.h>

#include <LedControl.h>

#include <Ultrasonic.h>

// User Defined Variables
int countdown = 30*1000; // 30 seconds
const long goalDelay = 500; // Adujust the Goal LED Delay in ms
int bonus = 30; // 30 points required for bonus time (clock reset) 


// Define Hardware
/*
Seven Segment Display
 Pin 8-4 is connected to the DataIn 
 Pin 9-5 is connected to the CLK 
 Pin 7-6 is connected to LOAD 
 */
LedControl lc=LedControl(8,9,7,1);
LedControl lc2=LedControl(4,5,6,1);

// Start Button
int startButtonPin = 12;        // PIN 12

// Distance Sensor
Ultrasonic ultrasonic(10, 13);  // PIN 10, 13

// RGB LED 
const int ledRedPin = 2;          // PIN 2
const int ledGreenPin = 3;          // PIN 3
const int ledBluePin = 11;         // PIN 11


// Segement selector (helper function used to create sections)
int topRightDisplay = 0;
int topLeftDisplay = 1;
int btmRightDisplay = 2;
int btmLeftDisplay = 3;

// Define initial LED states
int ledRedState = HIGH; 
int ledGreenState = LOW;
int ledBlueState = HIGH;

// Global Variables
int score = 0;
int highScore = 0;
int remaining = 0;
int topScore = 0;
unsigned long previousMillis = 0; // Timer reference - score
elapsedMillis sinceStart; // start of game
elapsedMillis statsMillis; // main loop 

// Number of overtime bonus sessions
int bonusCount = 0;
int lastScore = 0;

// State of game
boolean gameActive = false;

// Detect if ball has had a chance to clear rim
boolean deDuplicate = true;



// Helper Functions

void goal() {
  score += 2;
}

// Set Game Defaults
void resetGame(){

    // reset game variables
    score = 0;
    lastScore = 0;
    sinceStart = 0;

    // reset seven segment displays
    lc.clearDisplay(0);
    lc2.clearDisplay(0);
    displays(topLeftDisplay,topScore);
    displays(topRightDisplay,score);

    // reset RGB LED
    digitalWrite(ledGreenPin, false);
    digitalWrite(ledRedPin, false);
    digitalWrite(ledBluePin, true);
    
    // Spell "P0P" btmRightDislay
    lc2.setChar(0,7,'1',false);
    lc2.setChar(0,6,'2',false);
    lc2.setChar(0,5,'3',false);
    lc2.setChar(0,4,'4',true);
    lc2.setChar(0,3,'5',false);
    lc2.setChar(0,2,'6',false);
    lc2.setChar(0,1,'7',false);
    lc2.setChar(0,0,'8',true);
}


// Print stats to console
void stats(){
    Serial.print(" | Top Score");
    Serial.println(topScore);
    Serial.print(" | Score");
    Serial.println(score);
    Serial.print(" | Time Remaining: ");
    Serial.println(remaining);
}

// Formats and prints to Seven Segment displays consitently
void displays(int section,int number){
  uint8_t ones,tens,hundreds,thousands;
  thousands=number/1000;
  hundreds=number%1000/100;
  tens=number%100/10;
  ones=number%10;
  // set digits on 7 segment. Some digits are disabled (' ') for aesthetic reasons
  if(section == 0){                   // Top Right
    lc.setDigit(0,0,ones,false);
    lc.setDigit(0,1,tens,false);
    lc.setDigit(0,2,hundreds,false);
    //lc.setDigit(0,3,thousands,false); 
    lc.setChar(0,3,' ',false);
  }else if(section == 1){             // Top Left
    lc.setChar(0,4,' ',false);
    lc.setDigit(0,5,ones,false);
    lc.setDigit(0,6,tens,false);
    lc.setDigit(0,7,hundreds,false);
    //lc.setDigit(0,7,thousands,false);  
  }else if(section == 2){             // Bottom Right
    lc2.setDigit(0,0,ones,false);
    lc2.setDigit(0,1,tens,false);
    //lc2.setDigit(0,2,hundreds,false);
    //lc2.setDigit(0,3,thousands,false);
    lc2.setChar(0,2,' ',false);
    lc2.setChar(0,3,' ',false);
  } else if(section == 3){             // Bottom Left
    lc2.setDigit(0,4,ones,false);
    lc2.setDigit(0,5,tens,false);
    //lc2.setDigit(0,6,false,false);
    //lc2.setDigit(0,7,false,false);
    lc2.setChar(0,6,' ',false);
    lc2.setChar(0,7,' ',false);
  }
  
}

// Top Score Celebration Routing
void finishTopScore(){
    Serial.print("NEW TOP SCORE !");
    displays(topLeftDisplay,topScore);
    int y = false;
    for (int x = 0; x < 1024; x = x + 20){
      for (int y = 100; y < 1024; y++){
        analogWrite(ledGreenPin, x);
        analogWrite(ledRedPin, y);
      }
       // Spell "P0P"
      lc2.setChar(0,3,' ',false);
      lc2.setChar(0,2,'P',false);
      lc2.setChar(0,1,0,false);
      lc2.setChar(0,0,'P',true);
      delay(50);
      lc2.clearDisplay(0);       
    }
}

// display final messages and LED routines
void finishLowScore(){
  for (int x = 0; x <6; x++){
      lc2.setChar(0,7,false,false);
      lc2.setChar(0,6,false,false);
      lc2.setChar(0,5,'-',false);
      lc2.setChar(0,4,'-',false);
      displays(topRightDisplay,score);
      digitalWrite(ledRedPin, true);
      delay(500);
      displays(topRightDisplay,0);
      lc2.setChar(0,7,false,false);
      lc2.setChar(0,6,false,false);
      lc2.setChar(0,5,'0',false);
      lc2.setChar(0,4,'0',false);
      digitalWrite(ledRedPin, false);
      digitalWrite(ledGreenPin, false);
      digitalWrite(ledBluePin, false);
      delay(500);
    }
  delay(50);
  lc2.clearDisplay(0);  
}

elapsedMillis standbyRoutineMillis;
boolean blueCountUp = true;
void standbyRoutine(){

  if (standbyRoutineMillis > 50){

    // Fade Blue in and out
    if(blueCountUp){
      if (ledBlueState < 800){
        analogWrite(ledBluePin, ledBlueState++);    
      }else{
        blueCountUp = false; 
      }
    }else{
        if (ledBlueState > 0){
          analogWrite(ledBluePin, ledBlueState--);    
        }else{
          blueCountUp = true;
        }
    }

    // reset timer
    standbyRoutineMillis = 0;
  }
}


void runGame(){
  // Determine remaining game time
  remaining = (countdown - sinceStart);

  // Display Countdown Clock
  displays(btmLeftDisplay,remaining);
  

  // Check for ball, with delay to avoid duplicates
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= goalDelay) {   
    ledRedState = HIGH;
    ledGreenState = LOW;
    ledBlueState = HIGH;
    digitalWrite(ledGreenPin, ledGreenState);
    digitalWrite(ledRedPin, ledRedState);
    digitalWrite(ledBluePin, ledBlueState);
    Serial.print("distance ");
    Serial.println(ultrasonic.read());

    // Detect Ball
    if (ultrasonic.read() < 10 && deDuplicate){
      deDuplicate = false;  
      // save the last time a goal happened
      previousMillis = currentMillis;
      Serial.print("Ball Detected!");
      score += 2;
      Serial.print("Score: ");
      Serial.println(score);
      ledGreenState = HIGH;
      ledRedState = LOW;
      ledBlueState = LOW;

      // set the LED with the ledState of the variable:
      digitalWrite(ledGreenPin, ledGreenState);
      digitalWrite(ledRedPin, ledRedState);
      digitalWrite(ledBluePin, ledBlueState);
    }else{
      deDuplicate = true;
    }

  // End game loop
  if(remaining < 1){
    // Check if bonus time is added
    if(score-lastScore >= bonus){
      bonusCount++;
      displays(btmRightDisplay,bonusCount);
      lastScore = score;   
      // add extended bonus time by resetting start counter
      sinceStart = 0;
    }else{
          // End Game
          if (score > topScore){
            topScore = score;
            finishTopScore();      
          } else{
            finishLowScore();     
          }
          resetGame(); 
          gameActive = false;                      
    }
  } 
  // write score to 7 Segment
  displays(topRightDisplay,score);  
  }

}





// Run ONCE
void setup() {
  Serial.begin(57600);
  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(ledBluePin, OUTPUT);
  pinMode(startButtonPin, INPUT);

  // LED 7 Segment logic
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  lc2.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  lc2.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
  lc2.clearDisplay(0);

  resetGame();
}


// MAIN LOOP
void loop() {

  // Start Game Button
  if(digitalRead(startButtonPin)){
    Serial.print("Game Started");
    // reset score
    resetGame();

    // Clear last messages
    lc2.clearDisplay(0);
    // set game state
    gameActive = true;
  }

  // Run Game
  if (gameActive){
    runGame();

    if (statsMillis > 500) {    
      statsMillis = 0;         // reset the counter to 0 so the counting starts over...
      stats();
    }
  }else{
    standbyRoutine();
  }
}

first of all welcome to the arduino user-forum.

If you show some own effort you can receive help with hundreds of answers.
Well what do you think: How much time does it need to learn from scratch to drive such an excavator?

5 Minutes?
60 Minutes?
2 hours?
10 hours?

It is similar with programming.
As you are unexperienced you can not know it. Your question with

  • here is the functionality
  • here is my code

can you make it work?

is similar like calling a doctor telling him on the phone
oh I feel so bad - can you please heal me?

The doctor will say: I can help but you have to come to me personally and I have a lot of additional questions to you you have to answer and then after answering all these questions I can start examining you what is causing your illness.

Same with programming.
a lot of additional questions:
where did you find this project? post a link
Does this websource tell all the details about the used components?

what exact type of microcontroller are you using?
what type of driver-chip is used on the 7 segment display?
how ist everything powered?
how much power does the 7-segment display need?
where did you get the LedCpntrol.h library from? post a link

best regards Stefan

Thanks for the reply. I know that programming must be studied and tested and that is why it fascinates me. however this project is already underway and I don't have time to study Arduino programming. with your corrections I will begin to learn. the link where I found this project and this:

this is the nano card I bought

AZDelivery Scheda Nano V3 con... https://www.amazon.it/dp/B01MS7DUEM?ref=ppx_pop_mob_ap_share

the LedControl.h library is that of the arduino program

unfortunately as you can see from the post link there is not much information. however, it is the only project I have found that has the functions that interest me. time-points-bonus-topscore

not true! It might be that the fault that causes your build to switch off the display was not
pop-shot'ting up right to your eyes.

Compared to many many other projects there is a lot of information and explaining how it is done and which part of the code is doing what.

Anyway. This code is 369 lines long. Not really short not really long code.
So it will take some time but not years to analyse what causes your project to not function as expected yet.

additional questions:
do you have a digital multimeter?
If you start tinkering with microcontrollers a digital multimeter is a must.

As you are a real beginner and haven't told much details of what you have done so far
there are a lot of questions:
to what board did you adjust the board selection?

what does that mean "arduino program" which arduino program??
the source-code? the IDE?

in file - preferences change to verbose output and activate compiler-warnings All
After compiling, copy the content of the output-window into a posting as a code-section

Everything clear? Surely not.

I have written what you have to do as first steps in very short words to put you into the situation of not exactly knowing what to do.

It is the same for your potential helpers. There is still a lot of information missing to narrow down the problem.

As faster as you provide more information as faster the forum can help you.

This program is known for working. So it might be a hardware-problem.
Some wires connected wrong, too small power-supply etc.

From where do the 7-segment displays get power?
If it is from the arduino-nano then you are a lucky guy if your arduino's voltage-regulator is still alive and functioning.

So many 7-segments draw too much current for powering them directly from the arduino nano. The 7-segment-displays and the RGB-LED need their own powersupply.

As a next step answer the questions you have been asked.
If you do not answer the questions you decrease the will of other users to answer at all.
There are a lot more questions every day on the forum that make more fun than asking again and again to finally answer questions.

best regards Stefan

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