Eradic program startup

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

Then tell us how you are powering the Arduino!

I am using 12 Volt power supply via the power port.

Oops

1 Like

How much current does the display take and how is it connected to the Arduino? Your symptoms may relate to a power supply problem.


for(int s = 0; s <= 6; s++){   //check sensor array.
      result = digitalRead(sensors[s]);
      if (result == LOW) {

Keep in mind the ability of switches and sensors to respond and ‘bounce’ far faster than your eyes will see when they are triggered.

Look into detecting WHEN the sensor changes state, not just if they are on or off.

Paul,

I thought that might be the problem so I measured it. On start up with number 9 lit up it draws about 8 milliamps with all digits lit up it draws about 35. milliamps.
the display is connected via SCL and SDA pins with 5 volts of power. the board I am using has an auto reset circuit breaker that I can defeat.

Dave

Actually, you have to measure it with an oscilloscope. A DVM is too slow to see any sudden drops.

LCN,
I have debounce delays in the program but not sure if this is what you are talking about. When the the program does not start i.e. no display there is no voltage on the digital pins. Only the LED reset pushbutton lights up as the LED reset pushbutton is wired to pin13 and none of the inputs are live. No voltage . When the program does start running i.e. the LCD display shows nine (balls) at this time the inputs go high and when grounded they add to the score.

The inputs are static until the sensor in the hole I tripped.

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

Hope this answers your question

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