Advertisement
bobbinz

Micro:bit Motor Experiments

Oct 27th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Micro:bit Motor Experiments
  2. //Adam Robbins 2017
  3.  
  4. //Motor via transistor on pin0
  5. //Motor in reverse on pin 1 (via potential divider to halve reading)
  6.  
  7. let duty: number
  8. let mode: boolean
  9.  
  10. basic.forever(() => {
  11.     if (mode) {
  12.         pins.analogWritePin(AnalogPin.P0, duty)
  13.         if (input.buttonIsPressed(Button.A)) {
  14.             duty += -10
  15.             basic.showString("-")
  16.             if (duty < 1) {
  17.                 duty = 1
  18.                 basic.showString(" ")
  19.             }
  20.         } else if (input.buttonIsPressed(Button.B)) {
  21.             duty += 10
  22.             basic.showString("+")
  23.             if (duty > 1023) {
  24.                 duty = 1023
  25.                 basic.showString(" ")
  26.             }
  27.         } else {
  28.             led.plotBarGraph(duty, 1023)
  29.         }
  30.     } else {
  31.         duty = pins.analogReadPin(AnalogPin.P1)
  32.         led.plotBarGraph(duty, 1023)
  33.     }
  34. })
  35.  
  36. //Change Mode (Starts on input mode)
  37. input.onButtonPressed(Button.AB, () => {
  38.     if (mode) {
  39.         mode = false
  40.     } else {
  41.         mode = true
  42.     }
  43. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement