Skip to content

code Morse

massteff edited this page Sep 20, 2018 · 13 revisions

Précédent | Home | Suivant

Ex 6.1 Code Morse

But de l'exercice

Écrivez un programme qui vous permette d’envoyer un code en Morse. Les lettres dans les codes Morse sont encodées par des séquences des sons longs (traits) et des sons courts (points). Par exemple, la lettre V est encodée par trois points suivis par un trait.

Comment procéder?

Nous allons demander à notre robot Thymio de dire Bonjour en code Morse. Malheureusement, nous ne disposons pas des traits utilisés dans le code Morse. Nous allons donc utiliser des noirs et des blanches pour déterminer les points et les traits respectivement.

Screenshots

Ex 6.1 - Code Morse

Code

# 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)
# 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)

# 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 buttons
	when button.forward == 1 do
		call math.copy(notes[0:4], [524, 0, 524, 524, 524])
		call math.copy(durations[0:4], [14, 14, 7, 7, 7])
		note_index = 1
		note_count = 5
		call sound.freq(notes[0], durations[0])
		emit pair_run 0
	end

	when button.right == 1 do
		call math.copy(notes[0:4], [524, 0, 524, 0, 524])
		call math.copy(durations[0:4], [14, 14, 14, 14, 14])
		note_index = 1
		note_count = 5
		call sound.freq(notes[0], durations[0])
		emit pair_run 1
	end

	when button.backward == 1 do
		call math.copy(notes[0:2], [524, 0, 524])
		call math.copy(durations[0:2], [14, 28, 7])
		note_index = 1
		note_count = 3
		call sound.freq(notes[0], durations[0])
		emit pair_run 2
	end

	when button.left == 1 do
		call math.copy(notes[0:4], [524, 0, 524, 524, 524])
		call math.copy(durations[0:4], [7, 14, 14, 14, 14])
		note_index = 1
		note_count = 5
		call sound.freq(notes[0], durations[0])
		emit pair_run 3
	end

	when button.center == 1 do
		call math.copy(notes[0:4], [524, 0, 524, 0, 524])
		call math.copy(durations[0:4], [14, 14, 14, 14, 14])
		note_index = 1
		note_count = 5
		call sound.freq(notes[0], durations[0])
		emit pair_run 4
	end


onevent prox
	when prox.horizontal[2] >= 2000 do
		call math.copy(notes[0:4], [524, 0, 524, 0, 524])
		call math.copy(durations[0:4], [7, 14, 14, 28, 7])
		note_index = 1
		note_count = 5
		call sound.freq(notes[0], durations[0])
		emit pair_run 6
	end


onevent tap
	call math.copy(notes[0:3], [524, 524, 0, 524])
	call math.copy(durations[0:3], [7, 7, 14, 14])
	note_index = 1
	note_count = 4
	call sound.freq(notes[0], durations[0])
	emit pair_run 5
Clone this wiki locally