Cyfres Gweithdy Micro:Bit

English

Cyfres Gweithdy Micro:Bit Prifysgol Aberystwyth

Rydym wedi addasu'r deunydd a gafodd ei greu ar gyfer ein Clwb Roboteg fel bod pawb yn gallu ei ddefnyddio gartref neu yn yr ysgol.

Mae pob un o'r sesiynau isod yn cynnwys gwers fideo gan aelod o'n staff a set o ymarferion i asesu eich dealltwriaeth. Gan fod y dudalen hon ar gyfer dysgwyr annibynnol, mae'r atebion wedi'u cynnwys.

Ar gyfer athrawon ac arweinwyr grwpiau, cyn bo hir rydym yn gobeithio creu pecynnau o'r adnoddau hyn a fydd ar gael yn rhad ac am ddim yn ein Siop TES i'w defnyddio gan ysgolion a chlybiau STEM.

Dewiswch bennawd sesiwn i gychwyn.

Gwers Fideo (yn Saesneg):

Ymarferion:

Rydym wedi creu her ar dair lefel (efydd, arian ac aur) er mwyn i ddysgwyr ddechrau ymchwilio a chreu rhaglenni yn Microsoft Makecode.

Dylai pob dysgwr ymgymryd â'r her efydd i gychwyn cyn rhoi cynnig ar yr heriau uwch.

Cliciwch ar bennawd pob her i gael y manylion.


Creu rhaglen (yn Makecode) sy'n gwneud y canlynol:

  1. Pan fydd y rhaglen yn dechrau, rhaid sicrhau bod y LEDs yn dangos delwedd o'ch dewis.

    Gair o gyngor: I ddileu blociau, pwyswch y botwm dileu ar eich bysellfwrdd neu llusgwch y blociau yn ôl i'r ddewislen.


    • Gallwch ddefnyddio delwedd sy'n bodoli eisoes gyda'r bloc 'dangos eicon' neu greu eich delwedd eich hun gan ddefnyddio'r bloc 'dangos leds'.
    • Os ydych chi'n defnyddio'r bloc 'dangos leds', gallwch glicio ar y sgwariau i ddewis a ydyn nhw'n cael eu goleuo ai peidio.
    • Byddwch ond eisiau gweld y nodwedd hon pan fydd y rhaglen yn dechrau, felly dylech ei rhoi yn y bloc 'ar ddechrau'.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.

    Defnyddiwch y bloc 'dangos leds':

    Y tu mewn i'r bloc 'ar ddechrau' y mae bloc 'dangos leds' gyda goleuadau wedi'u dewis ar ffurf haul. Mae'r bloc 'am byth' yn aros yn wag.

    Defnyddiwch y bloc 'show eicon':

    Y tu mewn i'r bloc 'ar ddechrau' y mae bloc 'dangos eicon', ac mae eicon ysbryd wedi'i ddewis. Mae'r bloc 'am byth' yn aros yn wag.

  2. Ar ôl cwblhau'r cam cychwyn, dylech ddefnyddio'ch rhaglen i chwarae animeiddiad drosodd a throsodd nes eich bod yn stopio'r rhaglen.

    Gair o gyngor: Cyfres o ddelweddau yw animeiddiad sy'n gwneud i wrthrych edrych fel petai'n symud.


    • Bydd angen i chi ddefnyddio'r bloc 'am byth' i wneud hyn.
    • Gellid defnyddio dwy ddelwedd yn unig i greu animeiddiad syml.
    • Er enghraifft, gallwch ddefnyddio'r bloc 'dangos eicon' ar gyfer y galon fawr ac yna'r galon fach. Bydd hyn yn creu animeiddiad sy'n edrych fel calon yn curo.
    • Does dim angen saib ar gyfer cyfres o ddelweddau sy'n gwneud animeiddiad oni bai eich bod am arafu'r lluniau.
    • Gallwch ddefnyddio 'dangos leds', 'dangos eicon', neu gyfuniad o'r ddau.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Erbyn hyn, mae gan y bloc 'am byth' dri bloc 'dangos leds'. Mae'r bloc cyntaf yn dangos un golau ar y chwith pellaf, mae'r ail yn dangos yr un golau ynghyd â golau yn y canol, ac mae'r trydydd yn dangos yr un goleuadau â'r ail ynghyd â golau ar y dde bellaf.

    Mae hyn yn dangos nad oes angen i chi ddefnyddio llawer o LEDs i greu animeiddiad.


  3. Gofalwch fod delwedd wahanol yn ymddangos ar eich Micro:Bit wrth bwyso botwm A.

    Gair o gyngor: Mae botwm A yn fewnbwn, felly chwiliwch am y ddewislen sydd â'r bloc sydd ei angen arnoch.

    Gair o gyngor: I brofi'r rhaglen, dydy'r botwm A ddim yn cyfateb i'r botwm A ar eich bysellfwrdd. Mae'n cyfeirio at y botwm A ar Micro:Bit y tu mewn i'r efelychydd, a gallwch glicio arno gyda'ch llygoden.


    • Mae'r bloc y bydd ei angen arnoch ar gael yn y ddewislen bloc Mewnbwn.
    • Dydy'r bloc hwn ddim yn mynd y tu mewn i'r blociau 'ar ddechrau' neu 'am byth'. Yn hytrach, mae'n dechrau adran newydd o flociau cod.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae yna floc pinc/porffor 'ar fotwm A wedi'i wasgu' yn y rhaglen sy'n cynnwys bloc 'dangos eicon' gyda delwedd o jiráff wedi'i dewis.

  4. Ychwanegwch sain neu gerddoriaeth at eich dilyniant cychwyn.

    Gair o gyngor: Dylech sicrhau bod lefel sŵn eich cyfrifiadur yn isel cyn ychwanegu sain. Gallwch addasu'r sŵn yn ystod y prawf i lefel o'ch dewis.

    Gair o gyngor: Os ydych chi'n defnyddio cerddoriaeth, dylech gadw'r alaw yn fyr a'i chwarae unwaith yn unig, neu bydd rhaid i chi aros am amser hir nes bod yr alaw wedi gorffen yn ystod pob prawf.

    Gair o gyngor: Os nad ydych am greu eich alaw eich hun, sgroliwch i lawr y ddewislen Cerddoriaeth i ddod o hyd i alawon a synau parod.


    • Os ydych chi'n defnyddio'r bloc 'play melody' ar frig y ddewislen Cerddoriaeth, gallwch greu eich alaw eich hun trwy glicio ar y symbol nodyn cerddorol yn y bloc, yna uwcholeuo'r bocsys i greu nodau gwahanol.
    • Os ydych chi'n defnyddio'r bloc 'play melody' gydag alawon sydd wedi'u rhagosod, dylech newid yr opsiwn 'in background' i 'until done' gan ddefnyddio'r saeth i lawr.

    Os ydych yn dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Defnyddio eich alaw eich hun:

    Mae yna floc 'play melody at tempo 120 (bpm) until done' o dan y bloc 'dangos eicon' yn yr adran 'ar ddechrau'.

    Defnyddio alaw sydd wedi'i rhagosod:

    Mae yna floc 'play melody ffync until done' o dan y bloc 'dangos eicon' yn yr adran 'ar ddechrau'.

    Defnyddio effaith sain:

    Mae yna floc 'play hapus until done' o dan y bloc 'dangos eicon' yn yr adran 'ar ddechrau'.

  5. Gofalwch fod y Micro:Bit yn ymateb pan fydd yn clywed sain uchel.

    Gair o gyngor: Dangosodd y fideo fod y meicroffon yn gweithredu fel synhwyrydd sain a'i fod yn ddyfais fewnbynnu.

    Gair o gyngor: Gallech ddefnyddio delwedd, testun, cerddoriaeth neu sain fel ymateb.

    Gair o gyngor: Sain uchel yn y rhaglen hon yw unrhyw beth dros 170.


    • Mae'r bloc sydd ei angen arnoch ar gael yn y ddewislen blociau Mewnbwn.
    • Edrychwch o dan micro:bit (V2). Mae'r bloc yn yr adran hon oherwydd bod dau fersiwn o BBC Micro:Bit ar gael erbyn hyn a dim ond fersiwn 2 sydd â meicroffon.
    • Bydd angen i chi wylio'r fideo eto os ydych chi'n ansicr sut i greu sain uchel yn yr efelychydd.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.

    Mae'r datrysiad hwn yn sicrhau bod y Micro:Bit yn dangos neges pan fydd yn clywed sain uchel.

    Mae bloc 'ar sain uchel' newydd wedi'i ychwanegu sy'n cynnwys bloc 'dangos testun 'SHHH!''.



