Devices & Components
Arduino Nano
Solderless Breadboard Half Size
Jumper wires (generic)
SG90 Micro-servo motor
Ultrasonic Sensor - HC-SR04 (Generic)
Hardware & Tools
Hot glue gun (generic)
Software & Tools
Arduino Web Editor
Arduino IDE
Project description
Code
GoldScrew_EatingRobot.ino
arduino
1/* 2 * Cartboard eating Robot 3 * Author: GoldScrew 4 * Email: goldscrewdiy@gmail.com 5 * Description: It's using HC-SR04 (Detect food by distance <= 5 cm) and Server (to open and close the mouth) 6 */ 7 8#include <Servo.h> 9#define SERVO_PIN 9 // Set pin 9 for servo 10 11// HC-SR04 ultrasonic sensor 12const int trig = 3; // trig of HC-SR04 13const int echo = 2; // echo of HC-SR04 14 15// Servo 16Servo mouthServo; 17 18void setup() 19{ 20 // Serial connection with baudrate 960 21 Serial.begin(9600); 22 23 // Send signal with TRIG 24 pinMode(trig, OUTPUT); 25 26 // Receive signal with ECHO 27 pinMode(echo, INPUT); 28 29 // Mouth Server 30 mouthServo.attach(SERVO_PIN); 31 mouthServo.write(90); 32 33} 34void loop() 35{ 36 /* Duration */ 37 unsigned long duration; 38 int distance; //Distance 39 40 /* Send signal from TRIG pin */ 41 digitalWrite(trig, 0); //Stop trig pin 42 delayMicroseconds(2); //Delay 2 micro seconds 43 digitalWrite(trig, 1); //Send signal from TRIG pin 44 delayMicroseconds(10); //Delay 10 micro seconds 45 digitalWrite(trig, 0); //Stop trig pin 46 47 /* Measure HIGH pulse width at ECHO pin */ 48 duration = pulseIn(echo, HIGH); 49 50 //Calculate distance 51 distance = int(duration/2/29.412); 52 53 if(distance <= 5) 54 { 55 //Print distance 56 Serial.println("the distance is less than 5 cm"); 57 58 // Start open mouth 59 mouthServo.write(0); 60 61 // Delay 62 delay(1200); 63 64 } else { 65 66 //Print distance 67 Serial.println("the distance is more than 5 cm"); 68 69 // Close the mouth 70 mouthServo.write(90); 71 } 72 73 // Delay 74 delay(200); 75}
GoldScrew_EatingRobot.ino
arduino
1/* 2 * Cartboard eating Robot 3 * Author: GoldScrew 4 * Email: goldscrewdiy@gmail.com 5 6 * Description: It's using HC-SR04 (Detect food by distance <= 5 cm) and Server (to 7 open and close the mouth) 8 */ 9 10#include <Servo.h> 11#define SERVO_PIN 12 9 // Set pin 9 for servo 13 14// HC-SR04 ultrasonic sensor 15const int trig = 16 3; // trig of HC-SR04 17const int echo = 2; // echo of HC-SR04 18 19// 20 Servo 21Servo mouthServo; 22 23void setup() 24{ 25 // Serial connection with 26 baudrate 960 27 Serial.begin(9600); 28 29 // Send signal with TRIG 30 pinMode(trig, 31 OUTPUT); 32 33 // Receive signal with ECHO 34 pinMode(echo, INPUT); 35 36 37 // Mouth Server 38 mouthServo.attach(SERVO_PIN); 39 mouthServo.write(90); 40 41 42} 43void loop() 44{ 45 /* Duration */ 46 unsigned long duration; 47 48 int distance; //Distance 49 50 /* Send signal from TRIG pin */ 51 digitalWrite(trig, 52 0); //Stop trig pin 53 delayMicroseconds(2); //Delay 2 micro seconds 54 digitalWrite(trig, 55 1); //Send signal from TRIG pin 56 delayMicroseconds(10); //Delay 10 micro seconds 57 58 digitalWrite(trig, 0); //Stop trig pin 59 60 /* Measure HIGH pulse width 61 at ECHO pin */ 62 duration = pulseIn(echo, HIGH); 63 64 //Calculate distance 65 66 distance = int(duration/2/29.412); 67 68 if(distance <= 5) 69 { 70 //Print 71 distance 72 Serial.println("the distance is less than 5 cm"); 73 74 75 // Start open mouth 76 mouthServo.write(0); 77 78 // Delay 79 delay(1200); 80 81 82 } else { 83 84 //Print distance 85 Serial.println("the distance 86 is more than 5 cm"); 87 88 // Close the mouth 89 mouthServo.write(90); 90 91 } 92 93 // Delay 94 delay(200); 95}
Downloadable files
Parts connection diagram
Parts connection diagram

Comments
Only logged in users can leave comments