# Série 02, activité 2.3 "Les sauts d'humeur du rouge" # variables for state var state[4] = [0,1,0,0] var new_state[4] = [0,1,0,0] var nCompte = 0 # Compte le nombre de fois que le bouton "avance" a été appuyé. var anRouge[7] = 0, 32, 0, 8, 32, 8, 32 # noir, rouge, noir, moyen, rouge, moyen, rouge # 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 # Reset dans l'état de départ. new_state[0] = 0 new_state[1] = 1 new_state[2] = 0 new_state[3] = 0 call leds.top(0,0,0) nCompte = 0 end when button.forward == 1 do # Les lumières d'états "tournent en rond". if state[1] == 1 then new_state[1] = 0 new_state[3] = 1 elseif state[3] == 1 then new_state[2] = 1 new_state[3] = 0 elseif state[2] == 1 then new_state[0] = 1 new_state[2] = 0 elseif state[0] == 1 then new_state[0] = 0 new_state[1] = 1 end nCompte = nCompte + 1 if nCompte >= 7 then # La variable nCompte boucle de 0 à 6 nCompte = 0 end # Change l'intensité du rouge en fonction de la valeur de nCompte call leds.top(anRouge[nCompte],0,0) end # when button.forward == 1 do call math.copy(state, new_state) callsub display_state