Defnyddiwch yr un rhaglen i greu'r canlynol:

  1. Ar ddiwedd eich dilyniant cychwyn, cliriwch y sgrin ac ychwanegwch neges groeso.

    Gair o gyngor: Defnyddiwch neges fer oherwydd bydd angen i chi aros iddi gwblhau bob tro y byddwch yn profi'ch rhaglen.

    Gair o gyngor: Mae'r ddau floc sydd eu hangen i wneud hyn ar gael yn y ddewislen Sylfaenol.


    • Ym maes rhaglennu, rydym yn defnyddio'r gair llinyn i ddisgrifio unrhyw gyfres o symbolau.
    • Bydd angen i chi ddod o hyd i'r bloc 'dangos testun' cyn gallu newid y gair "helo" i air/neges o'ch dewis.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae bloc 'clirio sgrin' wedi'i ychwanegu ar ddiwedd yr adran 'ar ddechrau' ynghyd â bloc 'dangos testun Hi' o dan hynny.

  2. Ychwanegwch neges i'ch dolen 'am byth' sy'n dweud 'Angen Mewnbwn'.

    Gair o gyngor: Dylech drin y sgrin hon fel sgrin aros.


    • Byddwch yn defnyddio'r un bloc ag a ddefnyddiwyd gennym ar gyfer y neges groeso uchod.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Ychwanegwyd bloc 'dangos testun' â'r neges 'Angen Mewnbwn' at waelod y bloc 'am byth'.

  3. Sicrhewch fod y Micro:Bit yn arddangos delweddau gwahanol pan fydd y botwm B yn cael ei bwyso a phan fydd y ddau fotwm yn cael eu pwyso.

    Gair o gyngor: Mae gan y bloc 'ar fotwm A wedi'i wasgu' saeth i lawr sy'n eich galluogi i newid yr A i B neu A+B.


    • Cofiwch fod modd dod o hyd i'r blociau botwm yn y ddewislen Mewnbwn.
    • Bydd angen tri o'r blociau hyn arnoch yn eich rhaglen, un ar gyfer pob cyfuniad o fotymau.
    • I brofi hyn, dylai'r efelychydd fod â botwm newydd sydd wedi'i labelu A+B, sy'n gadael i chi ddynwared effaith pwyso'r ddau fotwm gan ddefnyddio'r llygoden.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae blociau newydd 'on button B pressed' ac 'on button A+B pressed' wedi'u hychwanegu. Mae'r ddau yn cynnwys bloc 'dangos eicon' gwahanol.

  4. Ychwanegwch effeithiau sain gwahanol gan ddibynnu ar ba fotwm sy'n cael ei bwyso.

    Gair o gyngor: Mae'n bosibl y byddwch yn dechrau gweld rhai mathau o ymddygiad digroeso yn eich profion wrth i ni ychwanegu mwy o flociau at y rhaglenni botwm. Byddwn yn rhoi sylw i pam mae hyn yn digwydd a sut i osgoi hyn yn nes ymlaen yn y gyfres.


    • Os nad ydych chi'n ansicr, gweler cam 4 yn yr her efydd.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae blociau 'play sound until done' gwahanol wedi'u hychwanegu at bob un o'r adrannau pwyso botymau.

  5. Ychwanegwch adwaith ar gyfer pan fydd y Micro:Bit yn canfod ei bod yn dawel.

    Gair o gyngor: Mae llawer o flociau yn cynnwys cwymplenni sy'n eich galluogi i newid geiriau allweddol.

    Gair o gyngor: Ar gyfer y Micro:Bit, ystyr tawel yw unrhyw lefel sŵn o dan 90.


    • Cofiwch fod canfod sain yn fewnbwn.
    • Mae modd newid y bloc 'ar sain uchel'.
    • Gallai adwaith fod yn sain, yn ddelwedd, yn eicon neu'n neges.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae adran 'ar sain tawel' wedi cael ei hychwanegu at y rhaglen sy'n cynnwys bloc 'dangos leds' gyda goleuadau'n cael eu goleuo i ddangos wyneb hapus.



Parhau i greu eich rhaglen trwy ychwanegu'r canlynol:

  1. Newid y ddelwedd sy'n ymddangos wrth gychwyn gydag animeiddiad.

    Gair o gyngor: Gallech ddefnyddio delweddau ailadroddus, neu efallai y byddwch am ddefnyddio bloc o'r ddewislen Dolennau i helpu gyda hyn.


    • Mae yna floc 'ailadrodd 4 o weithiau' yn y ddewislen Dolennau a gellir ei osod i ailadrodd faint fynnwch chi o weithiau. Mae hyn yn golygu mai dim ond unwaith y bydd angen i chi ddefnyddio pob delwedd.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae'r ddelwedd yn y bloc 'ar ddechrau' wedi'i disodli â bloc 'ailadrodd 4 o weithiau' sy'n cynnwys dau eicon gwahanol (calon fawr a chalon fach).

    Mae animeiddiad cychwyn y rhaglen hon yn dangos calon yn curo.


  2. Newidiwch eich animeiddiad dolen am byth i ddangos person neu anifail yn symud i'r sgrin.

    Gair o gyngor: Ewch ati i ddewis neu greu delwedd lawn o'ch person/anifail yn gyntaf. Yna bydd hi'n haws i chi greu'r animeiddiad.


    • Gofalwch fod eich anifail/person yn ymddangos o ochr chwith eich sgrin gan ddefnyddio un golofn o oleuadau ar y tro cyn ei symud yr holl ffordd ar draws y sgrin.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae'r animeiddiad dolen am byth bellach yn cynnwys cyfres o flociau 'dangos leds' sy'n creu animeiddiad o neidr sy'n cyrraedd o'r ochr chwith nes ei bod yn gwbl weladwy.

  3. Newidiwch adwaith y Micro:Bit drwy ogwyddo i'r chwith a gogwyddo i'r dde.

    Gair o gyngor: Mae gan rai blociau gwymplenni i newid gair allweddol.

    Gair o gyngor: Mae'r Micro:Bit yn defnyddio ei fesurydd cyflymu ar gyfer hyn, sy'n synhwyrydd symudiadau. Ai allbwn neu fewnbwn yw hwn?


    • Bydd angen i chi ddefnyddio bloc o'r ddewislen Mewnbwn.
    • Oes bloc sy'n seiliedig ar symud y gallwch chi ei ddefnyddio? Pa opsiynau sydd ganddo ar gyfer y gair allweddol?

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae adrannau 'ar gwyro i'r chwith' ac 'ar gwyro i'r dde' wedi'u hychwanegu, ac mae'r ddwy adran yn cynnwys bloc 'dangos leds' sy'n dangos saeth yn pwyntio i'r un cyfeiriad â'r gogwydd.

  4. Ychwanegwch effeithiau sain at y gogwydd i'r chwith a'r dde.


    • Mae'r un broses ar waith â phan wnaethom ni ychwanegu effeithiau sain at fewnbynnau'r botwm.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae blociau 'play sound until done' gwahanol wedi'u hychwanegu at y ddwy adran mewnbynnu gogwydd.

  5. Ar ddiwedd eich dolen am byth, ewch ati i ddisodli'r bloc 'dangos testun Need Input' â bloc sgrin glir ac yna dangoswch lefel y sain yn lle hynny.

    Gair o gyngor: Mae lefel y sain ar Micro:Bit yn cael ei mesur ar ffurf rhif.

    Gair o gyngor: Bydd angen i chi uno dau floc gyda'i gilydd i greu'r cyfarwyddyd hwn.


    • Mae'r ddewislen Sylfaenol yn cynnwys bloc 'dangos rhif'.
    • Mae'r synhwyrydd sain yn ddyfais fewnbynnu, felly bydd angen i chi edrych yn y ddewislen Mewnbwn ar gyfer y bloc a fydd yn mynd i mewn i'ch bloc 'dangos rhif'.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae'r bloc 'dangos testun Angen Mewnbwn' yn y ddolen am byth wedi'i ddisodli â bloc 'clirio sgrin' ac yna bloc sy'n cynnwys 'dangos rhif' a 'lefel y sain'.

Gwers Fideo (yn Saesneg):

Ymarferion:

Ar gyfer y sesiwn hon byddwn yn creu rhaglen i droi eich BBC Micro:Bit yn anifail anwes rhithwir.

Fel sy'n wir am bob un o'n hymarferion, mae dysgwyr yn cwblhau un lefel cyn symud i'r nesaf. Fel arall, mae'n bosibl y bydd dysgwyr yn methu derbyn gwybodaeth neu'n gadael allan cam hanfodol sy'n atal y rhaglen rhag gweithio.

Cliciwch ar bennawd pob her i gael y manylion.


