Micro:bit IoT In C

We have decided not to make the programs available as a download because this is not the point of the book - the programs are not finished production code but something you should type in and study.

There is also the problem of what happens when NetBeans or mbed changes and the downloads no longer work. 

The best solution is to provide the source code of the programs in a form that can be copied and pasted into a NetBeans or mbed project. This means that you have some interaction with the code rather than just running it and it means that it is immune to changes in the IDEs.

The only downside is that you have to create a project to paste the code into.

To do this follow the instruction in chapter 2 for mbed and chapter 3 for NetBeans

If anything you consider important is missing or if you have any requests or comments  contact me

This email address is being protected from spambots. You need JavaScript enabled to view it.

page 14

#include "MicroBit.h"
MicroBit uBit; int main() {
 uBit.init();
 uBit.display.scroll("NEW PROGRAM");
 while (1) {
  uBit.io.P0.setDigitalValue(1);
  uBit.io.P0.setDigitalValue(0);
 }
 release_fiber();
}

page 22

#include "MicroBit.h" MicroBit uBit; int main() {
 uBit.init();
 uBit.display.scroll("NEW PROGRAM");
 release_fiber();
}

page 58

#include "MicroBit.h"
MicroBitSerial serial(USBTX, USBRX); 
int main() {
    volatile unsigned int*in=(unsigned int *)(0x50000000UL+0x510);
    volatile unsigned int*pin3=(unsigned int *)(0x50000000UL+0x70C);
    unsigned int mask = 1 << 3;
    *pin3=0x0;
    volatile int i;
    while (1) {
        while (mask == ((*in) & mask)) {};
        while (mask != ((*in) & mask)) {};
        for (i = 0; i < 5000; i++) {
            if (mask != ((*in) & mask)) break;
        }
        serial.printf("%d\n\r", i);
    }
    return 0;
}

page 65

#include "MicroBit.h"
MicroBit uBit;

int main() {
    uBit.init();
    uBit.io.P0.setAnalogValue(200);
    uBit.io.P1.setAnalogValue(511);
    uBit.io.P2.setAnalogValue(800);
 
    uBit.io.P0.setAnalogPeriod(1);
    uBit.io.P1.setAnalogPeriod(1);
    uBit.io.P2.setAnalogPeriod(1);
   
    release_fiber();
    return 0;
}

page 68

#include "MicroBit.h"
MicroBit uBit;

int main() {
   int i;
   uBit.init();
   uBit.io.P0.setAnalogValue(200);
   uBit.io.P0.setAnalogPeriodUs(50);
   for(;;){    
       for(i=0;i<1024;i=i+10){
               uBit.io.P0.setAnalogValue(i);
               wait_us(100);
        }
   }
   release_fiber();
   return 0;
}

page 72

int main() {     
    uBit.init();    
    uBit.io.P0.setAnalogValue(511);
    uBit.io.P0.setAnalogPeriodUs(1000);
    int w=1;
    int inc=1;
    for (;;) {
       uBit.io.P0.setAnalogValue(w);
       w=w+inc;
       if(w>1024 || w<=0)inc=-inc;
       wait_us(5000);
    }     
    release_fiber();
    return 0;
}

page 73

#include "MicroBit.h"
MicroBit uBit; 

int main() {  
    uBit.init();
    uBit.io.P0.setAnalogValue(511);
    uBit.io.P0.setAnalogPeriodUs(1000);
    
    int w=0;
    int inc=1;
    for (;;) {
       uBit.io.P0.setAnalogValue(w*w*w);
       w=w+inc;
       if(w>10 || w<=0)inc=-inc;
       wait_us(50000);
    }    
    release_fiber();
    return 0;
}

page 76

#include "MicroBit.h"
MicroBit uBit;

int main() {
    uBit.init();
    for (;;) {
        uBit.io.P0.setServoValue(0);
        uBit.sleep(1000);
        uBit.io.P0.setServoValue(90);
        uBit.sleep(1000);
        uBit.io.P0.setServoValue(180);
        uBit.sleep(1000);
    }
    release_fiber();
    return 0;
}

page 77

#include "MicroBit.h"
MicroBit uBit;

