Hey everyone. This is my first post here, and I am very new to Arduino. I'm trying to create a game, where i have an lcd screen, a push button, and an ultrasonic sensor hooked up to my arduino leonardo.
The goal of the game is, if button is pushed, a random number between 1-30 will be displayed. You then have 2 seconds to move a piece of paper to the number shown (there is a ruler in place in front of the sensor).
Now, the sensor has a typical range of ~80cm sometimes higher. The way i am calculating if you got it in two seconds is, if cm < 70 (meaning your hand or piece of paper passed this threshold), start set time1. Once cm = randNumber, set time2. If time2-time1 < 2000 (milliseconds), score++;
I've create a boolean of isInZone to false in my declaration. This boolean serves to shut off the time1 set and time2 set once they have been reached, so that only one instance of the two will be calculated.
But this time thing is what has been causing me issues. If anyone could offer advice, maybe I need a library, or I'm using the wrong variable, thank you.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TimeLib.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int trigPin = 5; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 4; // Echo Pin of Ultrasonic Sensor
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time = 0;
int button =6;
int score =0;
bool isInZone=false;
// float current_time = 0;
// float thousand = 1000;
long randNumber;
void setup()
{
Serial.begin(9600); // Starting Serial Terminal
randomSeed(analogRead(0));
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Distance:");
delay(50);
lcd.setCursor(12,0);
lcd.print("cm");
delay(50);
}
void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// current_time = millis();
// the code above is taken from
// https://www.instructables.com/id/Measuring-Distance-Over-Time-With-Arduino-HC-SR04-/^^
if(digitalRead(button)==HIGH){
lcd.setCursor(10,0);
randNumber = random(1, 30);
lcd.print(randNumber);
delay(50);
}
// Serial.println(current_time/thousand);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.println(randNumber);
delay(100);
// get distance
if ((cm < 70) && (isInZone == false)) {
time1 = millis();
delay(50);
Serial.println("time1");
Serial.println(time1);
Serial.println();
isInZone=true;
}
if ((cm == 22) && (isInZone == true)) {
time2 = millis();
Serial.println("time2");
Serial.println(time2);
Serial.println();
}
if (time2-time1 <= 2000 && isInZone == true) {
score++;
isInZone=false;
} else if (time2 - time1 > 2000 && isInZone == true){
score--;
isInZone=false;
}
lcd.setCursor(1,1);
lcd.print("Score:");
lcd.print(score);
delay(100);
// lcd.setCursor(1,1);
// lcd.print("Time:");
// lcd.setCursor(7,1);
// lcd.print(time+"s");
// delay(50);
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
// this code is also taken from https://www.instructables.com/id/Measuring-Distance-Over-Time-With-Arduino-HC-SR04-/
return microseconds / 29 / 2;
}