Dechreuwch raglen Micro:Bit newydd ar Makecode gan gwblhau'r camau canlynol.

  1. Ewch ati i greu neges a/neu ddelwedd sy'n ymddangos ar ôl pwyso dechrau er mwyn helpu i nodi'r rhaglen.

    Gair o gyngor: Cofiwch fod modd defnyddio'r bloc clirio'r sgrin i greu toriad rhwng delwedd a neges.


    Defnyddiwch y ddolen 'ar ddechrau' sy'n cynnwys bloc 'dangos testun 'fy anifail anwes'' wedi'i ddilyn gan 'clirio sgrin' ac yna blociau 'dangos eicon (neidr)' blocks.

  2. Ewch ati i greu animeiddiad bach o'ch anifail anwes yn aros y tu mewn i'r ddolen am byth.

    Gair o gyngor: Efallai y gallwch ddefnyddio animeiddiad y gwnaethoch chi ei greu yn Sesiwn Un.


    • Cofiwch, dim ond dwy ddelwedd sydd eu hangen arnoch i wneud animeiddiad.
    • Gallwch ddefnyddio'r eiconau sydd wedi'u creu ymlaen llaw i'ch rhoi ar ben ffordd.
    • Po fwyaf o ddelweddau rydych chi'n eu defnyddio, y mwyaf o amser y bydd ei angen i gwblhau'r ddolen am byth - gallai hyn achosi problemau wrth i ni greu'r rhaglen.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Nawr mae gennym ddolen am byth sy'n cynnwys bloc 'dangos eicon (neidr)' ac yna bloc 'dangos leds' sy'n dangos yr un neidr ond gyda'i chynffon mewn safle gwahanol.

  3. Defnyddiwch un o'r mewnbynnau (fel botwm A) i fwydo'ch anifail anwes ac ewch ati i'w ddefnyddio i ddangos delwedd o eitem fwyd ar y sgrin.


    • Efallai y bydd angen i chi glirio'r sgrin cyn dangos eich delwedd bwyd.
    • Cofiwch fod bloc rheoli botwm A wedi'i leoli yn y ddewislen Mewnbwn.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.

    Mae'r ateb hwn yn cynnwys fy ymgais i dynnu llun o fyrgyr ar gyfer fy anifail anwes.

    Mae bloc rheoli 'ar fotwm A wedi'i wasgu' wedi ei ychwanegu sy'n cynnwys bloc 'dangos leds' gydag ymgais i dynnu llun o fyrgyr.

  4. Ewch ati i greu animeiddiad sy'n dangos yr anifail anwes yn bwyta.

    Pwysig: Mae'n bosibl y byddwch yn dechrau gweld rhai mathau o ymddygiad digroeso wrth i'ch animeiddiadau wrthdaro. Trwy ddefnyddio ein blociau os datganiad newydd, gallwn gywiro'r broblem a storio'r rhaglen gyfan y tu mewn i un ddolen am byth. Mae'r ddelwedd isod yn dangos sut i gynnwys botwm A fel amod mewn os datganiad y tu mewn i'r ddolen am byth.

    Dolen am byth sy'n cynnwys bloc 'os botwm A yn cael ei wasgu wedyn' sy'n cadw bloc 'dangos leds'.

    Nodwch: Hira'n byd fydd eich dolen am byth, mwya'n byd y bydd hi'n ei gymryd i'r rhaglen ymateb i fewnbwn. Mae'n bosibl y bydd angen i chi ddal y mewnbynnau am fwy o amser er mwyn eu cofnodi.

    Cofiwch: Dim ond un ddolen am byth y dylech ei chael yn eich rhaglen.

    Gair o gyngor: Defnyddiwch yr adran arall yn y bloc os datganiad hwn fel cartref newydd i'ch anifail anwes sy'n aros am animeiddiad.


    • Gallwch animeiddio'r anifail anwes neu'r bwyd ar gyfer yr effaith hon.
    • Bydd bloc ailadrodd o'r ddewislen Dolennau yn helpu gyda'r animeiddiad.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae'r ddolen am byth wedi'i hailysgrifennu'n gyfan gwbl. Mae'n dweud y canlynol erbyn hyn am byth, os botwm A yn cael ei wasgu wedyn {ailadfrodd 3 o weithiau {dangos leds (byrgyr), dangos leds (byrgyr rhannol)}}, arall {dangos eicon (neidr), dangos leds (neidr gyda safle cynffon gwahanol)}.

  5. Ewch ati i greu ymateb gwael gan eich anifail anwes (gallwch ddefnyddio delwedd, animeiddio, a/neu sain) pan fydd yn clywed sŵn uchel.

    Gair o gyngor: Yn hytrach na defnyddio'r bloc 'ar sain uchel', defnyddiwch os datganiad arall y tu mewn i'ch dolen am byth ar gyfer lefel sain > 175.


    • Gallwch ehangu os datganiad trwy glicio ar yr arwydd plws. Gallwch wneud hyn ar gyfer cynifer o os datganiadau eraill ag y dymunwch.
    • Gallwch ddod o hyd i'r bloc mwy na yn y ddewislen Rhesymeg.
    • Er mwyn profi eich rhaglen, defnyddiwch y bar newydd ar gyfer lefel sain y meicroffon yn eich Micro:Bit wedi'i efelychu er mwyn newid y lefel. Pan fyddwch yn mynd dros 175 dylech weld y rhaglen - efallai y bydd ychydig o oedi.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Nawr mae gan y ddolen am byth ddatganiad 'arall os lefel y sain > 175 wedyn' sy'n cynnwys bloc 'clirio sgrin' a bloc 'dangos testun 'SHH!''.



Gan barhau â'r un rhaglen, ewch ati i gynnwys yr ychwanegiadau isod.

  1. Defnyddiwch y rhaglen i drefnu bod eich anifail anwes yn mynd i gysgu pan fydd hi'n tywyllu (defnyddiwch lai na 70 neu'n hafal i 70 ar gyfer y gwerth hwn).

    Gair o gyngor: Gallwch gael cynifer o os datganiadau ag y dymunwch mewn rhaglen Micro:Bit.


    • Defnyddiwch y botwm plws ar waelod eich os datganiad i ychwanegu arall os datganiad at eich rhaglen.
    • Gwiriwch pa symbol rydych chi'n ei ddefnyddio ar gyfer y gymhariaeth. Dylai edrych fel hyn ≤.
    • Gallech ddefnyddio testun o 'zzz' i ddangos bod eich anifail anwes wedi mynd i gysgu.
    • Cofiwch glirio'r sgrin yn gyntaf.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Adran newydd 'arall os lefel golau ≤ 70 wedyn' sy'n cynnwys bloc 'clirio sgrin' a bloc 'dangos testun 'zzz''.

  2. Ewch ati i greu newidyn ar gyfer 'llawnder' sy'n cynyddu fesul un bob tro y caiff eich anifail anwes ei fwydo.

    Gair o gyngor: Mae gosod newidyn newydd i sero wrth ddechrau yn arfer da bob tro.


    • Mae yna newidyn newid fesul bloc sy'n gwneud yr hyn sydd ei angen arnom.
    • Pa ran o'r cod y mae angen iddo fynd i mewn iddi?
    • Dylech sicrhau nad ydych chi'n ei osod y tu mewn i ddolen ailadrodd animeiddiad.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Erbyn hyn mae yna floc 'gosod llawnder i 0' y tu mewn i'r cod ar ddechrau a bloc 'newid llawnder gan 1' yn y datganiad 'os botwm A yn cael ei wasgu wedyn' ar ôl y bloc ailadrodd ar gyfer yr animeiddiad bwyta.

  3. Defnyddiwch fewnbwn (botwm B efallai) a fydd yn dangos gwerth 'llawnder' i'r defnyddiwr.


    • Gallwch roi newidyn y tu mewn i floc 'dangos testun'

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Datganiad 'arall os botwm B yn cael ei wasgu wedyn' yn cynnwys bloc 'dangos testun llawnder'.

  4. Defnyddiwch y rhaglen i wneud i'ch anifail anwes ymateb yn hapus i olau llachar (defnyddiwch fwy na 200 ar gyfer y gwerth hwn).


    • Os ydych chi wedi dewis gwneud animeiddiad, cofiwch am y bloc ailadrodd yn y ddewislen Dolennau.
    • Cofiwch sicrhau mai > yw eich symbol cymharu.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.

    Fe wnes i ddewis i'm nadredd ddathlu golau llachar gydag animeiddiad trosbennu.

    Datganiad 'arall os lefel golau > 200 wedyn' sy'n cynnwys dolen 'ailadfrodd 3 o weithiau' sy'n cynnwys bloc 'dangos eicon (neidr)' a thri bloc 'dangos leds' sy'n creu fy animeiddiad o neidr sy'n trosbennu.

  5. Defnyddiwch y rhaglen i wneud i'ch anifail anwes wneud sŵn i gael sylw os yw'r lefel sŵn yn llai na neu'n hafal i 50.

    Gair o gyngor: Gosodwch floc saib (o leiaf 500 milieiliad) ar ôl i'ch sŵn chwarae - coeliwch fi.


    • Cofiwch sicrhau bod pa bynnag sŵn rydych chi wedi dewis ei ddefnyddio yn y rhaglen yn chwarae unwaith yn unig neu hyd nes ei fod wedi gorffen.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Datganiad 'arall os lefel y sain ≤ 50 wedyn'sy'n cynnwys bloc 'play helo until done' ac yna bloc sy'n dweud 'oedi (ms) 500'.



