Thymio, exemple de code

Les lignes qui suivent sont utiles pour avoir des exemples de code en mode de programmation texte.
Ce code a été généré en mode VPL avancé, des commentaires ont été ajoutés.
# variables pour les 4 états représentés par 4 lumières
var state[4] = [0,0,0,0]
var new_state[4] = [0,0,0,0]

# variable for angle
var angle

# variables for notes
var notes[6]
var durations[6]
var note_index = 6
var note_count = 6
var wave[142]
var i
var wave_phase
var wave_intensity

# compute a sinus wave for sound
for i in 0:141 do
 wave_phase = (i-70)*468
 call math.cos(wave_intensity, wave_phase)
 wave[i] = wave_intensity/256
end
call sound.wave(wave)
# stop timer 0
timer.period[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)

# when a note is finished, play the next note
onevent sound.finished
#=====================
 if note_index != note_count then
  call sound.freq(notes[note_index], durations[note_index])
  note_index += 1
 end

# Traitement des boutons
onevent buttons
#==============
 when button.center == 1 do
  new_state[0] = 0  # Variables d'état
  new_state[1] = 0  # "callsub display_state" doit être appelé pour avoir un effet c.f. 7 lignes plus haut
  new_state[2] = 0
  new_state[3] = 0
  motor.left.target = 0  # Arrête le moteur gauche
  motor.right.target = 0  # Arrête le moteur droit
  call leds.top(32,32,32)  # Allume les LEDs du haut
  call leds.bottom.left(32,32,32)  # Allume les LEDs du bas gauche
  call leds.bottom.right(32,32,32)  # Allume les LEDs du bas droit
  call math.copy(notes[0:4], [440, 311, 440, 311, 524])
  call math.copy(durations[0:4], [14, 14, 14, 7, 7])
  note_index = 1
  note_count = 5
  call sound.freq(notes[0], durations[0])  # émet des sons
  timer.period[0] = 1500  # Lance le timer
  emit pair_run 0  # Une information durant le développement, pas essentiel
 end

 when button.forward == 1 do
  if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
   new_state[1] = 1
   new_state[3] = 1
   call leds.top(32,0,0)
   call leds.bottom.left(0,32,0)
   call leds.bottom.right(0,32,0)
   emit pair_run 1
  end
 end

 when button.right == 1 do
  if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 1 then
   new_state[1] = 0
   new_state[2] = 1
   new_state[3] = 1
   call leds.top(0,32,32)
   emit pair_run 2
  end
 end

 when button.backward == 1 do
  if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
   new_state[0] = 1
   new_state[2] = 1
   new_state[3] = 0
   call leds.bottom.left(32,32,0)
   call leds.bottom.right(32,32,0)
   emit pair_run 3
  end
 end

 when button.left == 1 do
  if state[0] == 1 and state[1] == 0 and state[2] == 1 and state[3] == 0 then
   new_state[0] = 0
   new_state[2] = 0
   call leds.bottom.left(32,0,32)
   call leds.bottom.right(32,0,32)
   emit pair_run 4
  end
 end

 when button.left == 1 and button.right == 1 do
  if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
   new_state[1] = 1
   new_state[3] = 1
   motor.left.target = 100
   motor.right.target = 100
   timer.period[0] = 1000
   call math.copy(notes[0:5], [440, 524, 440, 370, 311, 370])
   call math.copy(durations[0:5], [7, 7, 14, 7, 7, 14])
   note_index = 1
   note_count = 6
   call sound.freq(notes[0], durations[0])
   emit pair_run 5
  end
 end

 when button.forward == 1 and button.backward == 1 do
  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 7
  end
 end

 call math.copy(state, new_state)
 callsub display_state

