<!DOCTYPE aesl-source>
<network>


<!--list of global events-->


<!--list of constants-->


<!--show keywords state-->
<keywords flag="true"/>


<!--node thymio-II-->
<node nodeId="1" name="thymio-II"># 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 prox
	when prox.ground.delta[0] > 450 and prox.ground.delta[1] &lt; 400 do
		if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 200
			motor.right.target = 100
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] > 450 do
		if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 100
			motor.right.target = 200
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] &lt; 400 do
		if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 0
			motor.right.target = 0
			new_state[0] = 1
			new_state[1] = 1
			new_state[2] = 1
			new_state[3] = 1
		end
	end
	when prox.ground.delta[0] > 450 and prox.ground.delta[1] &lt; 400 do
		if state[0] == 1 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 150
			motor.right.target = 50
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] > 450 do
		if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 50
			motor.right.target = 150
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] &lt; 400 do
		if state[0] == 1 then
			motor.left.target = 0
			motor.right.target = 350
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] &lt; 400 do
		if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 350
			motor.right.target = -100
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] > 450 do
		if state[0] == 1 then
			motor.left.target = 50
			motor.right.target = 300
			new_state[0] = 0
			new_state[1] = 0
			new_state[2] = 0
			new_state[3] = 0
		end
	end
	when prox.ground.delta[0] > 450 and prox.ground.delta[1] &lt; 400 do
		if state[0] == 0 and state[1] == 1 and state[2] == 0 and state[3] == 0 then
			motor.left.target = 250
			motor.right.target = 50
			new_state[0] = 0
			new_state[1] = 0
			new_state[2] = 0
			new_state[3] = 0
		end
	end
	when prox.ground.delta[0] > 450 and prox.ground.delta[1] &lt; 400 do
		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] = 0
			motor.left.target = 200
			motor.right.target = 100
			call leds.top(32,0,0)
		end
	end
	when prox.ground.delta[0] &lt; 400 and prox.ground.delta[1] > 450 do
		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] = 0
			motor.left.target = 100
			motor.right.target = 200
			call leds.top(0,32,0)
		end
	end
	when prox.ground.delta[1] &lt; 400 do
		if state[2] == 1 and state[3] == 1 then
			motor.left.target = 200
			motor.right.target = 100
			new_state[0] = 0
			new_state[1] = 0
			new_state[2] = 0
			new_state[3] = 0
		end
	end

	call math.copy(state, new_state)
	callsub display_state

onevent buttons
	when button.right == 1 do
		if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
			motor.left.target = 400
			motor.right.target = 0
			new_state[0] = 0
			new_state[1] = 1
			new_state[2] = 0
			new_state[3] = 0
		end
	end
	when button.left == 1 do
		if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
			motor.left.target = 0
			motor.right.target = 500
			new_state[0] = 1
			new_state[1] = 0
			new_state[2] = 0
			new_state[3] = 0
		end
	end
	when button.forward == 1 do
		if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
			motor.left.target = 150
			motor.right.target = 150
			new_state[0] = 1
			new_state[1] = 1
			new_state[2] = 0
			new_state[3] = 0
		end
	end
	when button.backward == 1 do
		if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
			motor.left.target = -300
			motor.right.target = 300
			new_state[2] = 1
			new_state[3] = 1
		end
	end

	call math.copy(state, new_state)
	callsub display_state