Nawr, gadewch i ni ddatblygu'r rhaglen ymhellach gyda'r tasgau canlynol.

  1. Os yw eich anifail anwes yn cysgu wrth glywed sŵn uchel, rhaid iddo ymateb trwy ddangos mwy o ddicter/ofn/syndod na phan fydd yn effro.

    Gair o gyngor: Gallwch roi os datganiadau y tu mewn i'w gilydd, gan greu os datganiad sydd wedi'i bentyrru.

    Pwysig: Bydd eich rhaglen yn gweithio ei ffordd trwy'r rhestr o os datganiadau a arall os datganiadau yn y ddolen am byth. Bydd yn rhedeg y darn cyntaf o god os bodlonir yr amodau, ac yn gadael y gweddill.

    Meddyliwch am hyn fel rhestr wirio lle rydych chi'n dychwelyd i frig y rhestr a dechrau eto ar ôl gwirio rhywbeth.

    Mae hyn yn golygu bod angen i chi wirio pa un o'r amodau (sŵn uchel neu dywyllwch) sy'n dod gyntaf yn eich rhaglen a mewnosod y 'datganiad os' newydd yn yr amod perthnasol.



    • Defnyddiwch os ac arall ddatganiad ar gyfer hyn er mwyn penderfynu pa ymateb sŵn uchel sy'n cael ei ddangos.
    • Gan ein bod am glirio'r sgrin cyn y ddau opsiwn, gallwn ddefnyddio'r gorchymyn hwn unwaith cyn y os/arall datganiad newydd.
    • Fel arall, gallech ystyried defnyddio blociau rhesymeg uwch sy'n caniatáu sawl amod mewn os/arall os datganiad.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.

    Yn ein rhaglen atebion, mae'r amod s&wcirc:n uchel uwchben yr amod tywyll, felly mae'r os datganiad newydd yn mynd yno.

    Y tu mewn i'r bloc 'arall os lefel y sain > 175 wedyn' mae gennym floc 'clirio sgrin' ac yna bloc 'os lefel golau ≤ 70 wedyn' sy'n cynnwys 'dangos testun '!?!'', ac yna bloc 'arall' sy'n cynnwys 'dangos testun 'SHH!''.

  2. Mae angen i'ch newidyn 'llawnder' fod ag uchafswm gwerth o 5.

    Gair o gyngor: : Mae hyn yn golygu eich bod ond eisiau ychwanegu un at y gwerth llawnder pan fydd yn llai na 5 (neu'n llai na ac yn hafal i 4).


    • Mae angen os datganiad arnoch pan fydd y llawnder yn llai na 5 er mwyn ychwanegu at y gwerth pan fydd yn wir yn unig.
    • Ar hyn o bryd, does dim angen os datganiad i wneud hyn gan nad oes unrhyw gam gweithredu ar gyfer os datganiad nad yw'n wir.
    • Cofiwch gadw'r os datganiad hwn y tu allan i unrhyw ddolennau ailadrodd a ddefnyddir ar gyfer yr animeiddiad bwydo.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Y tu mewn i'n datganiad 'os botwm A yn cael ei wasgu wedyn' rydym wedi symud y bloc 'newid llawnder gan 1' y tu mewn i ddatganiad 'os llawnder < 5 wedyn'.

  3. Yn hytrach na dangos gwerth llawnder i'r defnyddiwr gan ddefnyddio'r mewnbwn a ddewiswyd yn yr heriau arian, defnyddiwch y rhaglen i ddangos lefel mesurydd.

    Gair o gyngor: Gallwch roi arall os datganiadau y tu mewn i os / arall os / arall datganiad.


    • I wneud hyn, bydd angen delwedd ar gyfer pob lefel lawnder i greu effaith mesurydd/medrydd.
    • Mewn rhestr datganiadau o'r fath, mae angen i chi gynnwys arall datganiad i gofnodi 0 a/neu unrhyw werthoedd annisgwyl. Gall hyn weithredu fel sgrin gwall i ni wedyn.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.

    Fe wnaethom ddewis siâp mesurydd crwn sy'n mynd yn fwy cyflawn pan fydd y lefel lawnder yn uwch.

    Y tu mewn i'n datganiad 'arall os botwm B yn cael ei wasgu wedyn' mae gennym floc 'os llawnder = 1 wedyn' sy'n cynnwys bloc'dangos leds'. Dilynir hyn wedyn gan arall os ddatganiadau ar gyfer gwerthoedd llawnder rhwng 2 a 5, gyda phob un yn cynnwys bloc 'dangos leds' newydd gyda delwedd lefel mesurydd cysylltiedig. Daw hyn i ben wedyn gyda 'datganiad arall' sy'n cynnwys bloc 'dangos leds' gyda delwedd o wyneb trist.

  4. Dylai'r anifail anwes wrthod bwyta os yw'n llawn (llawnder = 5).

    Gair o gyngor: Gallai helpu i symud yr animeiddiad bwyta i'r un os datganiad â phan fydd y newidyn llawnder yn cynyddu, gan sicrhau o ganlyniad bod gennych animeiddiad gwahanol pan fydd arall datganiad yn llawn.


    • Cofiwch fod angen dolennau ailadrodd y tu mewn i'r datganiadau hyn er mwyn ailadrodd animeiddiadau.
    • Wrth ddatblygu rhaglen, yn aml mae'n rhaid i ni newid lleoliadau adrannau cod er mwyn cynnwys nodweddion newydd.
    • I wneud hyn, mae angen atodi 'arall' i'n datganiad 'os llawnder < 5'.

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Mae'r animeiddiad bwydo bellach wedi'i symud y tu mewn i'r datganiad 'os llawnder < 5' (sydd y tu mewn i'r amod 'os botwm A yn cael ei wasgu'). Yna, mae arall datganiad  wedi'i ychwanegu sy'n dangos animeiddiad newydd (bloc 'dangos eicon' a bloc 'dangos leds' leds' y tu mewn i ddolen 'ailadfrodd 3 o weithiau') o'r anifail anwes yn gwrthod bwyta.

  5. Dylai'r gwerth llawnder ostwng yn araf dros amser wrth i chwant bwyd eich anifail anwes gynyddu eto.

    Gair o gyngor: Gan fod hon yn broses gefndirol na fydd yn dioddef ymyrraeth gan y ddolen am byth, gallwch ddefnyddio dolen amserydd ar wahân i wneud hyn.


    • Gallwch ddod o hyd i floc amserydd yn y ddewislen Dolennau.
    • Yr amser diofyn yw 500 ms - sef bob hanner eiliad - bydd angen i chi newid hyn (fe wnaethom ni weld bod munud - neu 60000 ms - yn addas iawn ar gyfer hyn).
    • Rydym am dynnu un i ffwrdd o'r llawnder fel y gallwn ddefnyddio bloc 'newid llawnder gan' gyda -1 yn hytrach nag 1.
    • Dydyn ni ddim eisiau gwerthoedd negyddol, felly rydym ond eisiau tynnu i ffwrdd o'r gwerth llawnder os yw dros 0 (neu'n fwy nag 1 ac yn hafal i 1).

    Os ydych chi'n dal i gael trafferth, defnyddiwch y botwm ateb isod.


    Dolen 'pob 60000 ms'sy'n cynnwys datganiad 'os llawnder ≥ 1 wedyn' gyda bloc 'newid llawnder gan -1' y tu mewn.



Mae gennych chi nawr anifail anwes rhithwir. Dyma rai syniadau ar gyfer ychwanegiadau a nodweddion eraill y gallwch chi roi cynnig arnyn nhw. Gweithgareddau i chi ymchwilio iddyn nhw a'u harchwilio yw'r rhain - does dim atebion ar gael.

  • Defnyddiwch y rhaglen i wneud i'r anifail anwes ddangos pan fydd chwant bwyd arno (llawnder ≤ 1).

    Gair o gyngor: Gellir gwneud hyn trwy ychwanegu arall os datganiad i'ch dolen am byth.


  • Ychwanegwch lefel hapusrwydd a mewnbynnau chwarae yn y rhaglen. Mwya'n byd y byddwch chi'n chwarae gyda'r anifail anwes, hapusa'n byd fydd e'n teimlo.

    Gair o gyngor: Efallai y byddwch am symud y mesurydd chwant bwyd fel ei fod yn dangos yn awtomatig ar ôl bwydo'r anifail anwes er mwyn rhyddhau botwm B ar gyfer hyn.


  • Gosodwch werth uchaf ac amser oeri ar gyfer y gwerth hapusrwydd. Sut fydd eich anifail anwes yn ymateb pan fydd y gwerth hapusrwydd yn gostwng yn rhy isel?

    Gair o gyngor: Bydd hyn yn defnyddio'r un fframwaith â'n mesurydd llawnder.


  • Defnyddiwch y rhaglen fel bod yr anifail anwes yn tyfu dros amser os yw'n derbyn gofal priodol.

    Gair o gyngor: Gallech greu newidyn sgôr ar gyfer hyn a threfnu bod yr anifail anwes yn gwella wrth i'r sgôr gyrraedd nodau penodol.



Gwers Fideo (yn Saesneg):

Ymarferion:

For this session we shall be creating a two-player dice duelling game using two BBC Micro:Bits. We recommend using the multi-editor for these exercises. Name the program on the left Player One and the other Player Two.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise, you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


  1. Have both Micro:Bits programmed to show a random number between 1 and 6 every time it is shaken - like a dice roll.

    Gair o gyngor: To avoid interference between loops, all outputs need to be inside the forever loop.


    • The video for Session Two included a walk-through on creating a basic dice program.
    • Use a different dice roll variable name for each player - this will reduce confusion later.
    • You only want the dice roll value to start showing after the first roll (so when the variable is no longer 0)


    Player One

    The 'ar ddechrau' loop contains a 'dangos leds' block with p1 displayed and a 'set player1Roll to 0' block. The forever loop contains an 'if player1Roll > 0 then' statement with a 'show number player1Roll' inside. There is also an 'on shake' loop which holds a 'set player1Roll to pick random 1 to 6' block.

    Player Two

    The 'ar ddechrau' loop contains a 'dangos leds' block with p2 displayed and a 'set player2Roll to 0' block. The forever loop contains an 'if player1Roll > 0 then' statement with a 'show number player2Roll' inside. There is also an 'on shake' loop which holds a 'set player2Roll to pick random 1 to 6' block.

  2. For Player One, have button A show your last roll whilst button B shows the other player's last roll. Do the same for Player Two.

    Gair o gyngor: Don't forget to set both Micro:Bits into the same radio group so they can talk to each other - this should be done during the start-up sequence.


    • Use if statements inside the forever loop for button pressing, not the separate 'ar fotwm A wedi'i wasgu' or ' on button B pressed' blocks.
    • Remember, you can stack if statements inside each other.
    • You can have an else statement that contains nothing, or you can use it to report an error.


    Player One

    New additions to the program: The 'ar ddechrau' loop has had a 'set player2Roll to 0' and a 'radio set group 138' added. Inside the forever loop, the 'if player1Roll > 0 then' and its contents are now inside an 'os botwm A yn cael ei wasgu wedyn' statement. An 'arall os botwm B yn cael ei wasgu wedyn' statement has been added containing an 'if player2Roll > 0 then' statement holding a 'show number player2Roll' block. The else statement has been left empty. The 'on shake' loop now has a 'radio send number player1Roll' block added to the end. Finally, there is now an 'on radio received receivedNumber' loop housing a 'set player2Roll to receivedNumber' block.

    Player Two

    New additions to the program: The 'ar ddechrau' loop has had a 'set player1Roll to 0' and a 'radio set group 138' added. Inside the forever loop, the 'if player2Roll > 0 then' and its contents are now inside an 'os botwm A yn cael ei wasgu wedyn' statement. An 'arall os botwm B yn cael ei wasgu wedyn' statement has been added containing an 'if player1Roll > 0 then' statement holding a 'show number player1Roll' block. The else statement has been left empty. The 'on shake' loop now has a 'radio send number player2Roll' block added to the end. Finally, there is now an 'on radio received receivedNumber' loop housing a 'set player1Roll to receivedNumber' block.

  3. When either player presses the touch logo, have the screen show the signal strength.


    • The touch logo is only available on Micro:Bit V2 - so check the V2 section of the input menu.
    • This one is hard to test in the simulator because you can only alter the signal strength when it is sending information (in this case when we shake it).


    Player One

    New addition of an 'else if logo is pressed then' statement inside the forever loop containing a 'show number received packet signal strength' block.

    Player Two

    New addition of an 'else if logo is pressed then' statement inside the forever loop containing a 'show number received packet signal strength' block.



  1. Pressing button A and B at the same time should give players the total of both players dice rolls.

    Gair o gyngor: You will need to have something to check both players have rolled.

    Pwysig: To check against two conditions (in this case, that both players have rolled), you could stack the if statements or you could use an 'and' block.

    Stacked Conditions

    An 'if player1Roll > 0 then' statement holding a 'if player2Roll > 0 then' statement which in turn holds a 'dangos testun' block that reads: This checks both conditions are true for this block

    The 'and' Boolean Operator

    An 'if player1Roll >0 and player2Roll > 0 then' statement holding a 'dangos testun' block that reads: This checks both conditions are true for this block.

    Note: A Boolean value is true of false. A Boolean Operator compares two inputs to produce a single true or false output.

    Gair o gyngor: The if button A and B pressed statement needs to be put in the forever loop above the if statements for button A and button B.

    Pwysig: When using else if statements, the program goes down the list of conditions and stops at the first one that is true. It will then skip the rest. When using the if button A and B pressed, any conditions checking if either of those buttons is pressed becomes true. So, 'if button A is pressed' will be true, 'if button B is pressed' will be true and 'if button A+B is pressed' will be true. This highlights the importance of the ordering of conditions.



    • There are blocks that do the calculations for us in the Maths menu.
    • You may want to add an else statement to this to show that not all players have rolled yet.


    Player One

    The main if statement in the forever loop has now been changed to read 'if button A+B is pressed then' which contains an 'if player1Roll > 0 and player2Roll > 0 then' statement holding a 'show number player1Roll + player2Roll' block. The button A program has been relocated to an 'else os botwm A yn cael ei wasgu wedyn' statement.

    Player Two

    The main if statement in the forever loop has now been changed to read 'if button A+B is pressed then' which contains an 'if player2Roll > 0 and player1Roll > 0 then' statement holding a 'show number player2Roll + player1Roll' block. The button A program has been relocated to an 'else os botwm A yn cael ei wasgu wedyn' statement.

  2. Move the program for button A so as the Micro:Bit will show the roll on the screen automatically, include an else statement for when the dice roll value is zero. This could be a waiting animation, image, or message/instruction.

    Gair o gyngor: Now you will need pauses to keep values other than the player's own roll on the screen longer.


    • You can move the show player's own roll program into the else statement for this.
    • Use 1 second (1000ms) pauses where needed.
    • You may need to hold down buttons for slightly longer when testing. Especially if you are using a string to show a message as it will not listen for buttons until the message has finished scrolling.
    • Leave the 'else os botwm A yn cael ei wasgu wedyn' statement empty for now.


    Player One

    'pause (ms) 1000' blocks have been added to the on start loop and after every 'dangos rhif' block in the forever loop. The 'if player1Roll > 0 then' statement has had an else statement added containing 'dangos testun Shake' and this has then been relocated from the 'else if button A pressed then' statement to the main else statement inside the forever loop.

    Player Two

    'pause (ms) 1000' blocks have been added to the on start loop and after every 'dangos rhif' block in the forever loop. The 'if player2Roll > 0 then' statement has had an else statement added containing 'dangos testun Shake' and this has then been relocated from the 'else if button A pressed then' statement to the main else statement inside the forever loop.

  3. Now use Button A to reset the game.

    Gair o gyngor: You will need to also send the reset command to the other player's Micro:Bit.


    • You can send a string, such as a word, over the radio group.
    • If using a string for this, you need to use the string comparison block - this is the one with quotation marks inside the value boxes.
    • Alternatively, you could send a different number which triggers the reset.
    • Check you are using the correct receiving blocks for the type of information you are sending.


    Player One

    The 'else os botwm A yn cael ei wasgu wedyn' statement within the forever loop, now contains 'set player1Roll to 0', 'set player2Roll to 0', and 'radio send string reset' blocks. A new 'on radio received receivedString' loop has been added containing an 'if receivedString = reset then' statement holding 'set player1Roll to 0' and 'set player2Roll to 0' blocks.

    Player Two

    The 'else os botwm A yn cael ei wasgu wedyn' statement within the forever loop, now contains 'set player2Roll to 0', 'set player1Roll to 0', and 'radio send string reset' blocks. A new 'on radio received receivedString' loop has been added containing an 'if receivedString = reset then' statement holding 'set player2Roll to 0' and 'set player1Roll to 0' blocks.



  1. Let us now have the players duelling. Whichever player rolls the highest number wins. Have the Micro:Bits show whether their player won or lost.

    Gair o gyngor: You can use the 'and' Boolean operator here to make sure both players have rolled before checking to see who won.


    • You need to work out where to put this in your program.
    • Compare the two players dice rolls to work out which is higher.
    • What happens when the players both roll the same number?
    • You can use images, strings, and/or animations for these.
    • Add pauses where you think they are needed.


    Player One

    Where the program shows the player1Roll value (inside the 'if player1Roll > 0' statement which is in turn inside the main else statement for the forever loop), a 'pause (ms) 1000' block has been added beneath the existing 'show number player1Roll'. Then under this a new if statement has been added: 'if player1Roll > 0 and player2Roll > 0 then'. This then contains a new if, else if and else statement. The if: 'if player1Roll > player2Roll then' holds a 'dangos eicon (happy)'. The else if: 'else if player2Roll > player1Roll then' holds a 'dangos eicon (sad)' block. The else holds a 'dangos eicon (asleep)' block.

    Player Two

    Where the program shows the player2Roll value (inside the 'if player2Roll > 0' statement which is in turn inside the main else statement for the forever loop), a 'pause (ms) 1000' block has been added beneath the existing 'show number player2Roll'. Then under this a new if statement has been added: 'if player2Roll > 0 and player1Roll > 0 then'. This then contains a new if, else if and else statement. The if: 'if player2Roll > player1Roll then' holds a 'dangos eicon (happy)'. The else if: 'else if player1Roll > player2Roll then' holds a 'dangos eicon (sad)' block. The else holds a 'dangos eicon (asleep)' block.

  2. Upgrade the program so both players must roll twice before comparing their totals to determine the winner. Players should not be able to roll more than twice reset.

    Gair o gyngor: You will need a new variable for the number of dice rolls.


    • Your new counter variable will need to be used in place of all conditions that currently check if the player's own roll value is over 0.
    • Have a message or image come up if a player tries to roll more than twice - this makes it easier to test.
    • The use and changing of the counter variable can be done in the 'on shake' loop.
    • You will need to set this new variable to zero both on start and during the reset process.
    • Changing the random number to be between 1 and 12 or multiplying the initial roll by two will not match the same probabilities as using two separate dice rolls - therefore, you will need to use a roll counting variable. For more information regarding probabilities of dice rolls, you can check out our Dice Duelling activity.


    Player One

    A new 'set rollCounter to 0' block has been added to the 'ar ddechrau' loop. The 'on shake' loop is now made up of an if, else if and else statement. The if statement: 'if rollCounter = 0 then' contains a 'set player1Roll to pick random 1 to 6' and a 'change rollCounter by 1, block. The else if statement: 'else if rollCounter = 1 then' contains a 'change player1Roll by pick random 1 to 6', a 'radio send number player1Roll', and a 'change rollCounter by 1' block. The else statement contains a 'dangos eicon (cross)' block. Inside the forever loop, all conditions that were originally set to 'player1Roll > 0' are now changed to 'rollCounter = 2'. 'set rollCounter to 0' blocks have also been added to both reset programs (the one activated on button A and the one activated when the radio receives the reset string.)

    Player Two

    A new 'set rollCounter to 0' block has been added to the 'ar ddechrau' loop. The 'on shake' loop is now made up of an if, else if and else statement. The if statement: 'if rollCounter = 0 then' contains a 'set player2Roll to pick random 1 to 6' and a 'change rollCounter by 1, block. The else if statement: 'else if rollCounter = 1 then' contains a 'change player2Roll by pick random 1 to 6', a 'radio send number player2Roll', and a 'change rollCounter by 1' block. The else statement contains a 'dangos eicon (cross)' block. Inside the forever loop, all conditions that were originally set to 'player2Roll > 0' are now changed to 'rollCounter = 2'. 'set rollCounter to 0' blocks have also been added to both reset programs (the one activated on button A and the one activated when the radio receives the reset string.)



