static PT_THREAD (protothread_compute_balls(struct pt *pt)) { PT_BEGIN(pt); while(1) { begin_time = PT_GET_TIME(); For each ball i from 1 to n For each ball j from i+1 to n Compute approximate rij by checking: if Δx and Δy are both less than 2 if (||rij||2 less than (2*(ballRadius))2 and hitCounter is zero) Compute vij Compute Δvi Add Δvi to vi Subtract Δvi from vj Set hitCounter big enough to avoid particle capture elseif (hitCounter>0) decrement hitCounter endif endif endfor endfor For each ball i from 1 to n erase ball i update ball i velocity using drag and wall collisions update position draw (or recycle) ball i endfor Inject new balls into playing field (if any) Check for user interaction by reading ADC and buttons (if any) // 15 fps => frame time of 67 mSec PT_YIELD_TIME_msec(67 - (PT_GET_TIME() - begin_time)) ; } // end while PT_END(pt); } // end thread