int main() {
    uBit.init();
    uBit.io.P0.setAnalogValue(0);
    uBit.io.P0.setAnalogPeriod(20);
   for (;;) {
    uBit.io.P0.setAnalogValue(52 * 0 / 180 + 920);
    uBit.sleep(1000);
    uBit.io.P0.setAnalogValue(52 * 90 / 180 + 920);
    uBit.sleep(1000);
    uBit.io.P0.setAnalogValue(52 * 180 / 180 + 920);
    uBit.sleep(1000);
  }
  release_fiber();
  return 0;
}

page 79

#include "MicroBit.h"
MicroBit uBit;
int main() {
    uBit.init();
    for (;;) {
        uBit.io.P0.setServoValue(0,1000);
        uBit.sleep(1000);
        uBit.io.P0.setServoValue(90,1000);
        uBit.sleep(1000);
        uBit.io.P0.setServoValue(180,1000);
        uBit.sleep(1000);
    }
    release_fiber();
    return 0;
}

page 88

#include "MicroBit.h"
MicroBit uBit;

int main() {
    uBit.init();    
    char buf[]={0x07};
    uBit.i2c.write(0x1C,buf,1,true);
    uBit.i2c.read(0x1C,buf,1);
    printf("Id %X\r\n",(int)buf[0]);
    release_fiber();
    return 0;
}

page 97

#include "MicroBit.h"
MicroBit uBit; int main() {

    uBit.init();
    char buf[4] = {0xE3};
    uBit.i2c.write(0x80, buf, 1);
    uBit.i2c.read(0x80, buf, 3);
    uint8_t msb = buf[0];
    uint8_t lsb = buf[1];
    uint8_t check = buf[2];
    printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check);
    unsigned int data16 = ((unsigned int) msb << 8) |(unsigned int) (lsb & 0xFC);
    int temp = -4685 + ((17572 * data16) >> 16);
    printf("Temperature %d.%d C \n\r", temp / 100, temp % 100);
    release_fiber();
    return 0;
}

page 100

#include "MicroBit.h"
MicroBit uBit;
uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check);

int main() {
    uBit.init();

    char buf[4] = {0xE3};
    uBit.i2c.write(0x80, buf, 1);
    uBit.i2c.read(0x80, buf, 3);
    uint8_t msb = buf[0];
    uint8_t lsb = buf[1];
    uint8_t check = buf[2];

    printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check);
    printf("crc %d \n ", crcCheck(msb,lsb,check));
   
    unsigned int data16 = ((unsigned int) msb << 8) | (unsigned int) (lsb & 0xFC);
    int temp = -4685 + ((17572 * data16) >> 16);
    printf("Temperature %d.%d C \n\r", temp / 100, temp % 100);

    buf[0] = 0xE5;
    uBit.i2c.write(0x80, buf, 1);
    uBit.i2c.read(0x80, buf, 3);
    msb = buf[0];
    lsb = buf[1];
    check = buf[2];
    printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check);
    printf("crc %d \n ", crcCheck(msb,lsb,check));
    
    data16 = ((unsigned int) msb << 8) |  (unsigned int) (lsb & 0xFC);
    int hum = -600 + ((12500 * data16) >> 16);
    printf("Humidity %d.%d %% \n\r", hum / 100, hum % 100);

    uint32_t data32 = ((uint32_t) msb << 16) | ((uint32_t) lsb << 8) | (uint32_t) check;

    release_fiber();
    return 0;
}


uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check) {
    uint32_t data32 = ((uint32_t) msb << 16)| ((uint32_t) lsb << 8) | (uint32_t) check;
    uint32_t divisor = 0x988000;
    for (int i = 0; i < 16; i++) {
        if (data32 & (uint32_t) 1 << (23 - i))
            data32 ^= divisor;
        divisor >>= 1;
    };
    return (uint8_t) data32;
}

page 106

#include "MicroBit.h"
MicroBit uBit;
MicroBitPin P0=uBit.io.P0;;
 int main() {
 uBit.init();
 P0.setDigitalValue(1);
 P0.setDigitalValue(0);
 volatile int i;
 for (i = 0; i < 1200; i++) {
 };
 int b = P0.getDigitalValue();  
}

page 111

#include "MicroBit.h" 
uint getByte(int, int[]);
MicroBit uBit;
MicroBitPin P0 = uBit.io.P0;

