Skip to content

Instantly share code, notes, and snippets.

@elktros
Created August 3, 2019 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/1627f8d66db499f1f3085dde2cce5dee to your computer and use it in GitHub Desktop.
Save elktros/1627f8d66db499f1f3085dde2cce5dee to your computer and use it in GitHub Desktop.
Quadruped_Robot_ArduinoCodeforChat
// Include the Servo library
#include <Servo.h>
// Declare ultrasonic sensor pins
const int trigPin = 8;
const int echoPin = 10;
// Declare the Servo pin
int servoPin1 = 0;
int servoPin2 = 1;
int servoPin3 = 2;
int servoPin4 = 3;
int servoPin5 = 4;
int servoPin6 = 5;
int servoPin7 = 6;
int servoPin8 = 7;
int servoPinu = 11;
// servo objects
Servo Servo1,Servo2,Servo3,Servo4,Servo5,Servo6,Servo7,Servo8,Servou;
char Incoming_value ; //Variable for storing Incoming_value
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Servo1.attach(servoPin1);
Servo2.attach(servoPin3);
// Servo3.attach(servoPin5);
Servo4.attach(servoPin7);
Servo5.attach(servoPin2);
Servo6.attach(servoPin4);
Servo7.attach(servoPin6);
Servo8.attach(servoPin8);
Servou.attach(servoPinu);
}
void loop()
{
Serial.flush();
//Serial.println("i am talking");
if(Serial.available() > 0)
{
Incoming_value = Serial.read(); //Read the incoming data and store it into variable Incoming_value
//Serial.print(Incoming_value);
if(Incoming_value == 'h') //Checks whether value of Incoming_value
{
//Serial.print("connected");
waveHello();
}
else if(Incoming_value == 'c')// navigation
{
checkDistance();
}
else if(Incoming_value == 'p') ///fly
{
tellNo();
}
}
delay(1000);
}
//code to nod the head
void tellNo()
{
int cc=5;
while(cc>0)
{
Servou.write(20);
delay(350);
Servou.write(160);
delay(350);
cc--;
}
Servou.write(90);
delay(200);
}
//checks the distance of an object with the help of ultrasonic sensor
void checkDistance()
{
Servou.write(170);
delay(200);
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(10000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10000);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.println(cm);
Servou.write(90);
delay(200);
}
//code to wave hello
void waveHello()
{
int i=2;
while(i>0)
{
i--;
Servo8.write(50);
Servo7.write(50);
delay(1000);
Servo8.write(150);
Servo7.write(150);
delay(1000);
Servo8.write(30);
Servo4.write(30);
delay(200);
Servo4.write(120);
delay(200);
Servo4.write(30);
delay(200);
Servo4.write(120);
Servo4.write(30);
delay(200);
Servo4.write(120);
delay(200);
Servo4.write(30);
delay(200);
Servo4.write(90);
Servo8.write(150);
delay(1000);
Servo8.write(90);
Servo7.write(90);
delay(1000);
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment