Advertisement
Guest User

Ze Complete Chaoz

a guest
Jan 30th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ZE Complete Chaoz
  2.  
  3. // Coded By: Vincent Arndt and Evan Finch
  4.  
  5. //This is maily created to have a servo rotate a platform that can hold any camera or such to rotate in the direction of movement.
  6.  
  7. #include <Servo.h> // Includes Servo Library
  8. Servo plat; // Gives the Servo a name
  9. int currentUSposition = 0;// Sets the original starting position
  10. int trigPin[] = {2,3,4,5,6}; // Puts all Trigger Pins from the Ultrasonic Sensors into a variable table
  11. int echoPin[] = {7,8,10,11,12}; // Puts all Echo Pins from the Ultrasonic Sensors into a variable table
  12. int currentUSpin = 8; // Sets the starting input pin
  13. int ultraPrev[] = {HIGH, HIGH, HIGH, HIGH, HIGH}; // Sets all previous Ultrasonic Sensors to "OFF"
  14. int USposition[] = {157,117.75,78.5,39.25,0}; // Assigns degrees to the Pins in the variable table
  15. int duration, distance; // Creates the distance and duration variable
  16. boolean USstatus; // Sets a boolean or randomized value to USstatus
  17.  
  18. void setup()
  19. {
  20.   Serial.begin(9600); // Starts the serial where communication is made in serial monitor
  21.   plat.attach(9); // Assigns the Servo Pin
  22.   pinMode (trigPin, OUTPUT); // Sets the Output
  23.   pinMode (echoPin, INPUT); // Sets the Input
  24.  
  25.   Serial.print("Calibrating Ultrasonic Sensors "); // Prints in serial monitor words incased {lines 18-27 are used from https://www.hackster.io/lindsi8784/motion-following-motorized-camera-base-61afeb?ref=user&ref_id=58889&offset=0}
  26.     for(int c = 0; c < 15; c++) // Creates a delay in order to fully calibrate the Ultrasonic Sensors
  27.       {
  28.         Serial.print("."); // Prints in serial monitor symbol incased
  29.         delay(1000); // Adds a 1 second delay per increase of "c"
  30.       }
  31.    Serial.println("Ultrasonic Sensors Ready"); // Prints in serial monitor words incased
  32.  
  33.   plat.write(78.5); // Sets the starting degrees for the servo to turn to if not already there
  34.  
  35. }
  36.  
  37. void loop()
  38. {
  39.   digitalWrite(trigPin, HIGH); // Sets the frequency to activate
  40.   delayMicroseconds (10); // Adds a microsecond delay
  41.   digitalWrite(trigPin, LOW); // Sets the frequency to deactivate
  42.   duration = pulseIn (echoPin,HIGH); // Uses the pulse received to create a duration
  43.   distance = (duration/2) / 29.1; // Creates a distance based off the duration read previously
  44.   if (distance >= 700 || distance <=0) // Creates a requirement pool
  45.   {
  46.     Serial.println("No object detected"); // If the requirements are met the serial monitor will output the message
  47.   }
  48.  
  49.  for (int US = 0; US < 5; US++) // Sets up reading through all Ultrasonic Sensors
  50.  {
  51.   currentUSpin = echoPin[US]; // sets variables to equal each other from varial table
  52.   USstatus = digitalRead (currentUSpin); // Uses boolean to set values to be used
  53.  
  54.   if(ultraPrev[US] == LOW) // Uses the opposite setup of state to initiate the rotation
  55.   {
  56.     if (currentUSposition != currentUSpin && ultraPrev[US] == LOW) // Set up requirement pool
  57.     {
  58.       plat.write(USposition[US]); // Sets the Servo to face a particular direction
  59.       Serial.println(USposition[US]); // Displays where the sound is being heard
  60.       Serial.println ("Distance:"); // Prints in serial monitor the message
  61.       Serial.print(distance); //  Prints the number being used as distance
  62.       delay(50); // Sets a small delay
  63.       currentUSposition = currentUSpin; // Changes the position of the Servo to match the pin registering noise
  64.       ultraPrev[US] = HIGH; // Sets the Pin sensing noise to a ON state
  65.     }
  66.     ultraPrev[US] = HIGH; // Sets Sensor to ON state
  67.     }
  68.   else // Creates a backup
  69.   {
  70.     ultraPrev[US] = LOW; // Sets Sensors to OFF state
  71.   }  
  72.  }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement