Components and supplies
Arduino Mega 2560
SG90 Micro-servo motor
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
3D Printer (generic)
Laser cutter (generic)
Apps and platforms
Arduino IDE
Project description
Code
code
c_cpp
1#include <Servo.h> 2#include <NewPing.h> 3 4#define TRIGGER_PIN 6 5#define ECHO_PIN 7 6#define MAX_DISTANCE 1000 7Servo servo1; 8Servo servo2; 9Servo servo3; 10Servo servo4; 11NewPing ultrasonic(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 12const int button1 = 8; 13const int button2 = 9; 14const int button3 = 10; 15int button1state = LOW; 16int button2state = LOW; 17int button3state = LOW; 18int stopcount = 0; 19int timecheck = 0; 20int speedincrement = 3; 21 22void setup() { 23 Serial.begin(9600); 24 servo1.attach(2); 25 servo2.attach(3); 26 servo3.attach(4); 27 servo4.attach(5); 28 pinMode(button1, INPUT); 29 pinMode(button2, INPUT); 30} 31 32void loop() { 33 34 int check = ultrasonic.ping(); 35 int distance = check/US_ROUNDTRIP_CM; //converts distance to cm 36 Serial.println(distance); 37 38 button1state = digitalRead(button1); //green 39 button2state = digitalRead(button2); //red 40 button3state = digitalRead(button3); //yellow 41 42 if(button1state == 1){ 43 speedincrement += 2; 44 } 45 else if(button3state == 1){ 46 speedincrement -= 2; 47 } 48 49 if(distance <= 25){ //one of the stop commands: there is an obstacle 50 while(timecheck <= 10000){ 51 timecheck++; 52 } 53 if(timecheck == 10000){ 54 freeze(); 55 } 56 } 57 else if(stopcount >= 1){ //second stop command: second button is held 58 freeze(); 59 stopcount++; 60 } 61 else if(button2state == 1){ //second stop command ends here 62 stopcount = 0; 63 speedincrement = 3; 64 } 65 else{ //base case: walking 66 timecheck = 0; 67 walkForward(); 68 } 69} 70 71void walkForward(){ 72 for(int i = 40; i >= 0; i = i - speedincrement){ 73 delay(10); 74 servo1.write(i); 75 delay(10); 76 servo3.write(120-(i+10)); 77 delay(10); 78 } 79 for(int i = 130; i >= 90; i = i - speedincrement){ 80 delay(10); 81 servo2.write(i); 82 delay(10); 83 servo4.write(190-(i+15)); 84 delay(10); 85 } 86} 87 88void freeze(){ 89 servo1.write(90); 90 servo2.write(90); 91 servo3.write(90); 92 servo4.write(90); 93}
code
c_cpp
1#include <Servo.h> 2#include <NewPing.h> 3 4#define TRIGGER_PIN 5 6 6#define ECHO_PIN 7 7#define MAX_DISTANCE 1000 8Servo servo1; 9Servo servo2; 10Servo 11 servo3; 12Servo servo4; 13NewPing ultrasonic(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 14const 15 int button1 = 8; 16const int button2 = 9; 17const int button3 = 10; 18int button1state 19 = LOW; 20int button2state = LOW; 21int button3state = LOW; 22int stopcount = 23 0; 24int timecheck = 0; 25int speedincrement = 3; 26 27void setup() { 28 Serial.begin(9600); 29 30 servo1.attach(2); 31 servo2.attach(3); 32 servo3.attach(4); 33 servo4.attach(5); 34 35 pinMode(button1, INPUT); 36 pinMode(button2, INPUT); 37} 38 39void loop() 40 { 41 42 int check = ultrasonic.ping(); 43 int distance = check/US_ROUNDTRIP_CM; 44 //converts distance to cm 45 Serial.println(distance); 46 47 button1state = 48 digitalRead(button1); //green 49 button2state = digitalRead(button2); //red 50 51 button3state = digitalRead(button3); //yellow 52 53 if(button1state == 1){ 54 55 speedincrement += 2; 56 } 57 else if(button3state == 1){ 58 speedincrement 59 -= 2; 60 } 61 62 if(distance <= 25){ //one of the stop commands: there is an 63 obstacle 64 while(timecheck <= 10000){ 65 timecheck++; 66 } 67 if(timecheck 68 == 10000){ 69 freeze(); 70 } 71 } 72 else if(stopcount >= 1){ //second 73 stop command: second button is held 74 freeze(); 75 stopcount++; 76 } 77 78 else if(button2state == 1){ //second stop command ends here 79 stopcount = 80 0; 81 speedincrement = 3; 82 } 83 else{ //base case: walking 84 timecheck 85 = 0; 86 walkForward(); 87 } 88} 89 90void walkForward(){ 91 for(int i 92 = 40; i >= 0; i = i - speedincrement){ 93 delay(10); 94 servo1.write(i); 95 96 delay(10); 97 servo3.write(120-(i+10)); 98 delay(10); 99 } 100 for(int 101 i = 130; i >= 90; i = i - speedincrement){ 102 delay(10); 103 servo2.write(i); 104 105 delay(10); 106 servo4.write(190-(i+15)); 107 delay(10); 108 } 109} 110 111void 112 freeze(){ 113 servo1.write(90); 114 servo2.write(90); 115 servo3.write(90); 116 117 servo4.write(90); 118}
Documentation
cad model
cad model
Comments
Only logged in users can leave comments