Here are some extra features you can add to your dice duelling game. These are for you to investigate and explore - there are no answers provided.

  1. Adding instructions to player screens.

    Gair o gyngor: Try to keep instructions short or in picture form.


  2. Have a score system that tracks how many times each player has won or lost.

    Gair o gyngor: You can replace the programs for a button or use the touch sensor.


  3. Let the players know if they are shaking the Micro:Bit too hard.

    Gair o gyngor: This requires measuring the acceleration of the Micro:Bit, anything over a strength value of 1700mg is too much.


  4. Add animations to the dice rolls.

  5. Can you design a version of this game for two players using only one BBC Micro:Bit?

    Gair o gyngor: This will not need any radio communications.



Gwers Fideo (yn Saesneg):

Ymarferion:

For this session we shall be creating a traffic light program using a Neopixel strip.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


  1. Use the blocks provided by the Neopixel extension to connect a 24 LED strip to pin 1.

    Gair o gyngor: Don't forget to create your own start-up for this new program to identify it.


    • This is part of the set-up for your program, so should be done in the 'ar ddechrau' block.


    An 'ar ddechrau' block holding a 'dangos testun TL' followed by a 'set strip to NeoPixel at pin P1 with 24 leds as RGB (GRB format)' block.

  2. Set up 3 equal size ranges to recreate a traffic light with red at the top, yellow in the middle, and green at the bottom.

    Gair o gyngor: The LED strip numbering starts at 0.


    • 24 is dividable by 3, meaning you'll need to have the ranges cover 8 LEDs each.


    The 'ar ddechrau' now has a 'set range to strip range from 0 with 8 leds', a 'set range2 to strip range from 8 with 8 leds', and a 'set range3 to strip range from 16 with 8 leds' block. There is now a forever loop containing a 'range show color red' followed by 'range2 show color yellow' and 'range3 show color green' blocks.

  3. Have the traffic light repeatedly cycle through a crossing sequence - that is red for 10 seconds, red and yellow for 5 seconds, green for 10 seconds, and then yellow for 5 seconds.


    • Putting this sequence in the 'am byth' loop will ensure it repeats continuously.

    • Remember to turn off the un-needed colours each time (set them to black).


    The forever loop's contents have been changed to read; 'range show color red', 'range2 show color black', 'range3 show color black', 'pause (ms) 10000', 'range show color red', 'range2 show color yellow', 'range3 show color black', 'pause (ms) 5000', 'range show color black', 'range2 show color black', 'range3 show color green', 'pause (ms) 10000', 'range show color black', 'range2 show color yellow', 'range3 show color black', 'pause (ms) 5000',



  1. Move each light change into their own functions, remember to then call them in the forever loop.


    • Give each function a suitable name.


    The traffic light colour blocks have been moved to four different functions, one for red, one for red and yellow (called redYellow), one for green, and one for yellow. The forever loop contents have been changed to read; 'call red', 'pause (ms) 10000', 'call redYellow', 'pause (ms) 5000', 'call green', 'pause (ms) 10000', 'call yellow', 'pause (ms) 5000'.

  2. Add animations to the Micro:Bit to show when it is and isn't safe for pedestrians to cross the road. Do this using three new functions - one for walk, one for wait, and one for warning that the lights are changing to green for the traffic.

    Gair o gyngor: You will need to time your animation to replace the use of pause blocks currently for timing the lights.

    Gair o gyngor: A variable will mean you only need one function for wait that can be used for when the light is green and when it's yellow.


    • This is a good opportunity to practice working with the 'repeat' blocks inside the functions.

    • You can call a function from inside another function.

    • If using two images for your animations, you'll need to repeat them 10 times with half seconds pauses after each for the red and green. Only 5 repeats when the yellow light is active.


    The pause blocks have all been deleted from the forever loop. The 'ar ddechrau' block now holds a 'set repeats to 0' block. There is now a function called walk which contains a 'repeat 10 times' loop holding two images, each followed by a 500ms pause. Another function called warn which holds a 'repeat 5 times' loop holding four images each followed by a 250ms pause. A third new function called wait contains a 'repeat repeats times' loop holding two image blocks each followed by a 500ms pause. The red function has a 'call walk' block added to the end, whilst the redYellow function now has a 'call warn' block added. The yellow function has a 'set repeats to 5' and 'call wait' block added to the end. Finally, the green function has had a 'set repeats to 10' and a 'call wait' block added on the end.




  1. Create a traffic light colour variable for use to identify which light is currently active and use this in if/else if statements for setting the lights.

    Gair o gyngor: Remember to set the variable in the 'ar ddechrau' block.


    • You will need to use numbers to represent the different light states.

    • Use an else statement to capture any errors regarding the variable value.


    There is a new 'set light to 0' added to the 'ar ddechrau' block. The forever loop now reads; 'if light = 0 then', 'call red', 'change light by 1', 'else if light = 1 then', 'call redYellow', 'change light by 1', 'else if light = 2 then', 'call green', 'change light by 1', 'else if light = 3 then', 'call yellow', 'set light to 0', 'else', 'dangos testun Error'.

  2. Have the A button on the Micro:Bit cause the traffic to come to a stop.

    Gair o gyngor: You only need this over-ride to work whilst the traffic lights are green.

    Pwysig: You can stop a loop early using the 'break' block found in the Dolennau menu.



    • You need to tell the program to change to yellow when button A is pressed.

    • To over-ride the program in this way, you will need to check if button A is pressed inside the relevant animation's repeat block.

    • You may also need to check against another variable, this can use a stacked if statement or an 'and' Boolean Operator.


    An 'if button A is pressed and repeats = 10 then' statement containing a 'break' block has been added to the end of the wait function's repeat loop.

  3. Add sound to indicate when it is safe to cross for those who cannot see the visual instructions.


    • This just requires a single note to be played on each image change in the animation for when it is safe to start crossing (when the traffic light is red).


    There is now a 'play tone High D for 1 beat until done' block after each of the images in the walk function.



The below exercise is to expand the program further. This is for you to investigate and explore - there are no answers provided.

  1. Add a second Neopixel to pin 2 and have it act as the traffic light for traffic flowing in the other direction.

  2. How much can you tidy up your code with functions, while loops, repeat blocks, and variables?


Gwers Fideo (yn Saesneg):

Ymarferion:

