@@ -50,7 +50,7 @@ namespace lights {
5050 stripNeopixel . setPixelColor ( _i , neopixel . colors ( NeoPixelColors . Black ) )
5151 }
5252 stripNeopixel . show ( )
53- if ( gigglebot . voltageBattery ( ) < 3400 ) {
53+ if ( gigglebot . voltageBattery ( ) < 3600 ) {
5454 eyeColorLeft = neopixel . colors ( NeoPixelColors . Red )
5555 eyeColorRight = neopixel . colors ( NeoPixelColors . Red )
5656 }
@@ -150,7 +150,7 @@ namespace remote {
150150 /**
151151 * In order to have a remote micro:bit control the GiggleBot, both of them
152152 * must be in the same radio group - or remote group. You can use either this
153- * block or the "radio set group" block found under Radio.
153+ * block or the "radio set group" block found under Radio.
154154 * The two blocks are the same thing.
155155 * Make sure your set of remote microbit and gigglebot is assigned a unique
156156 * group, especially if there are many gigglebot pairs around you.
@@ -181,7 +181,7 @@ namespace remote {
181181 powerLeft = Math . idiv ( ( powerLeft * - 1 * input . acceleration ( Dimension . Y ) ) , 512 ) + Math . idiv ( ( 50 * input . acceleration ( Dimension . X ) ) , 512 )
182182 powerRight = Math . idiv ( ( powerRight * - 1 * input . acceleration ( Dimension . Y ) ) , 512 ) - Math . idiv ( ( 50 * input . acceleration ( Dimension . X ) ) , 512 )
183183 // limit those values from -100 to 100
184- powerLeft = Math . min ( Math . max ( powerLeft , - 100 ) , 100 )
184+ powerLeft = Math . min ( Math . max ( powerLeft , - 100 ) , 100 )
185185 powerRight = Math . min ( Math . max ( powerRight , - 100 ) , 100 )
186186
187187 // Buffer is 8 bytes
@@ -191,20 +191,6 @@ namespace remote {
191191 radio . sendBuffer ( buf )
192192 }
193193
194- export let lastPacket : radio . RadioPacket ;
195- let initialized = false ;
196-
197- function remote_init ( ) {
198- if ( initialized ) return ;
199- initialized = true ;
200-
201- radio . onDataReceived ( ( ) => {
202- lastPacket = radio . RadioPacket . getPacket ( radio . readRawPacket ( ) ) ;
203- control . raiseEvent ( DAL . MICROBIT_ID_RADIO , radio . MAKECODE_RADIO_EVT_BUFFER ) ;
204- } )
205- }
206-
207-
208194 /**
209195 * Use this block on the GiggleBot to control it with a second micro:bit
210196 * @param radioBlock eg:1
@@ -215,11 +201,8 @@ namespace remote {
215201 //% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
216202 //% group="GiggleBot:"
217203 export function onRemoteControl ( cb : ( ) => void ) {
218- remote_init ( ) ;
219- control . onEvent ( DAL . MICROBIT_ID_RADIO , radio . MAKECODE_RADIO_EVT_BUFFER , ( ) => {
220- cb ( ) ;
221- } ) ;
222- }
204+ radio . onReceivedBuffer ( cb )
205+ } ;
223206
224207 /**
225208 * Put this block inside of the 'remotely controlled gigglebot' to follow all comands received via remote control.
@@ -229,12 +212,17 @@ namespace remote {
229212 //% weight=97
230213 //% group="GiggleBot:"
231214 export function remoteControlAction ( ) : void {
232- let powerLeft = lastPacket . bufferPayload . getNumber ( NumberFormat . Float32BE , 0 ) ;
233- let powerRight = lastPacket . bufferPayload . getNumber ( NumberFormat . Float32BE , 4 ) ;
234215
216+ if ( radio . lastPacket == null ) {
217+ return ;
218+ }
219+
220+ let powerLeft = radio . lastPacket . bufferPayload . getNumber ( NumberFormat . Float32BE , 0 ) ;
221+ let powerRight = radio . lastPacket . bufferPayload . getNumber ( NumberFormat . Float32BE , 4 ) ;
235222 gigglebot . setLeftPower ( powerLeft )
236- gigglebot . setRightPower ( powerRight )
223+ gigglebot . setRightPower ( powerRight ) ;
237224 gigglebot . motorPowerAssignBoth ( gigglebot . leftPower ( ) , gigglebot . rightPower ( ) )
225+ basic . pause ( 25 )
238226 }
239227
240228}
0 commit comments