<toolsPlugins>
<ThymioVisualProgramming>
<vplroot xml-format-version="1">
<program advanced_mode="1">
<set>
<block value0="1" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="2" value1="2" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="200" value1="100" type="action" name="move"/>
</set>
<set>
<block value0="2" value1="1" value2="400" value3="450" type="event" name="proxground"/>
<block value0="2" value1="2" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="100" value1="200" type="action" name="move"/>
</set>
<set>
<block value0="2" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="2" value1="2" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="0" value1="0" type="action" name="move"/>
<block value0="1" value1="1" value2="1" value3="1" type="action" name="setstate"/>
</set>
<set>
<block value0="0" value1="0" value2="0" value3="1" value4="0" type="event" name="button"/>
<block value0="1" value1="1" value2="1" value3="1" type="state" name="statefilter"/>
<block value0="400" value1="0" type="action" name="move"/>
<block value0="2" value1="1" value2="2" value3="2" type="action" name="setstate"/>
</set>
<set>
<block value0="0" value1="1" value2="0" value3="0" value4="0" type="event" name="button"/>
<block value0="1" value1="1" value2="1" value3="1" type="state" name="statefilter"/>
<block value0="0" value1="500" type="action" name="move"/>
<block value0="1" value1="2" value2="2" value3="2" type="action" name="setstate"/>
</set>
<set>
<block value0="1" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="1" value1="2" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="150" value1="50" type="action" name="move"/>
</set>
<set>
<block value0="2" value1="1" value2="400" value3="450" type="event" name="proxground"/>
<block value0="2" value1="1" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="50" value1="150" type="action" name="move"/>
</set>
<set>
<block value0="2" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="1" value1="0" value2="0" value3="0" type="state" name="statefilter"/>
<block value0="0" value1="350" type="action" name="move"/>
</set>
<set>
<block value0="2" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="2" value1="1" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="350" value1="-100" type="action" name="move"/>
</set>
<set>
<block value0="2" value1="1" value2="400" value3="450" type="event" name="proxground"/>
<block value0="1" value1="0" value2="0" value3="0" type="state" name="statefilter"/>
<block value0="50" value1="300" type="action" name="move"/>
<block value0="2" value1="2" value2="2" value3="2" type="action" name="setstate"/>
</set>
<set>
<block value0="1" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="2" value1="1" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="250" value1="50" type="action" name="move"/>
<block value0="2" value1="2" value2="2" value3="2" type="action" name="setstate"/>
</set>
<set>
<block value0="1" value1="0" value2="0" value3="0" value4="0" type="event" name="button"/>
<block value0="1" value1="1" value2="1" value3="1" type="state" name="statefilter"/>
<block value0="150" value1="150" type="action" name="move"/>
<block value0="1" value1="1" value2="2" value3="2" type="action" name="setstate"/>
</set>
<set>
<block value0="1" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="1" value1="1" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="2" value1="2" value2="2" value3="2" type="action" name="setstate"/>
<block value0="200" value1="100" type="action" name="move"/>
<block value0="32" value1="0" value2="0" type="action" name="colortop"/>
</set>
<set>
<block value0="2" value1="1" value2="400" value3="450" type="event" name="proxground"/>
<block value0="1" value1="1" value2="2" value3="2" type="state" name="statefilter"/>
<block value0="2" value1="2" value2="2" value3="2" type="action" name="setstate"/>
<block value0="100" value1="200" type="action" name="move"/>
<block value0="0" value1="32" value2="0" type="action" name="colortop"/>
</set>
<set>
<block value0="0" value1="0" value2="1" value3="0" value4="0" type="event" name="button"/>
<block value0="1" value1="1" value2="1" value3="1" type="state" name="statefilter"/>
<block value0="-300" value1="300" type="action" name="move"/>
<block value0="0" value1="0" value2="1" value3="1" type="action" name="setstate"/>
</set>
<set>
<block value0="0" value1="2" value2="400" value3="450" type="event" name="proxground"/>
<block value0="0" value1="0" value2="1" value3="1" type="state" name="statefilter"/>
<block value0="200" value1="100" type="action" name="move"/>
<block value0="2" value1="2" value2="2" value3="2" type="action" name="setstate"/>
</set>
<set>
<block value0="0" value1="0" value2="0" value3="0" type="state" name="statefilter"/>
</set>
</program>
</vplroot>
</ThymioVisualProgramming>
</toolsPlugins>
</node>


</network>
