Noah OrtegaJj OdellDan HeoJuan de CarcerTyler Brient
Published

Sk8board Collision Detector V0.01

With our top of the line collision detector, you won’t ever have to worry about bumping into large objects right in front of you ever again.

BeginnerShowcase (no instructions)3 hours534
Sk8board Collision Detector V0.01

Things used in this project

Hardware components

Buzzer, Piezo
Buzzer, Piezo
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1

Story

Read more

Schematics

Schematics

Code

Code

C/C++
#include "Ultrasonic.h"

Ultrasonic ultrasonic(25);

//defines notes
#define NOTE_PAUSE  3
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4_1 260
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

//tempo for song
float speed = 1.3;

// order of notes to be played
int melody[] = {
   NOTE_D4, NOTE_D4, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_D4, NOTE_F4, NOTE_G4,
   NOTE_C4, NOTE_C4, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_G4,
   NOTE_B3, NOTE_B3, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_B3, NOTE_F4, NOTE_G4,
   NOTE_AS3, NOTE_AS3, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_AS3, NOTE_F4, NOTE_G4,
   NOTE_D4, NOTE_D4, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_D4, NOTE_F4, NOTE_G4,
   NOTE_C4, NOTE_C4, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_G4,
   NOTE_B3, NOTE_B3, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_B3, NOTE_F4, NOTE_G4,
   NOTE_AS3, NOTE_AS3, NOTE_D5, NOTE_A4, NOTE_GS4, NOTE_G4, NOTE_F4, NOTE_AS3, NOTE_F4, NOTE_G4,
   
   NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_D5, NOTE_D5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_G5, NOTE_GS5,
   NOTE_G5, NOTE_F5, NOTE_D5, NOTE_F5, NOTE_G5,
   NOTE_F5, NOTE_F5, NOTE_F5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_C6, NOTE_A5,
   NOTE_D6, NOTE_D6, NOTE_D6, NOTE_A5, NOTE_D6, NOTE_C6};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  8, 8, 4, 2, 4, 4, 4, 8, 8, 8, 8, 8, 4, 2, 4, 4, 4, 8, 8, 8, 8, 8, 4, 2, 4, 4, 4, 8, 8, 8,
  8, 8, 4, 2, 4, 4, 4, 8, 8, 8, 8, 8, 4, 2, 4, 4, 4, 8, 8, 8, 8, 8, 4, 2, 4, 4, 4, 8, 8, 8,
  8, 8, 4, 2, 4, 4, 4, 8, 8, 8, 8, 8, 4, 2, 4, 4, 4, 8, 8, 8,
  8, 4, 4, 8, 2, 4, 2, 4, 4, 4, 8, 4, 8,
  16, 8, 8, 4, 8,
  4, 4, 8, 4, 8, 2, 8, 4,
  4, 8, 8, 8, 4, 1};


void setup() {                
  // initialize the digital pin as an output.
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(25, INPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  int offset = 20; // Offset in cm
  volatile long lastTime = 0, curTime = 0; //lastTime it buzzed, current time, 
  long beepTime = 100000000; //
  int lastDist = 100000000; //last distance from wall detected
  int count = 0; //counts up to escape
  boolean escape = false; //go to song if true, otherwise dont
  
  while(1)
  {
   long distance;
   distance = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
   //for debugging
   Serial.print(distance);//0~400cm
   Serial.println("------");

   //If it is not time to play the song and it is not the 0 that comes up every other time (only accepts 0 if the last measured was 0 instead of the varying actual_distance then 0)
   if(escape == false && ((distance < 280 && distance > 0) || (distance == 0 && lastDist == 0)))
   {   
       // Beeps with a varying time that is less than the current distance so if distance changes quickly it still stops beeping
       if(curTime - lastTime >= distance / 2)
       {
           
           digitalWrite(38, HIGH);   // turn the LED on (HIGH is the voltage level)
           //sets the last time it beeped to current time as it just beeped
           lastTime = curTime;
           //Beeps for one forth the distance, we decided this was a good length so that someone moving very fast will still have a gap between beeps and it wont play constantly
           beepTime = distance / 4;
           // for small beep times makes sure it is not 0
           if(beepTime <=4)
           {
               beepTime = 1;
           } 
           //Makes it not super long if its over 100
           else if(beepTime > 100)
           {
               beepTime = 100;
           }
           Serial.print("beep: ");
           Serial.println(beepTime);
       } 
       //Within a certain distance it contantly beeps
       else if(curTime - lastTime >= beepTime && distance >= 20)
       {
           digitalWrite(38, LOW);    // turn the LED off by making the voltage LOW
       }
       // Plays song if it is within crashing distance (arbitrarily chosen as 10) and is moving closer
       if((distance < 10 && lastDist > distance) || distance < 4)
       {
           count ++;
       }
       else{
           count = 0;
       } 
       // chooses to play song as count increased enough times
       if(count > 2)
       {
           escape = true;
           count = 0;
       }
       curTime ++;
   }
   //turns off beeper
   else if(escape == false)
   {
       digitalWrite(38, LOW);
       Serial.println("Skipped");
   }
   //plays the song
   else
   {
       int list_size;
       list_size = sizeof(melody) / sizeof(melody[0]);
       // goes through notes
       for (int thisNote = 0; thisNote < list_size; thisNote++) {

           // to calculate the note duration, take one second divided by the
           // note type. quarter note = 1000 / 4, eighth note = 1000/8, etc.
           int noteDuration = 1000/(noteDurations[thisNote]*speed);
           tone(38, melody[thisNote], noteDuration);

           int pauseBetweenNotes = noteDuration + 50/speed; // delay between pulse
           delay(pauseBetweenNotes);

           noTone(38); // stop the tone 
          // If we get far away from a wall (about 500 is where it can no longer detect things) it stops playing
           if(ultrasonic.MeasureInCentimeters() > 500)
           {
               escape = false;
               break;
           }
       }
   }
   //updates distance
   lastDist = distance;
  }
}

Credits

Noah Ortega
2 projects • 0 followers
Jj Odell
1 project • 0 followers
Dan Heo
1 project • 0 followers
Juan de Carcer
0 projects • 0 followers
Tyler Brient
0 projects • 0 followers

Comments