# variables for state var state[4] = [0,0,0,0] var new_state[4] = [0,0,0,0] # variables for notes var notes[6] var durations[6] var note_index = 6 var note_count = 6 var wave[142] var i var wave_phase var wave_intensity # compute a sinus wave for sound for i in 0:141 do wave_phase = (i-70)*468 call math.cos(wave_intensity, wave_phase) wave[i] = wave_intensity/256 end call sound.wave(wave) # stop timer 0 timer.period[0] = 0 # reset outputs call sound.system(-1) call leds.top(0,0,0) call leds.bottom.left(0,0,0) call leds.bottom.right(0,0,0) call leds.circle(0,0,0,0,0,0,0,0) # subroutine to display the current state sub display_state call leds.circle(0,state[1]*32,0,state[3]*32,0,state[2]*32,0,state[0]*32) # when a note is finished, play the next note onevent sound.finished if note_index != note_count then call sound.freq(notes[note_index], durations[note_index]) note_index += 1 end onevent prox when prox.ground.delta[0] >= 450 and prox.ground.delta[1] >= 450 do call leds.top(0,32,0) new_state[0] = 0 new_state[1] = 0 new_state[2] = 0 new_state[3] = 0 emit pair_run 0 end when prox.horizontal[0] <= 1000 and prox.horizontal[1] <= 1000 and prox.horizontal[2] <= 1000 and prox.horizontal[3] <= 1000 and prox.horizontal[4] <= 1000 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = 150 motor.right.target = 150 call leds.top(0,32,0) emit pair_run 1 end end when prox.horizontal[4] >= 2000 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = -50 motor.right.target = 200 call leds.top(0,32,0) emit pair_run 2 end end when prox.horizontal[0] >= 2000 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = 200 motor.right.target = -50 call leds.top(0,32,0) emit pair_run 3 end end when prox.horizontal[2] >= 2000 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = -250 motor.right.target = 0 call leds.top(0,32,0) emit pair_run 4 end end when prox.ground.delta[0] >= 450 and prox.ground.delta[1] <= 400 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = 0 motor.right.target = 0 timer.period[0] = 2000 new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 0 call math.copy(notes[0:5], [440, 524, 440, 370, 311, 370]) call math.copy(durations[0:5], [7, 7, 14, 7, 7, 14]) note_index = 1 note_count = 6 call sound.freq(notes[0], durations[0]) emit pair_run 5 end end when prox.ground.delta[0] <= 400 and prox.ground.delta[1] >= 450 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = 0 motor.right.target = 0 timer.period[0] = 2000 new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 0 call math.copy(notes[0:5], [440, 524, 440, 370, 311, 370]) call math.copy(durations[0:5], [7, 7, 14, 7, 7, 14]) note_index = 1 note_count = 6 call sound.freq(notes[0], durations[0]) emit pair_run 6 end end when prox.ground.delta[0] <= 400 and prox.ground.delta[1] <= 400 do if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then motor.left.target = 0 motor.right.target = 0 timer.period[0] = 2000 new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 0 call math.copy(notes[0:5], [440, 524, 440, 370, 311, 370]) call math.copy(durations[0:5], [7, 7, 14, 7, 7, 14]) note_index = 1 note_count = 6 call sound.freq(notes[0], durations[0]) emit pair_run 7 end end when prox.ground.delta[0] <= 400 and prox.ground.delta[1] <= 400 do if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 0 then motor.left.target = 150 motor.right.target = 150 call leds.top(0,0,32) emit pair_run 12 end end when prox.ground.delta[0] <= 400 and prox.ground.delta[1] >= 450 do if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 0 then motor.left.target = -50 motor.right.target = 100 call leds.top(0,0,32) emit pair_run 13 end end when prox.ground.delta[0] >= 450 and prox.ground.delta[1] <= 400 do if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 0 then motor.left.target = 100 motor.right.target = -50 call leds.top(0,0,32) emit pair_run 14 end end when prox.ground.delta[0] >= 450 and prox.ground.delta[1] >= 450 do if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 0 then new_state[0] = 0 new_state[1] = 0 new_state[2] = 0 new_state[3] = 0 motor.left.target = 150 motor.right.target = 150 call leds.top(0,0,32) emit pair_run 15 end end call math.copy(state, new_state) callsub display_state onevent timer0 timer.period[0] = 0 if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 0 then call leds.top(32,0,0) timer.period[0] = 1000 new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 1 emit pair_run 8 end if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 1 then call leds.top(26,32,0) timer.period[0] = 1000 new_state[0] = 0 new_state[1] = 1 new_state[2] = 1 new_state[3] = 1 emit pair_run 9 end if state[0] == 0 and state[1] == 1 and state[2] == 1 and state[3] == 1 then call leds.top(32,0,32) timer.period[0] = 1000 new_state[0] = 1 new_state[1] = 1 new_state[2] = 1 new_state[3] = 1 emit pair_run 10 end if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then call leds.top(0,0,32) new_state[0] = 0 new_state[1] = 0 new_state[2] = 1 new_state[3] = 0 motor.left.target = 150 motor.right.target = 150 emit pair_run 11 end call math.copy(state, new_state) callsub display_state