This program is for scoring on a skeeball game I made. 10 to 100 points depending which hole you hit. Score in totaled after each shot. After 9 balls and the reset light comes on. Depress the reset button and the ball count goes back to nine and the score back to zero.
When I power up my Arduino Uno program some times it starts and sometimes not. hitting reset and powering off and on might start it. When it runs my display lights up and all else works. Frustrating because I can't find rime or reason for program running. It almost seems random but I think it has to do with timing.
Thanks for your help. Dave
type or paste code [code]
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment();
//Name servo
Servo ballReturn;
// Pin declarations
int sensors[7] ={A2,3,4,5,6,A3,9,}; //create an array of sensors //REDBOARD CHANGE //4 was A4 x1
int resetButton = 10;
int servoPin = 11;
int resetDisplays = 10;
int LED_Pin = 13;
// General declarations
int score = 0;
int balls = 9;
int n = 0;
int result;
int s;
// Debounce delays
int sensDelay = 500;
int resetDelay = 50;
// Servo declaration
int open = 90;
int close =180;
void setup() {
// using a for loop to designate each input pin as an INPUT.
for (int eachSensor = 0; eachSensor <= 6; eachSensor++) {
pinMode(sensors[eachSensor] ,INPUT_PULLUP);
}
pinMode(resetButton, INPUT_PULLUP);
ballReturn.attach(servoPin);
Serial.begin(9600);
updateDisplays();
matrix.begin(0x70);
pinMode(LED_Pin,OUTPUT);
pinMode(A2, INPUT_PULLUP); //REDBOARD ADD
pinMode(A3, INPUT_PULLUP);
//pinMode(A4, INPUT_PULLUP); removed in x1
matrix.print(10*score+balls);//new works
matrix.writeDisplay();//new works
}
void loop() {
reset();//new
while (balls > 0) {
digitalWrite(LED_Pin, LOW);//new
reset();//new
// returnControl(close);
for(int s = 0; s <= 6; s++){ //check sensor array.
result = digitalRead(sensors[s]);
if (result == LOW) {
if (s == 0) { n = 100;
} else if (s == 1) {
n = 100;
} else if (s == 2) {
n = 50;
} else if (s == 3) {
n = 40;
} else if (s == 4) {
n = 30;
} else if (s == 5) {
n = 20;
} else if (s == 6) {
n = 10;
}
addpoints(n);
}
}
//add line: a break attached to button to reset inc, without resetting arduino.
/* startOver();*/
}//end of while loop
if (balls = 0); {
blnk();
}
} //end of primary loop
void addpoints(int n){
score = score + n;
balls = balls - 1;
updateDisplays();
Serial.print("your score is: ");// prints to serial mointor
Serial.println(score);
Serial.print("balls remaining: ") ;
Serial.println(balls);
delay(sensDelay);
}
void blnk (){
digitalWrite(LED_Pin,HIGH);
/* delay(100);
digitalWrite(LED_Pin,LOW);
delay(100);*/
}
void reset(){
int resetState = digitalRead(resetButton);
if (resetState == LOW){
delay(resetDelay);
returnControl(open);
score = 0;
balls = 9;
updateDisplays();
Serial.print("balls remaining: ");//prints to serial monitor
Serial.println(balls);
delay(1500);
returnControl(close);
}
}
void returnControl(int x){
ballReturn.write(x);
}
void updateDisplays(){
matrix.print(10*score+balls);
matrix.writeDisplay();
}
[/code]here