logo

Smart Livestock Management

Home Final Project About Me

Week 12 Documentation

Fusion360 spool file

  • Spool file v1
  • Hardware

    CNC Shield, Stepper Motor, & Ultrasonic Sensor

    I used quarter steps for the stepper motor to ensure a less jerky movement. View the stepper driver configuration below (src here).

    I used pins D12 & D13 for the ultrasonic sensor. It's important to ensure that the pins being used aren't also being used for the stepper motor (using the pins on the right side is a safe choice). I also plugged in the sensor to the 5V and GND pins on the right side of the shield. The battery (or whatever 12V power source used) actually powers the Arduino too.

    The finished wiring (battery not shown, battery replaces plug-in).

    Now, it's important to calculate number of steps required for the spider to travel 10 feet. I used the radius of the spindle and the number of quarter steps in a revolution to calculate this.

            
              1 revolution total length of string if spindle has a radius of 0.79 in =
    
    
              =4.96 inch circumference (spider falls 4.96 inches/revolution)
              
              Full step = 200 steps per revolution
              
              Quarter step = 800 steps per revolution
              
              Find inches per step:
              
              4.96/800 = 0.0062 inches/step 
              
              Solve for x (number of steps): 
              
              0.0062 * x = 84 inches(7’)
              
              84 / 0.0062 = 13548 steps for the spider to fall 7 feet in quarter step mode
              
              
            we'll make it 12000 just to be safe lol, using excess string would be bad          
    
            
          

    Code for just the ultrasonic sensor

            
              #include "Ultrasonic.h"
    
    /*
       Pass as a parameter the trigger and echo pin, respectively,
       or only the signal pin (for sensors 3 pins), like:
       Ultrasonic ultrasonic(13);
    */
    Ultrasonic ultrasonic(12, 13);
    int distance;
    int headHeight = 24; // in inches
    
    
    void setup() {
      Serial.begin(9600);
    }
    
    int increment = 0;
    void loop() {
      // Pass INC as a parameter to get the distance in inches
    
    
      distance = ultrasonic.read(INC);
    
      Serial.print("Distance in Inches: ");
      Serial.println(distance);
      
      // make an incrementor, increment goes up for each distance less than 24, if the increment reaches 5 (on i=5),
      // deploy
    
      if (distance < headHeight) {
      
      increment++;
      Serial.println(increment);
      if (increment == 5) {
        Serial.println("deploy spider");
        increment = 0;
      }
      } else{
        Serial.println("dont deploy spider");
      }
    delay(5000);
    }
            
          

    Motor+sensor code integrated together.

              
                // Basic CNC shield stepper motor example.
                #include "Ultrasonic.h"
                
                /*
                   Pass as a parameter the trigger and echo pin, respectively,
                   or only the signal pin (for sensors 3 pins), like:
                   Ultrasonic ultrasonic(13);
                */
                Ultrasonic ultrasonic(12, 13);
                int distance;
                int headHeight = 24; // in inches
                
                // motor stuff
                int enablePin = 8;
                int stepPin = 2;
                int dirPin = 5;
                int dir = LOW;
                int speed =1000; // Steps per second
                int dist = 12000; // Total number of steps to move (approx 6 feet)
                
                void setup() {
                  Serial.begin(9600);
                  // put your setup code here, to run once:
                  pinMode(enablePin,OUTPUT);
                  pinMode(stepPin,OUTPUT);
                  pinMode(dirPin,OUTPUT);
                
                  // do the code below to ensure the motor isn't trying to spin before
                  // we meet the conditions for deploying the spider
                  digitalWrite(enablePin,HIGH);
                  digitalWrite(stepPin, LOW);
                  digitalWrite(dirPin, dir);
                }
                
                int increment = 0;
                void loop() {  
                  distance = ultrasonic.read(INC);
                
                  Serial.print("Distance in Inches: ");
                  Serial.println(distance);
                  
                  // make an incrementor, increment goes up for each distance less than 24, if the increment reaches 5 (on i=5),
                  // deploy
                
                  if (distance < headHeight) {
                  
                  increment++;
                  Serial.println(increment);
                
                  // on the 5th read less than 24 inches, start the motor running code
                  if (increment == 5) {
                    Serial.println("deploy spider");
                    digitalWrite(enablePin, LOW);  // start motor
                    digitalWrite(dirPin,dir);
                    for (int i = 0; i < dist; i++) {
                      digitalWrite(stepPin, LOW);
                      delayMicroseconds(300);
                      digitalWrite(stepPin, HIGH);
                      delayMicroseconds(300);
                    }
                  
                    delay(10000);
                    dir = !dir;
                
                    for (int i = 0; i < dist; i++) {
                      digitalWrite(stepPin, LOW);
                      delayMicroseconds(500); // a bit slower when going up
                      digitalWrite(stepPin, HIGH);
                      delayMicroseconds(500);
                    }
                    digitalWrite(enablePin, HIGH); // disable motor after move
                    increment = 0;
                    delay(10000); // delay after spider has been deployed
                  }
                  } else{
                    Serial.println("dont deploy spider");
                  }
                  delay(5000);
                }