Skip to content

compter en unaire

MichARom edited this page Sep 11, 2018 · 6 revisions

Précédent - Suivre une ligne | Home | Suivant - Compter en modulo 8

Exercice 9.1:

Donnée:

Jusqu’à combien pouvons-nous compter sur le Thymio en utilisant la représentation unaire ?

Thymio VPL:

L'action est effectuée à chaque fois que l'on clique sur le bouton central.

Si photo indisponible nous contacter!
Cette première ligne permet de représenter le chiffre 1 sur le robot.

Si photo indisponible nous contacter!
Cette deuxième ligne permet de représenter le chiffre 2 sur le robot.

Si photo indisponible nous contacter!
Cette troisième ligne permet de représenter le chiffre 3 sur le robot.

Si photo indisponible nous contacter!
Cette quatrième ligne permet de représenter le chiffre 4 sur le robot.

Si photo indisponible nous contacter!
Cette ligne permet de revenir au chiffre 0, et donc de faire une boucle infinie 0,1,2,3,4,0,1,2,3,4...

Code:

# variables for state
var state[4] = [0,0,0,0]
var new_state[4] = [0,0,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)

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

	when button.center == 1 do
		if state[0] == 1 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
			new_state[0] = 1
			new_state[1] = 1
			emit pair_run 1
		end
	end

	when button.center == 1 do
		if state[0] == 1 and state[1] == 1 and state[2] == 0 and state[3] == 0 then
			new_state[0] = 1
			new_state[1] = 1
			new_state[3] = 1
			emit pair_run 2
		end
	end

	when button.center == 1 do
		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] = 1
			new_state[3] = 1
			emit pair_run 3
		end
	end

	when button.center == 1 do
		if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
			new_state[0] = 0
			new_state[1] = 0
			new_state[2] = 0
			new_state[3] = 0
			emit pair_run 4
		end
	end

	call math.copy(state, new_state)
	callsub display_state

Réponse:

4

Clone this wiki locally