int main() {
    uBit.init();
    P0.setDigitalValue(1);
    for(;;){
     P0.setDigitalValue(0);
     volatile int i;
     uBit.sleep(1);
     P0.getDigitalValue();
     for (i = 1; i < 200; i++) {
       if (P0.getDigitalValue() == 0)break;
     };  
     int buf[41];
     int j;
     for (j = 0; j < 41; j++) {
        for (i = 1; i < 200; i++) {
            if (P0.getDigitalValue() == 1)break;
        };
        for (i = 1; i < 200; i++) {
            if (P0.getDigitalValue() == 0) break;
        }
        buf[j] = 0;
        if (i > 11)buf[j] = 1;
     }
     uint byte1 = getByte(1, buf);
     uint byte2 = getByte(2, buf);
     uint byte3 = getByte(3, buf);
     uint byte4 = getByte(4, buf);
     uint byte5 = getByte(5, buf);
    
     int16_t humidity = (byte1 << 8) | byte2;
     int16_t temperature = (byte3 << 8) | byte4;
     if(temperature & 0x8000)
            temperature=-(temperature & 0x7FFF);
    

     printf("Checksum %X %X \n\r",  byte5, (byte1 + byte2 + byte3 + byte4) & 0xFF);
     printf("Humidity %hd.%hd%%\n\r",  humidity / 10, abs(humidity % 10));
     printf("Temperature %hd.%hdC\n\r",  temperature / 10, abs(temperature % 10));
     char buff[10];
     sprintf(buff,"%hd.%hd%%",humidity / 10, abs(humidity % 10));
     uBit.display.scroll(buff, 200);
     sprintf(buff,"%hd.%hdC",temperature / 10, abs(temperature%10));
     uBit.display.scroll(buff, 200);
     uBit.sleep(2500);
   }
 } 

uint getByte(int b, int buf[]) {
    int i;
    uint result = 0;
    b = (b - 1)*8 + 1;
    for (i = b; i <= b + 7; i++) {
        result = result << 1;
        result = result | buf[i];
    }
    return result;
}

page 127

#include "MicroBit.h" 

int init();
void sendskip();
void writeBit(int);
void sendOne();
void sendZero();
void writeByte(int);
int readBit();
int convert();
int readByte();

MicroBit uBit;

MicroBitPin P0 = uBit.io.P0;
MicroBitPin P1 = uBit.io.P1;

int main() {
 uBit.init();

 P0.setDigitalValue(1); 
 P0.setPull(PullUp);
 P1.getDigitalValue();
 uBit.sleep(1);
 while (1) {
  init();
  writeByte(0xCC);
  int r = convert();
  init();
  writeByte(0xCC);
  writeByte(0xBE);
  int b1 = readByte();
  int b2 = readByte();
  int16_t temp = (b2 << 8 | b1);  temp = temp * 100 / 16;
  char buff[10];
  sprintf(buff, "%d.%d", temp / 100, abs(temp % 100));
  uBit.display.scroll(buff, 200);
  uBit.sleep(1000);
 }
 release_fiber();
} 

int init() {
 volatile int i;P0.setDigitalValue(0);
 for (i = 0; i < 600; i++) {};
 P0.setDigitalValue(1);
 for (i = 0; i < 30; i++) {};
 int b = P1.getDigitalValue();
 for (i = 0; i < 600; i++) {};
 return b;
} 

void sendZero() {
 volatile int i;
 P0.setDigitalValue(0);
 for (i = 1; i < 75; i++) {};
 P0.setDigitalValue(1);
 for (i = 1; i < 6; i++) {};
} 

void sendOne() {
 volatile int i;
 P0.setDigitalValue(0);
 for (i = 1; i < 1; i++) {};
 P0.setDigitalValue(1);
 for (i = 1; i < 80; i++) {};
} 

void writeBit(int b) {
 volatile int i;
 int delay1, delay2;
 if (b == 1) {
  delay1 = 1;
  delay2 = 80;
 } else {
  delay1 = 75;
  delay2 = 6;
 }
 P0.setDigitalValue(0);
 for (i = 1; i < delay1; i++) {};
 P0.setDigitalValue(1);
 for (i = 1; i < delay2; i++) {};
} 

