Jose Romaní
Published © CC BY-NC

James Harden 3-Points Baskets Arduino Simulator

Pressing a button you can throw to basket like James Harden with its 3-points shooting streaks.

IntermediateFull instructions provided325
James Harden 3-Points Baskets Arduino Simulator

Things used in this project

Story

Read more

Schematics

harden_baskets_button_copy_VdXGX2oCfM.fzz

Mount this schematic of the bread board.

Code

Harden_baskets_LED_button_jan122020

Arduino
Firmware. USE WITH BUTTON. Press the button to shoot to basket. Use together with Serial Plotter or Serial Monitor if you wish.
/*
   We suppose James Harden has a three-points percentage of 40% but with propensity to streaks of several baskets in a row.
   Open Serial Monitor and push the button to shoot to basket!
   Author: Jose Romaní. 
   01/07/2021 Vigo, Spain.
   website: https://sites.google.com/view/pi9hmb/home
*/


int threePercent = 40; //you can play with this value. Better with 30 <= treePercentage <= 100;
//unsigned int lapse = 20; //you can play with this parameter. Only use if button is avoid. Comment(//) also code: stopLoop = true; of the last line of the loop 
unsigned long convertedBaskets, failedBaskets, total; 
float rate;
int offset, less, more;
int inarowFailed, inarowConverted;
//for hardware:
#define LED0 8
#define LED1 7
#define LED2 6
#define BUTTON 2 //new throw to basket
volatile bool stopLoop = false; //start/stop main loop

//debounce
#define debounceDelay 200
long t = - debounceDelay; 

void setup() {
  
  Serial.begin(115200);
  randomSeed(analogRead(A1)); //whatever analog input desconected.
  
  pinMode(LED2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED0, OUTPUT);
  digitalWrite(LED2, LOW); //LEDs OFF
  digitalWrite(LED1, LOW);
  digitalWrite(LED0, LOW);
  
  pinMode(BUTTON, INPUT_PULLUP); //new throw to basket
  attachInterrupt(digitalPinToInterrupt(BUTTON), changeState, RISING);
  stopLoop = true; //inicialize better after attach interrupt.
  Serial.println("Shoot baskets! Push the button on the breadboard\n");
  
}

void loop() {
  //debounce
  while (millis() - t < debounceDelay){ 
    stopLoop = true;
  }
  
  //comment this following line of code and the last of the loop if you do not want to use the button
  while(stopLoop){}
  
//  delay(200); //another alternative of deboucing of button 

  //3 point basket converted
  if (random(threePercent - less, 100 + threePercent + more) >= 100) {
    convertedBaskets++;
    inarowFailed = 0;
    inarowConverted++;
    digitalWrite(LED2, HIGH); //LEDs ON
    digitalWrite(LED1, HIGH);
    digitalWrite(LED0, HIGH);
    Serial.println("CONGRATULATIONS 3-POINT BASKET!");
    if (inarowConverted <= 3 ) more += offset;
    else more = 0;
  }
  //3 point basket failed
  else {
    failedBaskets++;
    inarowConverted = 0;
    inarowFailed++;
    digitalWrite(LED2, LOW); //LEDs OFF
    digitalWrite(LED1, LOW);
    digitalWrite(LED0, LOW);
    Serial.println("3-POINT BASKET FAILED. SHOOT AGAIN");
    if (inarowFailed <= 3) less += offset;
    else less = 0;
  }
  //whole baskets threw
  total = failedBaskets + convertedBaskets;
  //percentage of 3 point shoots
  rate = convertedBaskets *100 / total;

  printing();

//  delay(lapse); //use only without button

  stopLoop = true; //stops loop until new trow (push button) to basket

  //debounce
  t = millis();
}

//See paremeters by Serial Monitor or Serial Plotter
void printing(){
  Serial.print("Three-point baskets converted: ");
  Serial.println(convertedBaskets);
  Serial.print("In a row converted: ");
  Serial.println(inarowConverted);
  Serial.print("Three-points baskets failed: ");
  Serial.println(failedBaskets);
  Serial.print("In a row failed: ");
  Serial.println(inarowFailed);
  Serial.print("Baskets tried: ");
  Serial.println(total);
  Serial.print("3-Point Field Goal Percentage, FG3: ");
  Serial.print(rate);
  Serial.println(" %");

  Serial.println();
  Serial.println("------------------");
}

