Basketball Project

Hi there,
I’m relatively new in the world of Arduino but I’m working on writing a program using the Elegoo Nano and the ultrasonic distance reader. I’m planning on putting the distance reader on the back of the basketball hoop and I want the reader to measure when the ball is in the hoop. The point is to track how many shots I am making.

I’ve measured and the distance when the ball is in the basket to where I’ve placed the sensor is about 7-8 cm. So I’ve got the sensor recording, and I’ve even got the beginning of the if() statement to record when the reader senses the ball in the hoop, but now I want to have it print in the Serial Monitor. I’ll past the code I have so far down below, but I can’t figure out how to get it to +1 every time the sensor measures a value less than 13 cm (my measurement for margin of error). Could someone help me. Thanks

const int trigPin = 3;
const int echoPin = 6;
int x = 0;

float duration, distance;

void setup() {
// put your setup code here, to run once:

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);

duration = pulseIn(echoPin,HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);

if (distance < 10)
{
1+x;
Serial.print(“x”);
}
}

1+x

No.

x = x + 1;
// or
x++;
// or
x += 1;

Note that this code can possibly run so fast that your sensor will measure multiple times while the basketball is in the hoop. You should probably record when the basketball is in the hoop and then wait until the sensor detects no basketball in the hoop before incrementing again.