NEW Skeeball counter won't stop running

I Downloaded counting program from Github but it would not compile. A friend helped with the program but did not have an Arduino to run it. I loaded it. It complied but ran ball numbers into negative values and would not stop. When I triggered a score it would take it, drop the ball number by one then say nine balls available again. It may be the void reset line in the code.
First post and any help will be appreciated.

Dave

#include <SoftwareSerial.h>
#include <Servo.h>

//Name servo
Servo ballReturn;
 
// declare serial displays.
SoftwareSerial scoreDisp(A0,12);
SoftwareSerial ballDisp(A1,13);

// Pin declarations
int sensors[8] = {9,8,7,6,5,4,3,2};    //create an array of sensors
int resetButton = 10;
int servoPin = 11;
int resetDisplay = 10;
 
// General declarations
int score = 0;
int balls = 9;
int n = 0;
int result;
int s;
 
// Buffer Declaration
char ballTemp[10];
char scoreTemp[10];

// Debounce delays
int sensDelay = 500;
int resetDelay = 500;

// Servo declaration
int open = 90;
int close =180;
 
void setup() {
  // using a for loop to designate each input pinas an INPUT. 
  for (int eachSensor = 2; eachSensor <= 9; eachSensor++) {
    pinMode(sensors[eachSensor] ,INPUT);
  }
  pinMode(resetButton, INPUT);
  ballReturn.attach(servoPin);
  Serial.begin(9600);
  ballDisp.begin(9600);
  scoreDisp.begin(9600);
  resetDisplays();
  updateDisplays();
}

void loop()        {

  while (balls > 0) {
    // returnControl(close);

    for(int s = 2; s <= 9; s++){   //check sensor array.
      result = digitalRead(sensors[s]);
      
      if (result == LOW) {
        if (s == 2) {
          n = 100;
        } else if (s == 3) {
          n = 100;
        } else if (s == 4) {
          n = 50;
        } else if (s == 5) {  
          n = 40;
        } else if (s == 6) {
          n = 30;
        } else if (s == 7) {
          n = 20;
        } else if (s == 8) {
          n = 10;
        } else if (s == 9) {
          n = 0;
        }
        addpoints(n);
      }
    }

  //add line: a break attached to button to reset inc, without resetting arduino.
  startOver();
  
  }//end of while loop 

  startOver();

} //end of primary loop 

void addpoints(int n){
  score = score + n;
  balls = balls - 1;
  updateDisplays();
  Serial.print("your score is: ");
  Serial.println(score);
  Serial.print("balls remaining: ");
  Serial.println(balls);
  delay(sensDelay);
}

void reset(){
  returnControl(open);
  score = 0;
  balls = 9;
  updateDisplays();
  Serial.print("balls remaining: ");
  Serial.println(balls);
  delay(1500);
  returnControl(close);
}

void startOver(){
  int resetState = digitalRead(resetButton);
  if (resetState == HIGH){
    reset();
    delay(resetDelay);
  }
}

void returnControl(int x){
  ballReturn.write(x);
}

void updateDisplays(){
  resetDisplays();
  sprintf(ballTemp, "%4d", balls);
  sprintf(scoreTemp, "%04d", score);
  ballDisp.print(ballTemp);
  scoreDisp.print(score);
}

void resetDisplays(){
  ballDisp.write('v');
  scoreDisp.write('v');
}

Welcome to the forum! Congratulations and thank you for using code tags when posting your code.

A quick look shows you are running off the end of your sensor array. Both in setup() and reading it inside loop.

You have an array of sensors, but your for() loop runs from the actual pin number.

// Pin declarations
const int sensors[] = {9, 8, 7, 6, 5, 4, 3, 2}; //create an array of sensors
const int sensorsCount = sizeof(sensors) / sizeof(sensors[0]);
...
void setup() {
  // using a for loop to designate each input pinas an INPUT.
  for (int eachSensor = 0; eachSensor < sensorsCount; eachSensor++) {
    pinMode(sensors[eachSensor] , INPUT);
  }
...

Same when reading all the values

Blh64
I “think” I see what you mean. I’ll try changing the code and see what happens. Did you really want the for loop to start with zero??

FYI I’m a retired Mechanical Engineer, restore motorcycles, wood working, (built the skeeball game for my daughter) and make model steam engines. Writing code is not my cup of tea. Thanks again for your input.

From St. Louis MO
Dave

dleligdon:
Blh64
Did you really want the for loop to start with zero??

Yes, because you keep the sensor pins in an array. When you access the array with the index 0 (sensors[0]), it will load the first value in the array, which for you is 9. The array access is defined by the index and the array name: array[index]. In C/C++ indices start with 0 (= first element of array). To loop over your array, you therefore need to visit every index from 0 to length - 1.

You might also want to restructure your code a bit:

  1. In a skee-ball game, each hole is associated with a certain amount of points rewarded, when the hole is hit. To test, if the hole is hit, each hole has a sensor associated with it. You should group these two logical properties in your code:
struct SkeeBallHole
{
	int pinNumber;
	int points;
};

This way you can eliminate the big if/else structure in your code.

  1. The skee-ball game itself can also be associated with certain properties: a highscore and the number of balls available for the player. Why not group them as well?
struct SkeeBallGame
{
	int highscore;
	int ballsAvailable;
};
  1. The loop function runs continously, there is no need for a while-loop inside the loop function.

With the restructuring done, your loop function should look something like this:

void loop()
{
	// Enough balls?
	if(game.ballsAvailable > 0)
	{	
		// check, if a hole has been hit
		for(int holeNumber = 0; holeNumber < 8; holeNumber++)
		{
		  result = digitalRead( holes[holeNumber].pinNumber );
		  if(result == LOW)
		  {
			  // add points to highscore
			  game.highscore += holes[holeNumber].points;
			  // decrease the number of balls available
			  game.ballsAvailable--;
			  // we don't need to check the other sensors, we already found the one
			  break;
		  }
		}
	}
	else
	{
		// No more balls to throw. We might want to start over
		startOver();
	}
}

To initialize the holes and game variable you need something like this:

SkeeBallGame game = {0, 9};
SkeeBallHole holes[] = {{9, 0},{8, 10},{7, 20},{6, 30},{5, 40},{4, 50},{3, 100},{2, 100}};

LightuC
Thanks for your reply. I’m learning from you guys. I’ll try one thing at a time and see where it goes.
I am using IR sensors at each hole which signal pins 2 thru 9 to add the various point values. Then add up until 9 balls are used. As stated previously I downloaded t he program from Github. Your program may be simpler to use. Ill try to write a revised program using your ideas.
Thanks again
Dave