Skip to content

compter de 15 a 0

Fab1205 edited this page Sep 10, 2018 · 10 revisions

précédent | Home | suivant

Exercice 9.4

Compter de 15 à 0

Cette exercice a pour but de faire compter le Thymio de 15 à 0. Pour cela nous avons utilisé les états allumé et éteint pour compter en base 2.

onevent buttons
	when button.center == 1 do
		new_state[0] = 1
		new_state[1] = 1
		new_state[2] = 1
		new_state[3] = 1
		emit pair_run 0
	end

	call math.copy(state, new_state)
	callsub display_state


onevent mic
	if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
		new_state[0] = 1
		new_state[1] = 1
		new_state[2] = 1
		new_state[3] = 0
		emit pair_run 1
	end

	if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 0 then
		new_state[0] = 1
		new_state[1] = 1
		new_state[2] = 0
		new_state[3] = 1
		emit pair_run 2
	end

	if state[0] == 1 and state[1] == 1 and state[2] == 0 and state[3] == 1 then
		new_state[0] = 1
		new_state[1] = 1
		new_state[2] = 0
		new_state[3] = 0
		emit pair_run 3
	end

	if state[0] == 1 and state[1] == 1 and state[2] == 0 and state[3] == 0 then
		new_state[0] = 1
		new_state[1] = 0
		new_state[2] = 1
		new_state[3] = 1
		emit pair_run 4
	end

	if state[0] == 1 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
		new_state[0] = 1
		new_state[1] = 0
		new_state[2] = 1
		new_state[3] = 0
		emit pair_run 5
	end

	if state[0] == 1 and state[1] == 0 and state[2] == 1 and state[3] == 0 then
		new_state[0] = 1
		new_state[1] = 0
		new_state[2] = 0
		new_state[3] = 1
		emit pair_run 6
	end

	if state[0] == 1 and state[1] == 0 and state[2] == 0 and state[3] == 1 then
		new_state[0] = 1
		new_state[1] = 0
		new_state[2] = 0
		new_state[3] = 0
		emit pair_run 7
	end

	if state[0] == 1 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
		new_state[0] = 0
		new_state[1] = 1
		new_state[2] = 1
		new_state[3] = 0
		emit pair_run 8
	end

	if state[0] == 0 and state[1] == 1 and state[2] == 1 and state[3] == 0 then
		new_state[0] = 0
		new_state[1] = 1
		new_state[2] = 0
		new_state[3] = 1
		emit pair_run 9
	end

	if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 1 then
		new_state[0] = 0
		new_state[1] = 1
		new_state[2] = 0
		new_state[3] = 0
		emit pair_run 10
	end

	if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 0 then
		new_state[0] = 0
		new_state[1] = 0
		new_state[2] = 1
		new_state[3] = 1
		emit pair_run 11
	end

	if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
		new_state[0] = 0
		new_state[1] = 0
		new_state[2] = 1
		new_state[3] = 0
		emit pair_run 12
	end

	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] = 1
		emit pair_run 13
	end

	if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 1 then
		new_state[0] = 0
		new_state[1] = 0
		new_state[2] = 0
		new_state[3] = 0
		emit pair_run 14
	end

	if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
		new_state[0] = 1
		new_state[1] = 1
		new_state[2] = 1
		new_state[3] = 1
		emit pair_run 15
	end

	call math.copy(state, new_state)
	callsub display_state

VPL image 1