.:: Motion Sensing Robot ::.
Robot that detects motion and runs away

This project is a robot that runs away from people while playing music. As of now I did not yet make the robot run away. For now it runs a demo program to demonstrate mobility. I am currently in the process of 3D printing blinders for the motion sensors. I am also waiting for the sound boards to arrive.
DOWNLOAD VIDEO

1: #include <servo-01.h>
2:
3: // SENSORS
4: int front_sensor()
5: {
6: int ret = 0;
7: if(input(pin_A0) == 1) {ret = 1;}
8: else {ret = 0;}
9: return ret;
10: }
11:
12: int right_sensor()
13: {
14: int ret = 0;
15: if(input(pin_A1) == 1) {ret = 1;}
16: else {ret = 0;}
17: return ret;
18: }
19:
20: int rear_sensor()
21: {
22: int ret = 0;
23: if(input(pin_A2) == 1) {ret = 1;}
24: else {ret = 0;}
25: return ret;
26: }
27:
28: int left_sensor()
29: {
30: int ret = 0;
31: if(input(pin_A3) == 1) {ret = 1;}
32: else {ret = 0;}
33: return ret;
34: }
35:
36: // MOTOR CONTROL
37: void left_servo_backward()
38: {
39: for (int i=0;i<3;i++)
40: {
41: output_high(PIN_B7);
42: delay_us(500);
43: output_low(PIN_B7);
44: delay_us(19500);
45: }
46: }
47:
48: void left_servo_forward()
49: {
50: for (int j=0;j<3;j++)
51: {
52: output_high(PIN_B7);
53: delay_us(2500);
54: output_low(PIN_B7);
55: delay_us(17500);
56: }
57: }
58:
59: void right_servo_backward()
60: {
61: for (int j=0;j<3;j++)
62: {
63: output_high(PIN_B6);
64: delay_us(2500);
65: output_low(PIN_B6);
66: delay_us(17500);
67: }
68: }
69:
70: void right_servo_forward()
71: {
72: for (int i=0;i<3;i++)
73: {
74: output_high(PIN_B6);
75: delay_us(500);
76: output_low(PIN_B6);
77: delay_us(19500);
78: }
79: }
80:
81: // MOTION CONTROL
82: void go_forward(int amount)
83: {
84: for(int i=0;i<amount;i++)
85: {
86: left_servo_forward();
87: right_servo_forward();
88: }
89: }
90:
91: void go_backward(int amount)
92: {
93: for(int i=0;i<amount;i++)
94: {
95: left_servo_backward();
96: right_servo_backward();
97: }
98: }
99:
100: void turn_left(int amount)
101: {
102: for(int i=0;i<amount;i++)
103: {
104: left_servo_backward();
105: right_servo_forward();
106: }
107: }
108:
109: void turn_right(int amount)
110: {
111: for(int i=0;i<amount;i++)
112: {
113: left_servo_forward();
114: right_servo_backward();
115: }
116: }
117:
118: // AUDIO
119: void play_music()
120: {
121: output_high(PIN_B0);
122: delay_ms(100);
123: output_low(PIN_B0);
124: }
125:
126: // MAIN LOOP
127: void main()
128: {
129: setup_lcd(LCD_DISABLED);
130:
131:
132: set_tris_a(0b11111111); // PORTA input
133: set_tris_b(0b00000000); // PORTB output
134:
135: delay_ms(5000); // WAIT FOR SENSORS TO CLEAR
136:
137: while(TRUE)
138: {
139:
140: /*
141: if(front_sensor() == 1)
142: {
143: play_music();
144: go_backward(50);
145: delay_ms(5000);
146: }
147: if(rear_sensor() == 1)
148: {
149: play_music();
150: go_forward(50);
151: delay_ms(5000);
152: }
153: if(left_sensor() == 1)
154: {
155: play_music();
156: turn_right(8);
157: go_forward(50);
158: delay_ms(5000);
159: }
160: if(right_sensor() == 1)
161: {
162: play_music();
163: turn_left(8);
164: go_forward(50);
165: delay_ms(5000);
166: }
167: */
168:
169: // DEMO 01
170: turn_left(32);
171: turn_right(32);
172: delay_ms(1000);
173:
174: // DEMO 02
175: go_forward(20);
176: go_backward(20);
177: turn_right(8);
178: turn_left(8);
179: turn_left(8);
180: turn_right(8);
181: delay_ms(1000);
182:
183: // DEMO 03
184: go_forward(20);
185: turn_left(8);
186: go_forward(20);
187: turn_left(8);
188: go_forward(20);
189: turn_left(8);
190: go_forward(20);
191: turn_left(8);
192: delay_ms(1000);
193:
194:
195: }
196:
197: }
198:
199: