Trying to increase a variable when Ultrasonic senses a certain distance

Hi. I am trying to create a game in which once a ball gets close enough to an ultrasonic sensor, a point is added. The code I'm using doesn't seem to work. Any suggestions?

//Bali Tiger Skeeball

//pinVars

const int buttonPin = 10;
const int button2Pin = 3;
const int servoPin = 11;
const int backlight = 13;
int lastbuttonstate;
int currentbuttonstate;
int lastbs;
int currentbs;
int score = 0;
int time = 0;
float distance = 0;
int debounce = 1000;
const int trigPin = 10;
const int echoPin = 9;


//Servo Setup
#include <Servo.h>
Servo doorServo;

//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup() {
  Serial.begin(9600);                
  pinMode(buttonPin, INPUT_PULLUP);
  doorServo.attach(servoPin);
  pinMode(backlight, OUTPUT);
  pinMode(button2Pin, INPUT_PULLUP);

  doorServo.write(60);
  currentbuttonstate = digitalRead(buttonPin);
 
  currentbs = digitalRead(button2Pin);
 
  digitalWrite(backlight, HIGH);
  lcd.begin(16,2);
  lcd.print("Score: ");
  lcd.print(score);
  lcd.setCursor(0,1);
 
}

void loop() {
 

  lastbuttonstate = currentbuttonstate;      
  currentbuttonstate = digitalRead(buttonPin);
 
  lastbs = currentbs;
  currentbs = digitalRead(button2Pin);
 
 
  if(lastbuttonstate == HIGH && currentbuttonstate == LOW && millis() - time > debounce) {
    Serial.println("Button A was pressed");
    time = millis();
    score = 0;
    printscore();
    doorServo.write(152.5);
    delay(5000);
doorServo.write(60);
  }
  
    distance = getDistance(); //variable to store the distance measured by the sensor
 Serial.print(distance); //print the distance that was measured
 Serial.println(" in"); //print the units after the distance
  if(distance <= 10)
  {

    score=score+1;
    printscore();
    delay(250);
  }

  else
  {
    
  }


//Functions

float getDistance()     

{
  float echoTime;    
  float calculatedDistance; 

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  echoTime = pulseIn(echoPin, HIGH);    
  calculatedDistance = echoTime / 148.0; 

  return calculatedDistance;  
}


void printscore() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Score: ");
  lcd.print(score);
}


sketch seems to be ok

"ok" is an interesting description for code that doesn't compile.

Do you mean it wouldn't compile? Or do you have another version that compiles but doesn't do what you want? If so post that and explain what it's not doing correctly.

 else
  {

  }
}// loop() had no closing brace

It would compile but the LCD displays weird combinations of letters and numbers instead of a score

Well the code you posted didn't compile(missing the } on loop()), so post your version that did.

What does that mean? Post a photo perhaps?

If you run any test sketch like whatever come with your lcd library, does the lcd work then, by itself?

A couple of questions about your logic.

  1. Do you want the score to continue to increment if the ball remains less than 10 away from the sensor? In your code while the ball in less than 10 from the sensor you delay 250ms.
  2. Why is your debounce time 1 second? This requires you to hold the button for at least 1 second to register it. Is this what you intended?

Hi. I am attempting to make a game which increases the score variable by one when the ultrasonic senses an object in it's presence. The code I have right now isn't working, any suggestions?

//Bali Tiger Skeeball

//pinVars

const int buttonPin = 10;
const int button2Pin = 3;
const int servoPin = 11;
const int backlight = 13;
int lastbuttonstate;
int currentbuttonstate;
int lastbs;
int currentbs;
int score = 0;
int time = 0;
float distance = 0;
int debounce = 1000;
const int trigPin = 10;
const int echoPin = 9;


//Servo Setup
#include <Servo.h>
Servo doorServo;

//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup() {
  Serial.begin(9600);                
  pinMode(buttonPin, INPUT_PULLUP);
  doorServo.attach(servoPin);
  pinMode(backlight, OUTPUT);
  pinMode(button2Pin, INPUT_PULLUP);

  doorServo.write(60);
  currentbuttonstate = digitalRead(buttonPin);
 
  currentbs = digitalRead(button2Pin);
 
  digitalWrite(backlight, HIGH);
  lcd.begin(16,2);
  lcd.print("Score: ");
  lcd.print(score);
  lcd.setCursor(0,1);
 
}

void loop() {
 

  lastbuttonstate = currentbuttonstate;      
  currentbuttonstate = digitalRead(buttonPin);
 
  lastbs = currentbs;
  currentbs = digitalRead(button2Pin);
 
 
  if(lastbuttonstate == HIGH && currentbuttonstate == LOW && millis() - time > debounce) {
    Serial.println("Button A was pressed");
    time = millis();
    score = 0;
    printscore();
    doorServo.write(152.5);
    delay(5000);
doorServo.write(60);
  }
  
    distance = getDistance(); //variable to store the distance measured by the sensor
 Serial.print(distance); //print the distance that was measured
 Serial.println(" in"); //print the units after the distance
  if(distance <= 10)
  {
    Serial.println("Point is added");
    score=score+1;
    printscore();
    delay(250);
  }

  else
  {
    Serial.println("No change");
  }
  
}


//Functions

float getDistance()     {
  float echoTime;    
  float calculatedDistance; 

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  echoTime = pulseIn(echoPin, HIGH);    
  calculatedDistance = echoTime / 148.0; 

  return calculatedDistance;  
}


void printscore() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Score: ");
  lcd.print(score);
}



What does it do or not do ?

The LCD shows a bunch of random letters and numbers like "4DDDDDDD," "7wwwwwww" or "1======="

const int button2Pin = 3;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Are you trying to use pin 3 for 2 different purposes ?

I have merged your cross-posts @1222.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please take some time to pick the forum category that best suits the subject of your question and then only post once to that forum category. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

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