void sendskip() {
 writeBit(0);
 writeBit(0);
 writeBit(1);
 writeBit(1);
 writeBit(0);
 writeBit(0);
 writeBit(1);
 writeBit(1);
}

void writeByte(int byte) {
 int i;
 for (i = 0; i < 8; i++) {
  if (byte & 1) {
   writeBit(1);
  } else {
   writeBit(0);
  }
  byte = byte >> 1;
 }
}

int readBit() {
 volatile int i;
 P0.setDigitalValue(0);
 P0.setDigitalValue(1);
 for (i = 1; i < 20; i++) {};
 int b = P1.getDigitalValue();
 for (i = 1; i < 60; i++) {};
 return b;
} 

int convert() {
 volatile int i;
 int j;
 writeByte(0x44);
 for (j = 1; j < 1000; j++) {
  for (i = 1; i < 900; i++) {};
  if (readBit() == 1)break;
 };
 return (j);
} 

int readByte() {
 int byte = 0;
 int i;
 for (i = 0; i < 8; i++) {
  byte = byte | readBit() << i;
 };
 return byte;
} 

page 137

#include "MicroBit.h"
SPI spi(MOSI, MISO, SCK);
MicroBitSerial serial(USBTX, USBRX);
MicroBitPin CS(MICROBIT_ID_IO_P16, MICROBIT_PIN_P16, PIN_CAPABILITY_DIGITAL);

int main() {
    CS.setDigitalValue(1);
    spi.format(8, 0);
    spi.frequency(1000000);
    CS.setDigitalValue(0);
    uint8_t read_data = spi.write(0xAA);
    CS.setDigitalValue(1);
    if (read_data == 0xAA) printf("data received correctly");
    release_fiber();
    return 0;
}

page 165

#include "MicroBit.h"

MicroBit uBit;

void initWiFi();
int ATWiFi();
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);

#define Tx MICROBIT_PIN_P0
#define Rx MICROBIT_PIN_P1

#define DEBUG 1

int main() {
    initWiFi();
    ATWiFi();
    while (1)uBit.sleep(1000);
    release_fiber();
}
void initWiFi() {
    uBit.serial.redirect(Tx, Rx);
    uBit.serial.baud(115200);
    uBit.serial.setRxBufferSize(500);
}

int ATWiFi() {
    uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
    uBit.sleep(150);
    ManagedString s = uBit.serial.read(500, ASYNC);
    if (DEBUG)debug("\n\rAT \n\r" + s + "\n\r");
    return find("OK", s);
}

void debug(ManagedString s) {
    uBit.serial.redirect(USBTX, USBRX);
    uBit.serial.send(s, SYNC_SPINWAIT);
    uBit.serial.redirect(Tx, Rx);
}


int find(ManagedString c, ManagedString s) {
    int i;

    for (i = 0; i < (s.length() - c.length()); i++) {
        if (c == s.substring(i, c.length())) break;
    }

    if (i == (s.length() - c.length())) return 0;
    return 1;
}

page 178

#include "MicroBit.h"

MicroBit uBit;
void initWiFi();
int ATWiFi();
int resetWiFi();
int setUARTWiFi();
int scanWiFi();
int getIPWiFi();
int modeWiFi(int mode);
int connectWiFi(ManagedString ssid, ManagedString pass);
int getWebPageWiFi(ManagedString URL, ManagedString page);
int getVersionWiFi();
int startServerWiFi();

int waitForWiFi(ManagedString target, int retry, int pause);
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);

#define Tx MICROBIT_PIN_P0
#define Rx MICROBIT_PIN_P1

#define DEBUG 1

int main() {
    uBit.init();
    initWiFi();
    modeWiFi(1);
    connectWiFi("ssid", "password");
    getIPWiFi();
    startServerWiFi();
    release_fiber();
}

void initWiFi() {
    uBit.serial.redirect(Tx, Rx);
    uBit.serial.baud(115200);
    uBit.serial.setRxBufferSize(500);
}

int ATWiFi() {
    uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
    return waitForWiFi("OK", 150, 10);
}


int getVersionWiFi() {
    uBit.serial.send("AT+GMR\r\n", SYNC_SPINWAIT);
    return waitForWiFi("OK", 200, 10);
}

