Get in contact

posts/page

Clap Back Robot – Damien Kee

Jun 23, 2008

This robot samples any loud sounds and keeps track of the time between each. It then plays back the rhythm by tapping on a plastic container. The code is written in NXC and is a good example of how arrays can be used.

task main(){
int time[20]; //Take a maximum of 20 samples
long t0;
int i=0;
int x=1;
SetSensorSound(IN_1); /* Setup port 1 as sound sensor */
t0 = CurrentTick();/* Grab the current reading of the timer */

/* Loop while the difference between the current timer and the initial timer is less than 5 seconds */
while ((CurrentTick()-t0) < 5000) {
  if(Sensor(IN_1) < 30) {   /* If the sensors hears a loud sound, record the timer value */
    time[i] = CurrentTick()-t0;
    i++;
until(Sensor(IN_1) < 29); /* Wait until that sound has dissapated before looking for the next */
}

}

/* Start taps */
OnFwd(OUT_A, 100); Wait(75);
OnRev(OUT_A, 100); Wait(75);
Off(OUT_A);
for(x=1;x<20;x++) {
  if(time[x]==0){   /* If the difference is 0, that means that there are no more claps recorded */
  break;
}

/* Wait for the time difference between each clap don’t forget that tap also takes up a certain amount of time */
Wait( time[x]-time[x-1]-150);
OnFwd(OUT_A, 100);  Wait(75);
OnRev(OUT_A, 100); Wait(75);
Off(OUT_A);
}

Wait(1000);

}

Get in Contact