Introduction: 4 Leg - 8 Servo Arduino Walking Robot
#walking Robot #arduino #8 servo #bluetooth #android
Step 1: Components
1) micro servo 9g - 8pcs
2) Arduino nano - 1pcs
3) hc-04 bluetooth module
4) pin headers
5) pcb board
6) ultra sonic sensor
Step 2: Code , App and Sketch
Bluetooth module connections:
*arduino Rx to Bluetooth module Tx
*arduino Tx to Bluetooth module Rx
****************************************
Code:
****************************************
#include
int servoPin12 = 12; int servoPin11=11; int servoPin10=10; int servoPin9=9; int servoPin8=8; int servoPin7=7; int servoPin6=6; int servoPin5=5; const int trigPin = 3; const int echoPin = 4; int Autoflag=0; long duration; int distance;
String readString; Servo servo12; Servo servo11; Servo servo10; Servo servo9; Servo servo8; Servo servo7; Servo servo6; Servo servo5; void setup() { servo12.attach(servoPin12); servo11.attach(servoPin11); servo10.attach(servoPin10); servo9.attach(servoPin9); servo8.attach(servoPin8); servo7.attach(servoPin7); servo6.attach(servoPin6); servo5.attach(servoPin5); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); } void loop() { //rotate while(Serial.available()){ // the code here is for bluetooth delay(3); char c = Serial.read(); readString += c; } if(readString.length() > 0){ // when a certain button is pressed the reaction will be... Serial.println(readString); if(readString == "for") { //foward servo8.write(50);//8,7 step forward servo7.write(100); delay(300); servo12.write(60);//12,11 step foward servo11.write(140); delay(300); servo6.write(100);//6,5 step forward servo5.write(80);
delay(300); servo10.write(100);//10,9 step forward servo9.write(40); delay(300); //
servo12.write(130);//12,11 stay servo11.write(60); servo8.write(100);//8,7 stay servo7.write(180); servo6.write(50);//6,5 stay servo5.write(0); servo10.write(50);//10,9 step forward servo9.write(120); //
delay(300); // } if(readString == "back") { servo8.write(50);//8,7 step forward servo7.write(180); delay(300); servo12.write(60);//12,11 step foward servo11.write(60); delay(300); servo6.write(100);//6,5 step forward servo5.write(0);
delay(300); servo10.write(100);//10,9 step forward servo9.write(120); delay(300); //
servo12.write(130);//12,11 stay servo11.write(140); servo8.write(100);//8,7 stay servo7.write(100); servo6.write(50);//6,5 stay servo5.write(80); servo10.write(50);//10,9 step forward servo9.write(120); //
delay(300); } if(readString == "left")\ { servo8.write(50);//8,7 step forward servo7.write(100); delay(300); servo10.write(100);//10,9 step forward servo9.write(40); delay(300); // servo12.write(130);//12,11 stay servo11.write(60); servo6.write(50);//6,5 stay servo5.write(0); servo8.write(100);//8,7 stay servo7.write(180); servo10.write(50);//10,9 step forward servo9.write(120); delay(300); } if(readString == "right"){ servo12.write(60);//12,11 step foward servo11.write(140); delay(300); servo6.write(100);//6,5 step forward servo5.write(80); delay(300); // servo12.write(130);//12,11 stay servo11.write(60); servo6.write(50);//6,5 stay servo5.write(0); servo8.write(100);//8,7 stay servo7.write(180); servo10.write(50);//10,9 step forward servo9.write(120); delay(300); } if(readString == "auto") { Autoflag=1; while(Autoflag!=0) { if(readString == "offauto") { Autoflag=0; break; } // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); delay(200); if(distance>105) { //foward servo8.write(50);//8,7 step forward servo7.write(100); delay(300); servo12.write(60);//12,11 step foward servo11.write(140); delay(300); servo6.write(100);//6,5 step forward servo5.write(80);
delay(300); servo10.write(100);//10,9 step forward servo9.write(40); delay(300); //
servo12.write(130);//12,11 stay servo11.write(60); servo8.write(100);//8,7 stay servo7.write(180); servo6.write(50);//6,5 stay servo5.write(0); servo10.write(50);//10,9 step forward servo9.write(120); //
delay(300); // if(readString == "offauto") { Autoflag=0; break; } } if(distance<95) { servo8.write(50);//8,7 step forward servo7.write(180); delay(300); servo12.write(60);//12,11 step foward servo11.write(60); delay(300); servo6.write(100);//6,5 step forward servo5.write(0);
delay(300); servo10.write(100);//10,9 step forward servo9.write(120); delay(300); //
servo12.write(130);//12,11 stay servo11.write(140); servo8.write(100);//8,7 stay servo7.write(100); servo6.write(50);//6,5 stay servo5.write(80); servo10.write(50);//10,9 step forward servo9.write(120); //
delay(300); if(readString == "offauto") { Autoflag=0; break; } } if(distance<=105 && distance>=95) { servo12.write(130);//12,11 stay servo11.write(60); servo8.write(100);//8,7 stay servo7.write(180); servo6.write(50);//6,5 stay servo5.write(0); servo10.write(50);//10,9 step forward servo9.write(120); if(readString == "offauto") { Autoflag=0; break; } } if(distance>200) { servo8.write(50);//8,7 step forward servo7.write(100); delay(300); servo10.write(100);//10,9 step forward servo9.write(40); delay(300); // servo12.write(130);//12,11 stay servo11.write(60); servo6.write(50);//6,5 stay servo5.write(0); servo8.write(100);//8,7 stay servo7.write(180); servo10.write(50);//10,9 step forward servo9.write(120); delay(300); if(readString == "offauto") { Autoflag=0; break; } } readString=""; } } } readString=""; }