Skip to content

avancer pendant 3s

Terry edited this page Sep 8, 2018 · 1 revision

Précédent | Home | Suivant

Exercice 7.1 : Avancer Pendant 3s

Donnée

Écrivez un programme qui fasse avancer le robot à vitesse maximale pour trois seconde lorsque que le bouton avant est touché ; puis qui fasse reculer le robot. Ajoutez une paire événement-actions qui arrête le mouvement en touchant le bouton central.

Thymio VPL

Configuration

Lorsque le bouton avant est appuyé, Thymio commence à avancer, créé un état et un timer démarre. Lorsque le timer prend fin, si l'état est activé alors Thymio recule. Si le bouton central est appuyé, l'état est réinitialisé pour empêcher tout futur recul, et Thymio s'arrête.

Code

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

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

onevent buttons
	when button.forward == 1 do
		timer.period[0] = 3000
		motor.left.target = 500
		motor.right.target = 500
		new_state[0] = 1
		emit pair_run 0
	end

	when button.center == 1 do
		motor.left.target = 0
		motor.right.target = 0
		new_state[0] = 0
		emit pair_run 2
	end

	call math.copy(state, new_state)
	callsub display_state


onevent timer0
	timer.period[0] = 0
	if state[0] == 1 then
		motor.left.target = -500
		motor.right.target = -500
		emit pair_run 1
	end
Clone this wiki locally