int resetWiFi() {
    uBit.serial.send("AT+RST\r\n", SYNC_SPINWAIT);
    return waitForWiFi("OK", 1000, 10);
}

int setUARTWiFi() {
  uBit.serial.send("AT+UART_CUR=115200,8,1,0,0\r\n", SYNC_SPINWAIT);
  return waitForWiFi("OK", 200, 10);
}

int scanWiFi() {
    uBit.serial.send("AT+CWLAP\r\n", SYNC_SPINWAIT);
    return waitForWiFi("OK", 500, 50);
}

int modeWiFi(int mode) {
    ManagedString cmd="AT+CWMODE_CUR="+ManagedString(mode)+"\r\n";
    uBit.serial.send(cmd, SYNC_SPINWAIT);
    return waitForWiFi("OK", 200, 10);
}

int connectWiFi(ManagedString ssid, ManagedString pass) {
   ManagedString cmd = "AT+CWJAP_CUR=\""+ssid+"\",\""+pass+"\"\r\n";
   uBit.serial.send(cmd, SYNC_SPINWAIT);
   return waitForWiFi("OK", 200, 20);
}

int getIPWiFi() {
    uBit.serial.send("AT+CIFSR\r\n", SYNC_SPINWAIT);
    return waitForWiFi("OK", 200, 10);
}


int getWebPageWiFi(ManagedString URL, ManagedString page) {
  ManagedString cmd="AT+CIPSTART=\"TCP\",\""+URL+"\",80\r\n";
  uBit.serial.send(cmd, SYNC_SPINWAIT);
  if (waitForWiFi("OK", 100, 20) == 0) return 0;

  ManagedString http="GET "+page+"HTTP/1.0\r\nHost:"+URL+"\r\n\r\n";
  cmd = "AT+CIPSEND=" + ManagedString(http.length()) + "\r\n";
  uBit.serial.send(cmd, SYNC_SPINWAIT);
  int retry;
  ManagedString s;
  s = "";
  retry = 40;
  do {
      uBit.sleep(100);
      s = s + uBit.serial.read(500, ASYNC);
      retry--;
     } while (find(">", s) == 0 && retry != 0);

  uBit.serial.send(http, SYNC_SPINWAIT);
  retry = 100;
  do {
      uBit.sleep(100);
      s = s + uBit.serial.read(500, ASYNC);
      retry--;
    } while (s.length() < 500 && retry != 0);

  if (DEBUG)debug("\n\rPage\n\r" + s + "\n\r");
  return 1;
}

int startServerWiFi() {
    uBit.serial.send("AT+CIPMUX=1\r\n", SYNC_SPINWAIT);
    if (waitForWiFi("OK", 100, 20) == 0) return 0;

    uBit.serial.send("AT+CIPSERVER=1,80\r\n", SYNC_SPINWAIT);
    if (waitForWiFi("OK", 100, 20) == 0) return 0;

    ManagedString s;

    for (;;) {
        s="";
        do {
            uBit.sleep(100);
            if(s>500)s="";
            s = uBit.serial.read(500, ASYNC);
        } while (find("+IPD", s) == 0);
        if (DEBUG)debug("\n\rClient Connected\n\r" + s + "\n\r");

        
int b = find("+IPD", s);
        s = s.substring(b + 1, s.length());
        b = find(",", s);
        s = s.substring(b + 1, s.length());
        b = find(",", s);
        ManagedString id = s.substring(0, b);
        if (DEBUG)debug("\n\rTCP id:" + id + "\n\r");

        ManagedString headers = "HTTP/1.0 200 OK\r\n";
        headers = headers + "Server: micro:bit\r\n";
        headers = headers + "Content-type: text/html\r\n\r\n";

        ManagedString html ="Temperature{\"humidity\":81%,\"airtemperature\":23.5C}\r\n";
        ManagedString data = headers + html;

        ManagedString cmd = "AT+CIPSEND=" + id + "," + ManagedString(data.length()) + "\r\n";
        uBit.serial.send(cmd, SYNC_SPINWAIT);
        s = "";
        int retry = 40;
        do {
            uBit.sleep(100);
            s = s + uBit.serial.read(500, ASYNC);
            retry--;
        } while (find(">", s) == 0 && retry != 0);

        uBit.serial.send(data, SYNC_SPINWAIT);

        if (waitForWiFi("OK", 100, 100) == 0) return 0;
        if (DEBUG)debug("\n\rData Sent\n\r");

        cmd = "AT+CIPCLOSE=" + id + "\r\n";
        uBit.serial.send(cmd, SYNC_SPINWAIT);
        if (waitForWiFi("OK", 100, 100) == 0) return 0;
    }

}

