#include <Wire.h>
#include <LiquidCrystal_I2C.h> //Liraries for LCD display
#include <SoftI2C.h>
#define button 4
const int MPU_addr=0x68; // I2C address of the MPU-6050
int AcX,AcY,AcZ;
const int trigPin = 3;
const int echoPin = 2; //Digital pin input value
float count_shot = 0;
float made_shot = 0;
float field_pct = 0;
int button_pos = 0;
float toll = 0.05;
float normal_z = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftI2C SoftWire = SoftI2C(6,7); // SDA, SCL
void setup(){
Wire.begin();
SoftWire.begin();
SoftWire.beginTransmission(MPU_addr);
SoftWire.write(0x6B); // PWR_MGMT_1 register
SoftWire.write(0); // set to zero (wakes up the MPU-6050)
SoftWire.endTransmission(true);
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(trigPin, OUTPUT); //Ultrasonic Pins
pinMode(echoPin, INPUT);
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker");
lcd.setCursor(0, 1);
lcd.print("Field Goal:"); //LCD Print
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
}
void loop(){
for(int ind = 0; ind < 5; ind++) {
SoftWire.beginTransmission(MPU_addr);
SoftWire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
SoftWire.endTransmission(false);
SoftWire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=SoftWire.read()<<8|SoftWire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=SoftWire.read()<<8|SoftWire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) //X, Y, Z output realing calculations
AcZ=SoftWire.read()<<8|SoftWire.read();
float normal_z = float(AcZ/16384);
}
SoftWire.beginTransmission(MPU_addr);
SoftWire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
SoftWire.endTransmission(false);
SoftWire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=SoftWire.read()<<8|SoftWire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=SoftWire.read()<<8|SoftWire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) //X, Y, Z output realing calculations
AcZ=SoftWire.read()<<8|SoftWire.read();
float ac_x = float(AcX/16384);
float ac_y = float(AcY/16384);
float ac_z = float(AcZ/16384);
double duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(12);
digitalWrite(trigPin, LOW); //Inputs on Ultrasonic distance sensor
duration=pulseIn(echoPin, HIGH);
double dist = (duration/2.0/29.1); //Calculation for distance
if (dist < 22 or ac_z < (normal_z - toll) or ac_z > (normal_z + toll)) {
count_shot += 1;
if (dist < 22) {
made_shot += 1;
delay(2000);
} else {
bool flag = 0;
double time1 = millis();
double time_diff = 0;
while (flag == 0 and time_diff <= 2000) {
double duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(12);
digitalWrite(trigPin, LOW); //Inputs on Ultrasonic distance sensor
duration=pulseIn(echoPin, HIGH);
double dist = (duration/2.0/29.1);
if (dist < 22) {
made_shot += 1; //Calculation for whether shot actually went into hoop
flag = 1;
delay(2000 - time_diff);
}
double time2 = millis();
time_diff = time2 - time1;
}
// int extra_time = 2000 - time_diff;
// delay(extra_time);
}
// Serial.print("Field Goal: ");
// Serial.print(made_shot);
// Serial.print(" / ");
// Serial.println(count_shot);
// delay(1000);
if(count_shot > 0) {
field_pct = made_shot / count_shot * 100; //Field Goal % Calculation
} else {
field_pct = 0;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker"); //LCD display intially
lcd.setCursor(0, 1);
lcd.print("Field Goal:");
if(made_shot < 10) {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
} else if(made_shot < 100 and made_shot >= 10) {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(2, 2);
lcd.print("-");
lcd.setCursor(3, 2); //LCD Print field goals made / attempted
lcd.print(count_shot, 0);
} else {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(3, 2);
lcd.print("-");
lcd.setCursor(4, 2);
lcd.print(count_shot, 0);
}
lcd.setCursor(19, 2);
lcd.print("%"); //LCD print % sign for field goal percentage
lcd.setCursor(14, 2);
lcd.print(field_pct, 1);
}
//field goal percentage logic
// Serial.println(field_pct);
//lcd code
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//reset button code
button_pos = digitalRead(button);
if(button_pos == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RESET");
made_shot = 0;
count_shot = 0;
field_pct = 0;
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker"); //LCD print for if reset button is pressed
lcd.setCursor(0, 1);
lcd.print("Field Goal:");
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
}
}
The code should work like this The way it's supposed to work:
count_shot is incremented if-
The ball hits the hoop and goes through
The ball hits the hoop and doesn't go through
The ball goes through without hitting the hoop (swish)
shot_made is incremented if-
The ball goes through without hitting the hoop
The ball goes through and hit the hoop
I have a problem where the ball hits a hoop and it misses and its not counted as a miss for the field goal percentage + when i touch it it counts as a score.