Skip to content

compter en binaire

Valentin Piquerez edited this page Sep 10, 2018 · 3 revisions

Précédent | Home | Suivant

Exercice 9.3

Donnée

Jusqu'à combien pouvons-nous compter avec Thymio en utilisant la représentation binaire (base 2) ?

Explication

Pour compter en binaire avec Thymio, il faut utiliser les états du dessus. Chaque état peut être sur 0(blanc) ou sur 1(orange). Le nombre maximum qu'on peut donc atteindre en comptant en binaire est 1111, c'est-à-dire 15 en base de 10.

Programme

Code

# variables for state
var state[4] = [0,0,0,0]
var new_state[4] = [0,0,0,0]

# setup threshold for detecting claps
mic.threshold = 250
# 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 mic
	if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
		new_state[0] = 1
		new_state[1] = 0
		new_state[2] = 0
		new_state[3] = 0
		emit pair_run 0
	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] = 0
		new_state[3] = 0
		emit pair_run 1
	end

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

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

	if state[0] == 0 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] = 1
		emit pair_run 4
	end

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

	if state[0] == 0 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] = 1
		emit pair_run 6
	end

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

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

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

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

	if state[0] == 1 and state[1] == 1 and state[2] == 1 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] = 1
		new_state[1] = 0
		new_state[2] = 1
		new_state[3] = 1
		emit pair_run 12
	end

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

	if state[0] == 0 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] = 1
		emit pair_run 14
	end

	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 15
	end

	call math.copy(state, new_state)
	callsub display_state
Clone this wiki locally