Using a Redboard Plus and 1270 LED seven segment display connected via SDA and SCL pins on the board. When I power up thru my USB cable Have to hit the reset and unplug and plug many times to get the program to run. I know its working as the number nine appears on the LED display. I can run the score and reset the game via the program as long as I want. if I unplug and wait a few minutes it a crap shoot on wether or not the program will start. Reset, unplug, plug ,reset and it might come to life.
After trying numerous combinations, it appears that the program always runs when I upload the program and hit the reset button on the board. the number nine appears and all is well.
There must be a glitch in the program but I don’t have the expertise to find it. An associate at the Makers Space helper me in writing the program.
Any comments will be greatly appreciated, Dave
[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]