# Réagit aux capteurs de proximités (capteur de lumière)
onevent prox
#===========
 when prox.horizontal[0] >= 2000 do
  # Capteur avant gauche
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.top(32,0,0)
   emit pair_run 8
  end
 end

 when prox.horizontal[1] >= 2000 do
 # Capteur entre avant gauche et devant
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.top(0,32,0)
   emit pair_run 9
  end
 end

 when prox.horizontal[2] >= 2000 do
  # Capteur de devant
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.top(0,0,32)
   emit pair_run 10
  end
 end

 when prox.horizontal[3] <= 1000 do
 # Capteur entre avant droite et devant
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.bottom.left(32,0,0)
   call leds.bottom.right(32,0,0)
   emit pair_run 11
  end
 end

 when prox.horizontal[4] <= 1000 do
  # Capteur avant droite
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.bottom.left(0,32,0)
   call leds.bottom.right(0,32,0)
   emit pair_run 12
  end
 end

 when prox.ground.delta[0] >= 450 do
  # Capteur du bas gauche
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.top(0,0,0)
   call leds.bottom.left(0,32,32)
   call leds.bottom.right(0,32,32)
   emit pair_run 13
  end
 end

 when prox.ground.delta[1] <= 400 do
  # Capteur du bas droite
  if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.bottom.left(32,32,32)
   call leds.bottom.right(32,32,32)
   emit pair_run 14
  end
 end

# Réagit au timer = au minuteur, à l'horloge (chronomètre)
onevent timer0
#=============
 timer.period[0] = 0
 if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
  call math.copy(notes[0:2], [440, 524, 440])
  call math.copy(durations[0:2], [7, 7, 7])
  note_index = 1
  note_count = 3
  call sound.freq(notes[0], durations[0])
  call leds.top(0,0,0)
  call leds.bottom.left(0,0,0)
  call leds.bottom.right(0,0,0)
  emit pair_run 6
 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] = 0
  new_state[2] = 1
  new_state[3] = 1
  motor.left.target = 250
  motor.right.target = -300
  timer.period[0] = 1000
  emit pair_run 15
 end

 if state[0] == 0 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
  motor.left.target = 0
  motor.right.target = 0
  call math.copy(notes[0:4], [262, 311, 370, 440, 524])
  call math.copy(durations[0:4], [7, 7, 7, 7, 7])
  note_index = 1
  note_count = 5
  call sound.freq(notes[0], durations[0])
  emit pair_run 16
 end

# Réagit lorsqu'on tape sur le Thymio
onevent tap
#==========
 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] = 0
  new_state[3] = 1
  call leds.bottom.left(0,0,32)
  call leds.bottom.right(0,0,32)
  emit pair_run 17
 end

 call math.copy(state, new_state)
 callsub display_state

# Réagit au son capté par le microphone
onevent mic
#==========
 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] = 0
  call leds.top(32,32,32)
  call leds.bottom.left(32,32,32)
  call leds.bottom.right(32,32,32)
  emit pair_run 18
 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] = 0
  new_state[2] = 1
  new_state[3] = 1
  call leds.top(0,0,0)
  call leds.bottom.left(0,0,0)
  call leds.bottom.right(0,0,0)
  emit pair_run 19
 end

 call math.copy(state, new_state)
 callsub display_state

# Test d'inclinaison du Thymio (accéléromètre)
onevent acc
#==========
 call math.atan2(angle, acc[0], acc[2])
 when angle > -9557 and angle < -6827 do
  # penché sur la gauche 
  if state[0] == 1 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
   call leds.top(0,32,0)
   emit pair_run 20
  end
 end

 call math.atan2(angle, acc[0], acc[2])
 when angle > -1365 and angle < 1365 do
  # penché sur la droite 
  if state[0] == 0 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
   call leds.bottom.left(0,0,32)
   call leds.bottom.right(0,0,32)
   emit pair_run 21
  end
 end

 call math.atan2(angle, acc[1], acc[2])
 when angle > -6800 and angle < -4000 do
  # penché vers l'avant
  if state[0] == 1 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
   call leds.top(0,32,32)
   emit pair_run 22
  end
 end

 call math.atan2(angle, acc[1], acc[2])
 when angle > 4000 and angle < 7000 do
  # penché vers l'arrière
  if state[0] == 1 and state[1] == 0 and state[2] == 1 and state[3] == 1 then
   call leds.top(32,32,0)
   emit pair_run 23
  end
 end

 call math.copy(state, new_state)
 callsub display_state


Plan du Site : Home   arrow   microcontroleur.html ( = http://www.juggling.ch/gisin/microcontroleur/microcontroleur.html )


Page mise à jour le 30 septembre 2017 par Bernard Gisin     ( Envoyer un e-mail )
Hébergement par : www.infomaniak.ch