rachelhuang505
Published

robo dog

3-week project: four-legged robot that can autonomously explore its environment

Showcase (no instructions)411
robo dog

Things used in this project

Hardware components

SG90 Micro-servo motor
SG90 Micro-servo motor
×4
Arduino Mega 2560
Arduino Mega 2560
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

cad model

Code

code

C/C++
#include <Servo.h>
#include <NewPing.h>

#define TRIGGER_PIN 6
#define ECHO_PIN 7
#define MAX_DISTANCE 1000
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
NewPing ultrasonic(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
const int button1 = 8;
const int button2 = 9;
const int button3 = 10;
int button1state = LOW;
int button2state = LOW;
int button3state = LOW;
int stopcount = 0;
int timecheck = 0;
int speedincrement = 3;

void setup() {
  Serial.begin(9600);
  servo1.attach(2);
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
}

void loop() {

  int check = ultrasonic.ping();
  int distance = check/US_ROUNDTRIP_CM; //converts distance to cm
  Serial.println(distance);

  button1state = digitalRead(button1); //green
  button2state = digitalRead(button2); //red
  button3state = digitalRead(button3); //yellow

 if(button1state == 1){
  speedincrement += 2;
 }
 else if(button3state == 1){
  speedincrement -= 2;
 }
  
 if(distance <= 25){ //one of the stop commands: there is an obstacle
    while(timecheck <= 10000){
      timecheck++;
    }
    if(timecheck == 10000){ 
      freeze();
    }
  } 
  else if(stopcount >= 1){ //second stop command: second button is held
    freeze();
    stopcount++;
  }
  else if(button2state == 1){ //second stop command ends here
    stopcount = 0;
    speedincrement = 3;
  }
  else{ //base case: walking 
  timecheck = 0;
  walkForward(); 
  }
}

void walkForward(){
  for(int i = 40; i >= 0; i = i - speedincrement){
    delay(10);
    servo1.write(i);
    delay(10);
    servo3.write(120-(i+10));
    delay(10);
  }
   for(int i = 130; i >= 90; i = i - speedincrement){
    delay(10);
    servo2.write(i);
    delay(10);
    servo4.write(190-(i+15));
    delay(10);
  }
}

void freeze(){
  servo1.write(90);
  servo2.write(90);
  servo3.write(90);
  servo4.write(90);
}

Credits

rachelhuang505
3 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.