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);
}
"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.
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.
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);
}
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.