For this session we shall be creating a program to turn your BBC Micro:Bit into a sound meter using a servo.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


  1. Write a program that plays an alarm when the sound level is over 200.

    Gair o gyngor: Don't forget the start-up routine to identify the program.


    • This requires an if statement with a condition of sound level > 200.

    • There is a variable block for the sound level in the Input menu for the V2.

    • Add some pauses to reduce the rate of your chosen sound/note.


    The 'ar ddechrau' contains a 'dangos testun Loud' block. The forever loop holds 'if sound level > 200 then' statement holding a 'play tone Middle C for 1 beat until done' and a 'pause (ms) 200' block.

  2. Have the Micro:Bit display the sound level on its screen.

    Gair o gyngor: This can be done using a 'dangos testun' block.


    • This needs to be in the forever loop, outside of the current if statement.


    The forever loop now contains a 'dangos testun sound level' block before the if statement.

  3. Notice how the show string block slows down our alarm sound. Let us resolve this by replacing the number with these images and conditions:

    Sound level is 0.


    An image of a Micro:Bit with a blank screen - taken from the Makecode simulator.

    Sound level is over 0
    and
    less than or equal to 51.

    An image of a Micro:Bit with the bottom row of LEDs lit - taken from the Makecode simulator.

    Sound level is over 51
    and
    less than or equal to 102.

    An image of a Micro:Bit with the bottom two rows of LEDs lit - taken from the Makecode simulator.

    Sound level is over 102
    and
    less than or equal to 153.

    An image of a Micro:Bit with the bottom three rows of LEDs lit - taken from the Makecode simulator.

    Sound level is over 153
    and
    less than or equal to 204.

    An image of a Micro:Bit with the bottom four rows of LEDs lit - taken from the Makecode simulator.

    Sound level is over 204.


    An image of a Micro:Bit with all LEDs lit - taken from the Makecode simulator.

    The alarm should now only sound when the meter is full.


    • This will require a new series of if and else if statements.

    • Remember you can use the 'and' Boolean operator instead of stacking if statements.

    • Use the else statement at the end for any errors in the program.


    The forever loop has been re-written to: 'if sound level = 0 then', 'dangos leds' (blank), 'else if sound level > 0 and sound level ≤ 51 then', 'dangos leds' (bottom row lit), 'else if sound level > 51 and sound level ≤ 102 then', 'dangos leds' (bottom two rows lit), 'else if sound level > 102 and sound level ≤ 153 then', 'dangos leds' (bottom three rows lit), 'else if sound level > 153 and sound level ≤ 204 then', 'dangos leds' (bottom four rows lit), 'else if sound level > 204', 'dangos leds' (all lit), 'else', 'dangos testun error'.

  4. Add a 180° positional servo to pin 1. Program the servo to rest at 0° whilst the sound level is less than or equal to 102. Otherwise, it needs to move to 180°

    Gair o gyngor: You will need to use the servo extension.


    • We can use all the default settings for the positional servo for this. However, it is still good practice to set the servo to a start position (0° in this case) in the 'ar ddechrau' block.

    • You will need to set the servo angle in each if/else if statement.

    • When testing, make sure you give the servo time to complete its rotation - if you switch the condition too quickly it will not behave the way we expect it to.


    A 'set servo P1 angle to 0°' block has been added to the 'ar ddechrau' sequence and both the if/else if statements for when the sound level is ≤ 102. The other else if statements have a 'set servo P1 angle to 180°' added to them.



  1. When the servo changes direction, can you have the servo move only 10° every 300 milliseconds? This reduces the stress on the servo, stopping it from breaking - something the simulator does not replicate.

    Gair o gyngor: You will need to make sure the servo does not attempt to turn below 0° or further than 180° - this could break your servo.


    • You will need to create a variable for the angle - remember to set this in the 'ar ddechrau' block.

    • This new variable can be used to set the servo angle - changing it up or down by 10°.

    • When setting up your if statement conditions consider the values to use - as we're moving 10° at a time, consider using 10 and 170.

    • These new if statements will need to replace the set servo blocks in each if/else if statement.

    • As there is already a 200 millisecond pause in the > 204 statement, we need to adjust the movement to total 300.


    A 'set angle to 0' block has been added to the 'ar ddechrau' sequence. Inside the forever loop, each 'set servo P1 angle to 0°' blocks have been replaced with an 'if angle ≥ 10 then' statement containing 'change angle by -10', 'set servo P1 angle to angle°', and 'pause (ms) 300' blocks. The 'set servo P1 angle to 180°' blocks have been replaced with an 'if angle ≤ 170' statement containing 'change angle by 10', 'set servo P1 angle to angle°', and 'pause (ms) 300' blocks. The difference being in the section with the alarm, where we instead used a 'pause (ms) 100' block inside the new if statement.

  2. To tidy up our code, let's move the servo instructions into their own functions and call them when needed.

    Gair o gyngor: You'll need one for lowering and one for increasing the angle.


    • Remember the Functions menu is in the Advanced section.

    • To only have two functions - remove the 200 millisecond second pause from the alarm program so it too can use the 300 milliseconds pause in the servo function.


    The 'if angle ≥ 10 then' statements have been removed and replaced with a 'call quiet' block whilst a copy of this statement and its contents is now in a function called quiet. The same has been done for the opposite movements using a new function called loud. The 'pause (ms) 200' block inside the alarm program has been removed.



Before you start this exercise, we advise that you duplicate your program as we will be deleting a lot of the previous code.

Step 1. Close your program and return to makecode.microbit.org's homepage.

Step 2. Click on the view all option next to 'My Projects'.

Step 3. Select the file you want to duplicate (a green tick should appear on the file).

Step 4. Click on the duplicate option in the top right of the screen. This will allow you to then select a name for your new copy.

Step 5. Open your new copy and continue with the next challenge.

  1. Instead of having a single value determining the movement of the servo, let us look at having the servo act like the arm on a sound meter (as shown below), traveling to the correct angle for the current sound level.

    An illustration of how a servo could be used as a pointer on a sound meter.


    Pwysig: We have a range of 0° to 180° on the servo but the sound level has a range of 0 to 255. There are two ways to approach this:

    Do the maths ourselves

    1. Work out how far the meter needs to move for one unit of sound:

      (180 ÷ 255) = 0.7058823529411°


    2. Use the above value with a Maths block inside a 'set servo' block.

      The makecode block for this - 'set servo P1 angle to sound level x 0.7058823529411°'.

    Use the 'map' block

    1. The map block allows us to enter the range we're measuring (sound level) which is 0 to LED and convert it to the servo's range of 0 to 180. The block then does all the calculations for us.

      The makecode block for this - 'set servo P1 angle to map sound level from low 0 high 255 to low 0 high 180°'.


    • We need to delete the functions we've created for the servo from our program. The 'call' blocks will automatically vanish when their function is removed.

    • Keep the screen meter animation in place.

    • A single 'set servo' block using the method chosen from above is all that's required for this.


    The functions have been removed along with the call blocks. Instead, there is a 'set servo P1 angle to map sound level from low 0 high 255 to low 0 high 180°' inside the forever loop before the if statements for the screen and alarm programs.



The below exercises are designed to expand your understanding of servos. These are for you to investigate and explore - there are no answers provided.

  • Instead of using the meter to measure sound level, have it measure radio signal strength, with an alarm for if the signal gets too weak.

    Gair o gyngor: You will need to find the range of values for the radio signal strength.


  • Remember the traffic light program we created last session? Now you can add a rail crossing with a servo acting as the barrier arm.

    Gair o gyngor: You can only attach the servo to pins 0, 1 or 2. However, you can relocate the Neopixels to other pin numbers (up to 16).



Gwers Fideo (yn Saesneg):

Ymarferion:

For this session we shall be creating a program to control a railway crossing. This includes revision of radio communications, if-statements, variables, functions, Neopixels, and servos.

We recommend you attempt these challenges using the standard editor.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise, you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


