Golf_Club Arduino Code

Back to Home

const int Tee_Laser = 3;
const int Release_Laser = 2;
const int LED = 13;


int state = 0;
int count;
int x_base, y_base, z_base,
x_calc, y_calc, z_calc;


int HIT = 0;    
unsigned long timer;
unsigned long current;
unsigned long start;
unsigned long finish;
int current_disp;
float total;



const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)

static FILE uartout = {
  0}
;    // FOR PRINTF
static int uart_putchar (char c, FILE *stream)    // FOR PRINTF
{
  Serial.write(c) ;
  return 0 ;
}


void setup ()
{
  pinMode(LED,OUTPUT);
  pinMode(Release_Laser, INPUT);
  pinMode(Tee_Laser, INPUT);

  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);

  Serial.begin(9600);

  // fill in the UART file descriptor with pointer to writer
  fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);    // FOR PRINTF

  // The uart is the standard output device STDOUT
  stdout = &uartout;    // FOR PRINTF
}

void loop ()
{
  switch (state)
  {
  case 0:
    {
      digitalWrite (LED, LOW);
      if (digitalRead (Tee_Laser) == HIGH)
        state = 1;
      count = 0;
    }
    break;
  case 1:
    {
      digitalWrite(LED,HIGH);
      if(count++ >= 3000)
      {
        x_base = analogRead(xpin);
        y_base = analogRead(ypin);
        z_base = analogRead(zpin);
        printf("\t\t\t\t\t%i\n\t\t\t\t\t%i\n\t\t\t\t\t%i\n", x_base, y_base, z_base);
        state = 2;
      }
      else if (digitalRead(Tee_Laser) == LOW)
        state = 0;
    }
    break;
  case 2:
    {
      count = 0;
      digitalWrite(LED, LOW);
      if (digitalRead(Tee_Laser) == LOW);
      state = 3;
    }
    break;
  case 3:
    {     
      count ++;
      if (count > 100)
      {
        state = 4;
        timer = millis ();
      }
      else if (digitalRead(Tee_Laser) == HIGH)
        state = 2;
    }
    break;
  case 4:
    {
      current = millis ();
      current_disp = current - timer;
      x_calc = analogRead(xpin)-x_base;
      y_calc = analogRead(ypin)-y_base;
      z_calc = analogRead(zpin)-z_base;
      printf ("%i\t%i\t%i\t%i\t%i\n", current_disp,HIT,x_calc,y_calc,z_calc);     

      digitalWrite(LED, HIGH);
      if (digitalRead (Tee_Laser) == HIGH)
      {
        HIT = 500;
        start = millis();
        state = 5;
      }
    }
    break;
  case 5:
    {
      current = millis ();
      current_disp = current - timer;
      x_calc = analogRead(xpin)-x_base;
      y_calc = analogRead(ypin)-y_base;
      z_calc = analogRead(zpin)-z_base;
      printf ("%i\t%i\t%i\t%i\t%i\n", current_disp,HIT,x_calc,y_calc,z_calc);    
      if (digitalRead (Release_Laser) == HIGH)
        state = 6;
      digitalWrite(LED, HIGH);
    }
    break;

  case 6:
    {
      finish = millis();
      total = finish - start;
      total = total / 1000.0;
      total = 7 / total;

      digitalWrite(LED, LOW);
      Serial.print ("\t\t\t\t\t");
      Serial.print (total);
      Serial.println ("  centimeters / second");
      count = 0;
      state = 0;
    }
    break;
  default:
    state = 0;
    break;
  }
  delay(1);
}