//function for the button
void changeState(){
  stopLoop = false;
}

Harden_baskets_LED_Serial_jan122020

Arduino
If you do not use the button, or even do not want to mount the circuit, but you want to see what happens if it were shot many baskets. Then you should see the accumulated statistics, mainly by Serial Monitor. But also you can use the Serial Plotter to see the statistics drawn.
/*
   We suppose James Harden has a three-points percentage of 40% but with propensity to streaks of several baskets in a row.
   Open Serial Monitor and push the button to shoot to basket!
   Author: Jose Romaní. 
   01/07/2021 Vigo, Spain.
   website: https://sites.google.com/view/pi9hmb/home
*/


int threePercent = 40; //you can play with this value. Better with 30 <= treePercentage <= 100;
unsigned int lapse = 20; //you can play with this parameter. Only use if button is avoid. 
unsigned long convertedBaskets, failedBaskets, total; 
float rate;
int offset, less, more;
int inarowFailed, inarowConverted;
//for hardware:
#define LED0 8
#define LED1 7
#define LED2 6
#define BUTTON 2 //new throw to basket
volatile bool stopLoop = false; //start/stop main loop

//debounce
#define debounceDelay 200
long t = - debounceDelay; 

void setup() {
  
  Serial.begin(115200);
  randomSeed(analogRead(A1)); //whatever analog input desconected.
  
  pinMode(LED2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED0, OUTPUT);
  digitalWrite(LED2, LOW); //LEDs OFF
  digitalWrite(LED1, LOW);
  digitalWrite(LED0, LOW);
  
  pinMode(BUTTON, INPUT_PULLUP); //new throw to basket
  attachInterrupt(digitalPinToInterrupt(BUTTON), changeState, RISING);
  stopLoop = true; //inicialize better after attach interrupt.
  Serial.println("Shoot baskets! Push the button on the breadboard\n");
  
}

void loop() {
  //debounce
  while (millis() - t < debounceDelay){ 
    stopLoop = true;
  }
  
  //comment this following line of code and the last of the loop if you do not want to use the button
  //comment(//) also code: stopLoop = true; of the last lines of the loop if you do not want to use the button
//  while(stopLoop){}
  
//  delay(200); //another alternative of deboucing of button 

  //3 point basket converted
  if (random(threePercent - less, 100 + threePercent + more) >= 100) {
    convertedBaskets++;
    inarowFailed = 0;
    inarowConverted++;
    digitalWrite(LED2, HIGH); //LEDs ON
    digitalWrite(LED1, HIGH);
    digitalWrite(LED0, HIGH);
    Serial.println("CONGRATULATIONS 3-POINT BASKET!");
    if (inarowConverted <= 3 ) more += offset;
    else more = 0;
  }
  //3 point basket failed
  else {
    failedBaskets++;
    inarowConverted = 0;
    inarowFailed++;
    digitalWrite(LED2, LOW); //LEDs OFF
    digitalWrite(LED1, LOW);
    digitalWrite(LED0, LOW);
    Serial.println("3-POINT BASKET FAILED. SHOOT AGAIN");
    if (inarowFailed <= 3) less += offset;
    else less = 0;
  }
  //whole baskets threw
  total = failedBaskets + convertedBaskets;
  //percentage of 3 point shoots
  rate = convertedBaskets *100 / total;

  printing();

  delay(lapse); //use only without button

//  stopLoop = true; //stops loop until new trow (push button) to basket

  //debounce
  t = millis();
}

//See paremeters by Serial Monitor or Serial Plotter
void printing(){
  Serial.print("Three-point baskets converted: ");
  Serial.println(convertedBaskets);
  Serial.print("In a row converted: ");
  Serial.println(inarowConverted);
  Serial.print("Three-points baskets failed: ");
  Serial.println(failedBaskets);
  Serial.print("In a row failed: ");
  Serial.println(inarowFailed);
  Serial.print("Baskets tried: ");
  Serial.println(total);
  Serial.print("3-Point Field Goal Percentage, FG3: ");
  Serial.print(rate);
  Serial.println(" %");

  Serial.println();
  Serial.println("------------------");
}

//function for the button
void changeState(){
  stopLoop = false;
}

Credits

Jose Romaní

Jose Romaní

8 projects • 4 followers
Industrial Technical Engineer in Electronics and Automation

Comments