1 - Stepper Motor Horse Control

ABOUT THE COURSE

We will go over the final code for the stepper motor boards. The boards will receive a 5 digit code, convert that to a race, then race.

Video #1

 
  #include 
  
  Stepper motor(800, 13, 12, 11, 10);
  
  boolean firstrace = true;
  boolean getrace = false;
  boolean stage = false;
  boolean race = false;
  
  long x = 0;
  
  int racenum = 0;
  int racestep = 0;
  int rn = 0;
  
  void setup()
  {
    pinMode(7, INPUT);
    pinMode(6, INPUT);
    Serial.begin(9600);
    
    motor.setSpeed(70);
  }
  
  void loop()
  {
    // move horse to finish switch
    while (firstrace == true)
    {
      motor.step(200);
      delay(200);
      if (digitalRead(7) == HIGH)
      {
        firstrace = false;
        getrace = true;
      }
    }
    
    // wait and get signal
    while (getrace == true)
    {
      while (Serial.available() < 1)
      {
        delay(1);
      }
      
      if (Serial.available() != x)
      {
        x = Serial.available();
        x = Serial.parseInt();  // 12345
      }
      
      racenum = x * .001; // 12345 = 12.345
      racenum = int(racenum);  // 12
      racenum = racenum * 1000; // 12000
      racenum = x - racenum; // 12345 - 12000 = 345
      racenum = racenum * .1; // 34.5
      racenum = int(racenum); // 34
      
      if (racenum > 0)
      {
        rn = racenum;
          if (rn == 26 || rn ==  32 || rn ==  38 || rn ==  47 || rn ==  53 || rn ==  59 || rn ==  77)
          {
            racenum = 1;
          }
          else if (rn == 20 || rn ==  27 || rn ==  33 || rn ==  39 || rn ==  44 || rn ==  56 || rn ==  57 || rn ==  66 || rn ==  72 || rn ==  78)
          {
            racenum = 2;
          }
          else if (rn == 37 || rn ==  67 || rn ==  73 || rn ==  79 || rn ==  36 || rn ==  74 || rn ==  68 || rn ==  50 || rn ==  35 || rn ==  29 || rn ==  23 || rn == 41 || rn ==  65 || rn ==  71 || rn ==  45 || rn ==  21)
          {
            racenum = 3;
          }
          else if (rn == 46 || rn ==  52 || rn ==  58 || rn ==  76 || rn ==  42 || rn ==  75 || rn ==  69 || rn ==  60 || rn ==  54 || rn ==  48 || rn ==  30 || rn ==  22 || rn ==  24 || rn ==  28 || rn ==  34 || rn ==  40)
          {
            racenum = 4;
          }
          else
          {
            racenum = 5;
          }   
          getrace = false;
          stage = true;
      }
    }
    
    while (stage == true)
    {
      while (digitalRead(7) == HIGH)
      {
        delay(1);
      }
      motor.step(100);
      delay(50);
      if (digitalRead(6) == HIGH)
      {
        stage = false;
        race = true;
      }
    }
    
    while (race == true)
    {
      while (digitalRead(6) == HIGH)
      {
        delay(1);
      }
      
     if (racenum == 1)       // 22.75
     {
      while (race == true)
      {
        motor.step(400);
        delay(100);
        if (digitalRead(7) == HIGH)
        {
          race = false;
          getrace = true;
        }
      }
    }
    
    if (racenum == 2)       //  23.72
    {

      racestep = 38;
      while (racestep > 0)
      {
        motor.step(400);
        delay(100);
        racestep--;
      }
      while (race == true)
      {
        motor.step(200);
        delay(150);
        if (digitalRead(7) == HIGH)
        {
          race = false;
          getrace = true;
        }
      }
    }
    
    if (racenum == 3)       // 24.75
    {
      racestep = 20;
      while (racestep > 0)
      {
        motor.step(400);
        delay(200);
        racestep--;
      }
      while (race == true)
      {
        motor.step(400);
        delay(100);
        if (digitalRead(7) == HIGH)
        {
          race = false;
          getrace = true;
        }
      }
    }
    
    if (racenum == 4)       // 26.58
    {
      racestep = 20;
      while (racestep > 0)
      {
        motor.step(200);
        delay(100);
        racestep--;
      }
      racestep = 25;
      while (racestep > 0)
      {
        motor.step(400);
        delay(100);
        racestep--;
      }
      while (race == true)
      {
        motor.step(200);
        delay(200);
        if (digitalRead(7) == HIGH)
        {
          race = false;
          getrace = true;
        }
      }
    }
    
    if (racenum == 5)       // 27.3 
    {
      racestep = 30;
      while (racestep > 0)
      {
        motor.step(400);
        delay(100);
        racestep--;
      }
      while (race == true)
      {
        motor.step(200);
        delay(200);
        if (digitalRead(7) == HIGH)
        {
          race = false;
          getrace = true;
        }
      }
    }
    }
  }