void debug(ManagedString s) {
    uBit.serial.redirect(USBTX, USBRX);
    uBit.serial.send(s, SYNC_SPINWAIT);
    uBit.serial.redirect(Tx, Rx);
}


int find(ManagedString c, ManagedString s) {
    int i;

    for (i = 0; i < (s.length() - c.length()); i++) {
        if (c == s.substring(i, c.length())) break;
    }

    if (i == (s.length() - c.length())) return 0;
    return i;
}

int waitForWiFi(ManagedString target, int retry, int pause) {
    ManagedString s;
    do {
        uBit.sleep(pause);
        if(s.length()>500)s="";
        s = s + uBit.serial.read(500, ASYNC);
        retry--;
    } while (find(target, s) == 0 && retry != 0);
    if (DEBUG)debug("\n\r" + s + "\n\r");
    return retry;
}

page 185

#include "MicroBit.h"
MicroBit uBit;
int main() {
   uBit.init();
   uBit.display.disable();
   
   DigitalOut col3(P0_6,0);
   DigitalOut row2(P0_14);
    while(1) {
        row2 = 1;
        wait(0.2);
        row2 = 0;
        wait(0.2);
    }

   release_fiber();
    return 0;
}

page 189

#include "MicroBit.h"

MicroBit uBit;
int clickCount = 0;

void startGame();
int playGame();
void endGame(int position);void countDown();
void drawPlayArea();
void flashMan(int x, int y, int p, int vx, int vy);
void setUpButton();
void moveHorizontal();
void moveVertical(int x, int h);
void buttonClickCount(MicroBitEvent e);

int main() {
 uBit.init();
 startGame();
 int position = playGame();
 endGame(position);
 release_fiber();
 return 0;
} 

void startGame() {
 countDown();
 uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
 drawPlayArea();
 setUpButton();
} 

void countDown() {int t;
 for (t = 5; t >= 0; t--) {
  uBit.display.scroll(t);
 }
 uBit.display.scroll("!");
} 

void drawPlayArea() {
 uBit.display.clear();
 int y;
 for (y = 1; y <= 5; y++) {
  uBit.display.image.setPixelValue(2, y, 255);
 }
} 


int playGame() {
 int position = 4;
 clickCount = 0;
 int t1 = uBit.systemTime();

 while (uBit.systemTime() - t1 < 20000) {
   flashMan(4,  position, clickCount, 0, -1);
   if (clickCount > 10) {
     position = position - 1;
     clickCount = 0;
     if (position == 0)break;
   }
 }
 return position; 
} 

void endGame(int position) {
 if (position == 0) {
  moveHorizontal();
  moveVertical(0, 0);
 } else {
  moveVertical(4, position);
 }
} 

void setUpButton() {
 uBit.buttonB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, buttonClickCount);
} 

void buttonClickCount(MicroBitEvent e) {
 clickCount++; 
} 

void flashMan(int x, int y, int p, int vx, int vy) {
 uBit.display.image.setPixelValue(x, y, 0);
 uBit.display.image.setPixelValue(x + vx, y + vy, 0);
 uBit.sleep(100);
 uBit.display.image.setPixelValue(x, y, 25 * (10 - p));
 uBit.display.image.setPixelValue(x+vx, y+vy, 25*p);
 uBit.sleep(100); 
} 

void moveHorizontal() {
 int x, fraction;
 for (x = 4; x >= 0; x--) {
  for (fraction = 0; fraction <= 10; fraction++) {
   flashMan(x, 0, fraction, -1, 0);
  }
 }
} 

void moveVertical(int x, int h) {
 int y, fraction;
 if (h != 0) 
  uBit.display.image.setPixelValue(x, h - 1, 0);
  for (y = h; y <= 4; y++) {
    for (fraction = 0;
     fraction <= 10; fraction++) {
     flashMan(x, y, fraction, 0, 1);
    }
  }
}