For these exercises, imagine you have one Micro:Bit controlling the crossing whilst another is acting as a beacon on-board the train. The crossing will know a train is coming when the beacon's signal strength gets stronger.

  1. Use an image, animation and/or text to create a start-up screen that identifies your program.

    If only using an image, include a pause (500ms) so it remains on the screen long enough to see.


    • Try not to make the start-up take too long, otherwise this will get frustrating during testing as we progress.


    An 'on start' program containing a 'dangos leds' and a 'pause (ms) 500' block.

  2. Have the Micro:Bit send the number zero via radio from inside the forever loop.

    Gair o gyngor: Don't forget to set a radio group for your Micro:Bit to use.


    • You can find the blocks you need in the Radio menu.

    • You need to set a radio group in the start-up sequence.


    A 'radio set group 33' block has been added to the 'on start' program. There is now a 'forever' loop containing a 'radio send number 0' block.

  3. Create a beacon strength variable and have it set to the signal strength being received.

    Gair o gyngor: To measure the signal strength, the program needs to first receive the sent number.


    • Pick a suitable name for your new variable. Some programs use lots of variables, and it helps to be able to quickly identify what each one is for.

    • Remember to set your new variable to 0 when the program first starts - This is good practice for when you start learning text-based programming languages.

    • You will need to set your new variable to the received signal strength value inside an 'on radio received' block.


    A 'set variable to 0' block has been added to the 'on start' program. There is now a new loop using 'on radio received receivedNumber' which contains a 'set variable to received packet signal strength' block.

  4. We now need two functions, one for the program for when a train is coming, the other for when there isn't. Give each function a suitable name and have a different image in each.

    Gair o gyngor: The images at this stage are for testing, so any will do.


    • The Functions menu can be found in the Advanced section.

    • These functions will not show in testing, as we have not called them in the program.


    There is now a function called trainComing which contains a 'dangos eicon' block for a tick, and another called noTrain with a 'dangos eicon' block for a cross.

  5. Call your function for when a train is coming when the signal strength is greater than or equal to -75. Otherwise, call the no train coming function.

    Gair o gyngor: While testing in the simulator, treat the top Micro:Bit as the crossing controller and the bottom Micro:Bit as the train beacon.


    • You will need to create an if statement for this which compares the beacon strength value to -75.

    • The radio signal strength for the Micro:Bit in the simulator has a range of -128 (weakest signal) to -42 (strongest signal).

    • Remember we have several maths symbols to choose from: = equals, < less than, > greater than, ≤ less than or equal to, and ≥ greater than or equal to.


    The 'forever' loop has had an if/else statement added. The if statement holds the comparison of 'beaconStrength ≥ -75' and holds a 'call trainComing' block. The else statement contains a 'call noTrain' block.



  1. We now need to create our crossing lights. For this we need a Neopixel connected to pin 1 which is made up of 15 LEDs.

    Gair o gyngor: You'll need to add the Neopixel extension to your blocks menu.


    • Re-visit Session Four if you need a reminder of how to get and use the Neopixel extension blocks.

    • You will need to change the default length from 24 to 15 LEDs.


    A 'set strip to NeoPixel at P1 with 15 leds as RGB (GRB format)' block as been added to the end of the program in the 'on start' section.

  2. Set up two ranges, with suitable names - one for the top 5 LEDs and another for the bottom 5 LEDs.

    Gair o gyngor: Remember that counting in computing starts at zero.


    • The first range will start at 0, whilst the bottom range will start at 10.


    There are now 2 'set range' blocks in the on-start program. They are called topLight and bottomLight. The topLight range is 'set to strip range 0 with 5 leds' whilst the bottomLight range is 'set to strip range 10 with 5 leds'.

  3. Remove the image in your function for when a train is coming and replace it with a program that will turn the lights on and off (alternating between the top and the bottom) three times. Include a pause of 500ms after each light turns on.

    Gair o gyngor: Remember that setting a light to black turns it off.


    • Each time a light turns on, the other needs to turn off.

    • You can use a 'ailadfrodd 3 o weithiau' block to shorten the program.


    The program now inside the trainComing function is contained in a 'ailadfrodd 3 o weithiau' block. The block order is: 'topLight show color red', 'bottomLight show color black', 'pause (ms) 500', 'topLight show color black', 'bottomLight show color red' and 'pause (ms) 500'.

  4. Replace the image in you function for when a train is not approaching with a program to make sure the lights are all turned off.


    • You will need to set both of our light ranges to black in the function for when there is no train coming.


    The function for when a train is not approaching now holds two blocks: 'topLight show color black' and 'bottomLight show color black'.

  5. Have your program play a middle C note when the top light is on and a middle E note when the bottom light is on. This is our alarm to warn drivers and pedestrians a train is coming.

    Gair o gyngor: When testing, make sure the top Micro:Bit (our 'crossing') has a very low signal strength or the alarms on both Micro:Bits may merge and/or be out of sync.


    • You only want the notes to be 1 beat long.

    • You can change the note inside the 'play tone' block.


    There is now a 'play tone Middle C for 1 beat until done' block before the first pause in the function for when a train is coming. There is then a matching block with Middle E selected inserted above the second pause.



  1. For our railway crossing barrier we shall use a servo to control its movement. This needs to be connected to pin 2 and be in the open position (0°) at the start of the program.

    Gair o gyngor: This will require the Servos extension.


    • If stuck, please look back at Session Five.


    There is now a 'set servo P2 angle to 0°' block inside the 'on start' program.

  2. When a train is approaching, we want the lights and alarm to sound three times and then for the barrier to close (move to 90°).


    • Be careful not to place this new instruction inside the repeat loop.

    • Remember that the servo is on pin 2.


    The function for when a train is coming now has a 'set servo P2 angle to 90°' block after the repeat loop.

  3. Once the barrier is closed, we need the alarm to stop while the lights continue to flash.

    Gair o gyngor: You will need a way to check the angle of the servo.


    • You will need a servo angle variable.

    • This new variable will need to be updated each time the barrier opens or closes.

    • Move the program with the lights and alarm into an if statement for when the barrier is open.

    • For when the barrier is closed, have the lights continue to flash. As long as the barrier is down, this will keep repeating, so you don't need to use a repeat block here.


    There is now a 'set servoAngle to 0' variable block in the 'on start' program. The function for a train coming now has its previous program wrapped in an 'if servoAngle = 0 then' statement with an additional 'set servoAngle to 90' block at the end. Inside the else statement for this is the same light flashing program as created in part 3 of the silver exercises, without the repeat loop.

  4. Once the train has passed and the beacon signal fades to less than 75 again, we want the barrier to open (retuning to 0°).

    Gair o gyngor: Remember to also reset your new angle variable at the same time.


    • You can use the angle variable to tell if the gate needs opening. This is the opposite to the check we've already done to see if it needs closing.

    • Don't forget that our servo is attached to pin 2.


    There is now an if statement inside the function for when there is not train coming. This if statement has the condition of if the angle variable is equal to 90. Inside this there are two blocks: 'set servo P2 angle to 0°' and 'set (angle variable) to 0'.

  5. At the moment, the servo is moving too fast. This could cause injury to a pedestrian or damage a passing vehicle. So we need the barrier to lower 5° every 200ms. When it is opening, it is safer to go faster - in this case 20° every 300ms.

    Gair o gyngor: Turn both the red lights on fully during these movements as the flashing will stop during these movements.


    • Our function for when our train is coming will need an else if statement for when the barrier is only partially closed.

    • You will need to use the angle variable in the maths conditions.

    • Remember you can also use the 'and' Boolean block demonstrated previously if you want to check against two values.

    • Perhaps create new functions for the opening and closing movements. Yes, you can call a function inside another function.

    • When opening the barrier be aware that 90 cannot be divided by 20. Think about how you will make sure the barrier opens fully without going too far and breaking the servo.


    There is now a new function called closing which holds the following program: 'topLight show color red', 'bottomLight show color red', 'change servoAngle by 5', 'set servo P2 angle to servoAngle°', 'pause(ms) 200'. There is also a new function called opening which holds an if statement for when the servo angle is greater than or equal to 20. Inside this statement is the following: 'topLight show color red', 'bottomLight show color red', 'change servoAngle by 20', 'set servo P2 angle to servoAngle°', 'pause(ms) 300'. The else statement that follows holds a 'set servoAngle to 0' and a 'set servo P2 angle to servoAngle°' block. Inside the function for when a train is coming there are some changes. The if statement now has a 'call' block for the closing function instead of the servo movement blocks. There is now an else if statement following this with the condition that the servo angle is greater than 0 and less than 90. Inside this there is also a 'call' block for the closing function. The final change is in the function for when no train is approaching where the if statement condition has been changed to when the servo angle is greater than 0 and the blocks contained replaced with a single 'call' block for the opening function.


This section is an additional activity for you to try out - this will provide an opportunity for revision whilst introducing some new blocks and terms.

As with all our exercises, it is best to work through this is order.

Select a heading to expand.


  1. Create a basic dice rolling program on your Micro:Bit. This should show a random number between 1 and 6 on the screen when shaken.

    Gair o gyngor: Don't forget your start up screen.


    • If stuck, revisit sessions two and three.

    • This does not require a forever loop.


    The 'ar ddechrau' holds a 'dangos testun Dice' block. There is no forever loop. Instead, we have an 'on shake' input with a 'dangos testun pick random 1 to 6' block inside.

  2. Instead of showing a number, have the screen show the following images depending on the value rolled.

    One

    The screen has the centre LED lit.

    Two

    The screen has two LEDs lit in a diagonal.

    Three

    The screen has three LEDs lit in a diagonal.

    Four

    The screen has four LEDs lit in a square.

    Five

    The screen has five LEDs lit in a cross.

    Six

    The screen six LEDs lit in a rectangle.

    Gair o gyngor: You will need to create a variable to hold the dice roll value.


    • You will need a different if/else if statement for each possible value. You can then use the else statement to catch any errors.


    There is now a 'set diceRoll to 0' block in the 'ar ddechrau'. In the 'on shake' program, the 'dangos testun' block has been replaced with a 'set diceRoll to pick random 1 to 6' block. Below this is an if statement with five else if statements and an else statement attached. There is a condition for each possible dice roll value with an instruction inside to show the correct image. The else statement holds a 'dangos testun error' block.



A Magic-8 Ball is like a dice, put instead of numbers it produces answers when shaken.

  1. Instead of showing numbers in your program, have the Micro:Bit display the answers yes, no, maybe, go for it, good idea, bad idea, don't know, definitely, absolutely not, and no idea.

    Gair o gyngor: There are now 10 options.


    • For the first six options you can simply replace the current images.

    • You will then need to add four more else if statements.

    • Don't forget to change the random number generator to have 10 options instead of 6.


    No changes to the 'ar ddechrau' sequence. In the 'on shake' program, the 'pick random' block has been changed to the range of 1 to 10. Four additional 'else if' statements have been added for the values of 7, 8, 9, and 10. Each condition triggers one of the asked for responses. The 'else' statement remains the same.



There is a different way we could have approached this program. We could have used something called an array. Array blocks can be found in the Advanced menu.

An array is a special kind of variable. Instead of holding only a single piece of information like the variables we've used, it can hold a list of values/strings.

You can then call a value from the array by its location in the list. As with most coding, the numbering of list items starts at zero.

Example:

Let's say we wanted to write a program that randomly selected a string from a list of apple, orange, and banana when we press button A.

  1. As with any variable, we need to set the array in the 'ar ddechrau' section:

    An 'ar ddechrau' block containing a 'set text list to array of apple orange banana' block.

    We can add or take away items to the array using the plus and minus buttons on the block.


  2. To have the screen show a random item from the array we can use the 'dangos testun' combined with a 'get random value from' array block.

    There is now an 'ar fotwm A wedi'i wasgu' program containing a 'dangos testun get random value from fruit' block.

    We now have a complete program that randomly selects an item from the list and displays it on the screen when we press button A.

As the Magic-8 Ball program has a single variable which has a list of different strings (the answers) we could use an array and have the program randomly select from a list instead of using such a large if/else if/else statement.

Pwysig: Arrays cannot be used to replace all if and else if statements. With Makecode, they can only be used for lists of strings or numbers. For example, you could not use this for the dice program with different images for each value.


  1. Change your Magic-8 Ball program to use an array for all possible answers and randomly select from it when shaken.

    Gair o gyngor: You may want to duplicate your file first so you can compare them later.


    • Use the fruit list program in the example and replace the fruit with the Magic-8 Ball answers provided in the silver exercise.

    • Make sure you are using the text list block, the number version will error if you add words.

    • Remember, you can add more items to the array using the plus symbol on the set-up block.

    • Rename the list to something more suitable than fruit.

    • If you haven't already, re-design the start sequence for the Magic-8 Ball instead of the dice.


    The 'ar ddechrau' program has a 'dangos testun 8' block followed by a 'set text list to array of' block containing all ten possible answers. The 'on shake block now only contains one block - 'dangos testun get random value from text list'.


Now that you've discovered random generators using arrays, there are number of different fun things you can create. Here are some ideas.