Components and supplies
HC-05 Bluetooth Module
Speaker: 3W, 4 ohms
RGB Diffused Common Cathode
ultrasonic sensor
Arduino UNO
Breadboard (generic)
Dual H-Bridge motor drivers L293D
Pololu round chassis kit
Slide Switch
Tools and machines
3D Printer (generic)
Project description
Code
theend.ino
arduino
1 2//notes I found the sounds on https://mycontraption.com/sound-effects-with-and-arduino/ 3const float note_C0 = 16.35; //C0 4const float note_Db0 = 17.32; //C#0/Db0 5const float note_D0 = 18.35; //D0 6const float note_Eb0 = 19.45; //D#0/Eb0 7const float note_E0 = 20.6; //E0 8const float note_F0 = 21.83; //F0 9const float note_Gb0 = 23.12; //F#0/Gb0 10const float note_G0 = 24.5; //G0 11const float note_Ab0 = 25.96; //G#0/Ab0 12const float note_A0 = 27.5; //A0 13const float note_Bb0 = 29.14; //A#0/Bb0 14const float note_B0 = 30.87; //B0 15const float note_C1 = 32.7; //C1 16const float note_Db1 = 34.65; //C#1/Db1 17const float note_D1 = 36.71; //D1 18const float note_Eb1 = 38.89; //D#1/Eb1 19const float note_E1 = 41.2; //E1 20const float note_F1 = 43.65; //F1 21const float note_Gb1 = 46.25; //F#1/Gb1 22const float note_G1 = 49; //G1 23const float note_Ab1 = 51.91; //G#1/Ab1 24const float note_A1 = 55; //A1 25const float note_Bb1 = 58.27; //A#1/Bb1 26const float note_B1 = 61.74; //B1 27const float note_C2 = 65.41; //C2 (Middle C) 28const float note_Db2 = 69.3; //C#2/Db2 29const float note_D2 = 73.42; //D2 30const float note_Eb2 = 77.78; //D#2/Eb2 31const float note_E2 = 82.41; //E2 32const float note_F2 = 87.31; //F2 33const float note_Gb2 = 92.5; //F#2/Gb2 34const float note_G2 = 98; //G2 35const float note_Ab2 = 103.83; //G#2/Ab2 36const float note_A2 = 110; //A2 37const float note_Bb2 = 116.54; //A#2/Bb2 38const float note_B2 = 123.47; //B2 39const float note_C3 = 130.81; //C3 40const float note_Db3 = 138.59; //C#3/Db3 41const float note_D3 = 146.83; //D3 42const float note_Eb3 = 155.56; //D#3/Eb3 43const float note_E3 = 164.81; //E3 44const float note_F3 = 174.61; //F3 45const float note_Gb3 = 185; //F#3/Gb3 46const float note_G3 = 196; //G3 47const float note_Ab3 = 207.65; //G#3/Ab3 48const float note_A3 = 220; //A3 49const float note_Bb3 = 233.08; //A#3/Bb3 50const float note_B3 = 246.94; //B3 51const float note_C4 = 261.63; //C4 52const float note_Db4 = 277.18; //C#4/Db4 53const float note_D4 = 293.66; //D4 54const float note_Eb4 = 311.13; //D#4/Eb4 55const float note_E4 = 329.63; //E4 56const float note_F4 = 349.23; //F4 57const float note_Gb4 = 369.99; //F#4/Gb4 58const float note_G4 = 392; //G4 59const float note_Ab4 = 415.3; //G#4/Ab4 60const float note_A4 = 440; //A4 61const float note_Bb4 = 466.16; //A#4/Bb4 62const float note_B4 = 493.88; //B4 63const float note_C5 = 523.25; //C5 64const float note_Db5 = 554.37; //C#5/Db5 65const float note_D5 = 587.33; //D5 66const float note_Eb5 = 622.25; //D#5/Eb5 67const float note_E5 = 659.26; //E5 68const float note_F5 = 698.46; //F5 69const float note_Gb5 = 739.99; //F#5/Gb5 70const float note_G5 = 783.99; //G5 71const float note_Ab5 = 830.61; //G#5/Ab5 72const float note_A5 = 880; //A5 73const float note_Bb5 = 932.33; //A#5/Bb5 74const float note_B5 = 987.77; //B5 75const float note_C6 = 1046.5; //C6 76const float note_Db6 = 1108.73; //C#6/Db6 77const float note_D6 = 1174.66; //D6 78const float note_Eb6 = 1244.51; //D#6/Eb6 79const float note_E6 = 1318.51; //E6 80const float note_F6 = 1396.91; //F6 81const float note_Gb6 = 1479.98; //F#6/Gb6 82const float note_G6 = 1567.98; //G6 83const float note_Ab6 = 1661.22; //G#6/Ab6 84const float note_A6 = 1760; //A6 85const float note_Bb6 = 1864.66; //A#6/Bb6 86const float note_B6 = 1975.53; //B6 87const float note_C7 = 2093; //C7 88const float note_Db7 = 2217.46; //C#7/Db7 89const float note_D7 = 2349.32; //D7 90const float note_Eb7 = 2489.02; //D#7/Eb7 91const float note_E7 = 2637.02; //E7 92const float note_F7 = 2793.83; //F7 93const float note_Gb7 = 2959.96; //F#7/Gb7 94const float note_G7 = 3135.96; //G7 95const float note_Ab7 = 3322.44; //G#7/Ab7 96const float note_A7 = 3520; //A7 97const float note_Bb7 = 3729.31; //A#7/Bb7 98const float note_B7 = 3951.07; //B7 99const float note_C8 = 4186.01; //C8 100const float note_Db8 = 4434.92; //C#8/Db8 101const float note_D8 = 4698.64; //D8 102const float note_Eb8 = 4978.03; //D#8/Eb8 103 int cont=0; 104 105 //ultrasonic sensors 106 const int trigPin = 2; 107const int echoPin = 4; 108 int forward; 109 long duration; 110 long cm=100; 111 112 113 int speakerPin = 3; // speaker connected to digital pin 9 114const int motor1Pin = 7; // H-bridge leg 1 (pin 2 of H bridge) 115const int motor2Pin = 8; // H-bridge leg 2 (pin 7of H bridge) 116const int enablePin = 5; // H-bridge enable pin 1 117const int enablePin2 = 6; // H-bridge enable pin 9 118const int motor21Pin = 9; // H-bridge leg 1 (pin 15 of H bridge 119const int motor22Pin = 10; // H-bridge leg 2 (pin 10 of bridge) 120 121 int led=12;//red terminal of rgb led 122 int led2=13;//blue terminal of rgb led 123int incomingByte; // a variable to read incoming serial data into 124 125 126#include <Servo.h> // 127 128Servo myservo; // 129 130 131int pos = 1500; 132 133 134void setup() { 135 Serial.begin(9600); // open serial port to receive data 136 myservo.attach(11); 137 pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output 138 // set all the other pins you're using as outputs: 139 pinMode(motor1Pin, OUTPUT); 140 pinMode(motor2Pin, OUTPUT); 141 pinMode(enablePin, OUTPUT); 142 pinMode(motor21Pin, OUTPUT); 143 pinMode(motor22Pin, OUTPUT); 144 pinMode(enablePin2, OUTPUT); 145 146 pinMode(led, OUTPUT); 147 pinMode(led2, OUTPUT); 148 // set enablePin high so that motor can turn on: 149 digitalWrite(enablePin, LOW); 150 digitalWrite(enablePin2, LOW); 151 152 r2D2(); //starting sound 153 delay(500); 154} 155 156void loop() { 157 158 159 if (Serial.available() > 0) { 160 // read the oldest byte in the serial buffer: 161 incomingByte = Serial.read(); 162 //up 163 if (incomingByte == '1') { 164 forward++; 165 if(forward%40==0){ 166 pinMode(trigPin, OUTPUT); 167 digitalWrite(trigPin, LOW); 168 delayMicroseconds(2); 169 digitalWrite(trigPin, HIGH); 170 delayMicroseconds(10); 171 digitalWrite(trigPin, LOW); 172 173 // Read the signal from the sensor: a HIGH pulse whose 174 // duration is the time (in microseconds) from the sending 175 // of the ping to the reception of its echo off of an object. 176 pinMode(echoPin, INPUT); 177 duration = pulseIn(echoPin, HIGH); 178 179 // convert the time into a distance 180 181 cm = microsecondsToCentimeters(duration);} 182 183 184 185 186 187 188 if(cm>15){ 189 digitalWrite(led, LOW);//change color of led 190 digitalWrite(led2, HIGH); 191 analogWrite(enablePin, 200); // turn on the motor 1 192 digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low 193 digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high 194 195 196 analogWrite(enablePin2, 200); // turn on second motor 197 digitalWrite(motor21Pin, LOW); // set leg 1 of the H-bridge low 198 digitalWrite(motor22Pin, HIGH); // set leg 2 of the H-bridge high 199 delay(10); 200 digitalWrite(enablePin, LOW); // turn off motor 1 201 digitalWrite(enablePin2, LOW); // turn off motor 2 202 }else 203 { pinMode(trigPin, OUTPUT); 204 digitalWrite(trigPin, LOW); 205 delayMicroseconds(2); 206 digitalWrite(trigPin, HIGH); 207 delayMicroseconds(10); 208 digitalWrite(trigPin, LOW); 209 210 // Read the signal from the sensor: a HIGH pulse whose 211 // duration is the time (in microseconds) from the sending 212 // of the ping to the reception of its echo off of an object. 213 pinMode(echoPin, INPUT); 214 duration = pulseIn(echoPin, HIGH); 215 216 // convert the time into a distance 217 218 cm = microsecondsToCentimeters(duration); 219 220 221 } 222 223 224 225 226 227 228 229 cont=0; 230 231 232 }//down 233 else if (incomingByte == '2') { 234 digitalWrite(led, LOW); //change color of led 235 digitalWrite(led2, HIGH); 236 analogWrite(enablePin, 200); // turn on the motor 1 237 digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high 238 digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low 239 240 241 analogWrite(enablePin2, 200); // turn on second motor 242 digitalWrite(motor21Pin, HIGH); // set leg 1 of the H-bridge high 243 digitalWrite(motor22Pin, LOW); // set leg 2 of the H-bridge low 244 245 delay(10); 246 digitalWrite(enablePin, LOW); // turn off motor 1 247 digitalWrite(enablePin2, LOW); // turn off motor 2 248 249 cont=0; 250 251 } //left 252 else if (incomingByte == '3') { 253 digitalWrite(led2, LOW); 254 digitalWrite(led, HIGH); 255 256 257 digitalWrite(enablePin2, HIGH); // turn on second motor 258 digitalWrite(motor21Pin, LOW); // set leg 1 of the H-bridge low 259 digitalWrite(motor22Pin, HIGH); // set leg 2 of the H-bridge high 260 delay(10); 261 262 digitalWrite(enablePin2, LOW); // turn off motor 2 263 264 265 cont=0; 266 267 } 268 269 //right 270 271 else if (incomingByte == '4') { 272 digitalWrite(led2, LOW); 273 digitalWrite(led, HIGH); 274 275 digitalWrite(enablePin, HIGH); // turn on motor 1 276 digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low 277 digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high 278 279 280 281 282 delay(10); 283 digitalWrite(enablePin, LOW); // turn off motor 1 284 285 cont=0; 286 287 288 } 289 290 //triangle 291 else if ((incomingByte == '9')&&(cont==0)) { 292 cont++; 293 ohhh(); 294 delay(500); 295 296 297 298 299 } 300 301 302 //x 303 else if ((incomingByte == '7')&&(cont==0)) { 304cont++; 305 306 catcall(); 307 delay(500); 308 309 310 311 312 } 313 314 else if (incomingByte == '8') { 315 316 317 pos=pos+35; 318 if(pos<2000){myservo.writeMicroseconds(pos);} 319 else{pos=2000;} 320 delay(10); 321 cont=0; 322 323 324 325 } 326 327 328 329 else if (incomingByte == 'A') { 330cont=0; 331 pos=pos-35; 332 if(pos>1000){myservo.writeMicroseconds(pos);} 333 else{pos=1000;} 334 delay(10); 335 336 337 338 339 } 340 341 342 343 344 345 346 347 348 349 350 } 351 352} 353 354 355 // functions made by https://mycontraption.com/sound-effects-with-and-arduino/ 356void beep (int speakerPin, float noteFrequency, long noteDuration) 357{ 358 int x; 359 // Convert the frequency to microseconds 360 float microsecondsPerWave = 1000000/noteFrequency; 361 // Calculate how many HIGH/LOW cycles there are per millisecond 362 float millisecondsPerCycle = 1000/(microsecondsPerWave * 2); 363 // Multiply noteDuration * number or cycles per millisecond 364 float loopTime = noteDuration * millisecondsPerCycle; 365 // Play the note for the calculated loopTime. 366 for (x=0;x<loopTime;x++) 367 { 368 digitalWrite(speakerPin,HIGH); 369 delayMicroseconds(microsecondsPerWave); 370 digitalWrite(speakerPin,LOW); 371 delayMicroseconds(microsecondsPerWave); 372 } 373} 374 375 376 377void r2D2(){ 378 beep(speakerPin, note_A7,100); //A 379 beep(speakerPin, note_G7,100); //G 380 beep(speakerPin, note_E7,100); //E 381 beep(speakerPin, note_C7,100); //C 382 beep(speakerPin, note_D7,100); //D 383 beep(speakerPin, note_B7,100); //B 384 beep(speakerPin, note_F7,100); //F 385 beep(speakerPin, note_C8,100); //C 386 beep(speakerPin, note_A7,100); //A 387 beep(speakerPin, note_G7,100); //G 388 beep(speakerPin, note_E7,100); //E 389 beep(speakerPin, note_C7,100); //C 390 beep(speakerPin, note_D7,100); //D 391 beep(speakerPin, note_B7,100); //B 392 beep(speakerPin, note_F7,100); //F 393 beep(speakerPin, note_C8,100); //C 394} 395 396 397 398 399void catcall() { 400 for (int i=1000; i<5000; i=i*1.05) { 401 beep(speakerPin,i,10); 402 } 403 delay(300); 404 405 for (int i=1000; i<3000; i=i*1.03) { 406 beep(speakerPin,i,10); 407 } 408 for (int i=3000; i>1000; i=i*.97) { 409 beep(speakerPin,i,10); 410 } 411} 412 413void ohhh() { 414 for (int i=1000; i<2000; i=i*1.02) { 415 beep(speakerPin,i,10); 416 } 417 for (int i=2000; i>1000; i=i*.98) { 418 beep(speakerPin,i,10); 419 } 420} 421 422void uhoh() { 423 for (int i=1000; i<1244; i=i*1.01) { 424 beep(speakerPin,i,30); 425 } 426 delay(200); 427 for (int i=1244; i>1108; i=i*.99) { 428 beep(speakerPin,i,30); 429 } 430} 431 432 433long microsecondsToCentimeters(long microseconds) 434{ 435 // The speed of sound is 340 m/s or 29 microseconds per centimeter. 436 // The ping travels out and back, so to find the distance of the 437 // object we take half of the distance travelled. 438 return microseconds / 29 / 2; 439} 440 441 442 443 444 445 446 447 448
Downloadable files
Schematics
As you can see I used a power bank as a power supply. The motors are driven by the H bridge. The speaker is controlled with PWM. And the RGB led is used to blink red and blue light.
Schematics
Schematics
As you can see I used a power bank as a power supply. The motors are driven by the H bridge. The speaker is controlled with PWM. And the RGB led is used to blink red and blue light.
Schematics
Comments
Only logged in users can leave comments
Anonymous user
6 years ago
Hello! I've been wondering what application you used for this robot? Also, where on the schematics do you place your ultrasonic sensors and how to connect the servo for the head