Inventor's kit experiment four: Using a transistor to drive a motor.

Dependencies:   microbit

main.cpp

Committer:
haraldblab
Date:
2020-05-20
Revision:
0:b6b6869bee52

File content as of revision 0:b6b6869bee52:

/*
 * Inventor's kit for micro:bit
 * Experiment 4: Using a transistor to drive a motor.
*/

#include "MicroBit.h"

MicroBit uBit;

int duty;

int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();

    // intialize the duty cycle
    duty = 0;
        
    while(1)
    {
        // increase duty (cylle faster)
        while (duty < 1023)
        {
            uBit.io.P0.setAnalogValue(duty);
            duty++;
            uBit.sleep(10); //sleep for 20ms 
        }
        
        // decrease duty (cycle slower)
        while (duty > 0)
        {
            uBit.io.P0.setAnalogValue(duty);
            duty--;
            uBit.sleep(10); //sleep for 20ms 
        }
    }
}