Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ZE Complete Chaoz
- // Coded By: Vincent Arndt and Evan Finch
- //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.
- #include <Servo.h> // Includes Servo Library
- Servo plat; // Gives the Servo a name
- int currentUSposition = 0;// Sets the original starting position
- int trigPin[] = {2,3,4,5,6}; // Puts all Trigger Pins from the Ultrasonic Sensors into a variable table
- int echoPin[] = {7,8,10,11,12}; // Puts all Echo Pins from the Ultrasonic Sensors into a variable table
- int currentUSpin = 8; // Sets the starting input pin
- int ultraPrev[] = {HIGH, HIGH, HIGH, HIGH, HIGH}; // Sets all previous Ultrasonic Sensors to "OFF"
- int USposition[] = {157,117.75,78.5,39.25,0}; // Assigns degrees to the Pins in the variable table
- int duration, distance; // Creates the distance and duration variable
- boolean USstatus; // Sets a boolean or randomized value to USstatus
- void setup()
- {
- Serial.begin(9600); // Starts the serial where communication is made in serial monitor
- plat.attach(9); // Assigns the Servo Pin
- pinMode (trigPin, OUTPUT); // Sets the Output
- pinMode (echoPin, INPUT); // Sets the Input
- 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}
- for(int c = 0; c < 15; c++) // Creates a delay in order to fully calibrate the Ultrasonic Sensors
- {
- Serial.print("."); // Prints in serial monitor symbol incased
- delay(1000); // Adds a 1 second delay per increase of "c"
- }
- Serial.println("Ultrasonic Sensors Ready"); // Prints in serial monitor words incased
- plat.write(78.5); // Sets the starting degrees for the servo to turn to if not already there
- }
- void loop()
- {
- digitalWrite(trigPin, HIGH); // Sets the frequency to activate
- delayMicroseconds (10); // Adds a microsecond delay
- digitalWrite(trigPin, LOW); // Sets the frequency to deactivate
- duration = pulseIn (echoPin,HIGH); // Uses the pulse received to create a duration
- distance = (duration/2) / 29.1; // Creates a distance based off the duration read previously
- if (distance >= 700 || distance <=0) // Creates a requirement pool
- {
- Serial.println("No object detected"); // If the requirements are met the serial monitor will output the message
- }
- for (int US = 0; US < 5; US++) // Sets up reading through all Ultrasonic Sensors
- {
- currentUSpin = echoPin[US]; // sets variables to equal each other from varial table
- USstatus = digitalRead (currentUSpin); // Uses boolean to set values to be used
- if(ultraPrev[US] == LOW) // Uses the opposite setup of state to initiate the rotation
- {
- if (currentUSposition != currentUSpin && ultraPrev[US] == LOW) // Set up requirement pool
- {
- plat.write(USposition[US]); // Sets the Servo to face a particular direction
- Serial.println(USposition[US]); // Displays where the sound is being heard
- Serial.println ("Distance:"); // Prints in serial monitor the message
- Serial.print(distance); // Prints the number being used as distance
- delay(50); // Sets a small delay
- currentUSposition = currentUSpin; // Changes the position of the Servo to match the pin registering noise
- ultraPrev[US] = HIGH; // Sets the Pin sensing noise to a ON state
- }
- ultraPrev[US] = HIGH; // Sets Sensor to ON state
- }
- else // Creates a backup
- {
- ultraPrev[US] = LOW; // Sets Sensors to OFF state
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement