Advertisement
bobbinz

Micro:bit Capacitor Charge

Oct 30th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Micro:bit Capacitor Charge
  2. //Adam Robbins 2017
  3.  
  4. //Capacitor via Variable resistor and switch to Pin 0. Discharge via another switch
  5. //LEDs on Pins 1, 2, 8 and 12
  6.  
  7. basic.forever(() => {
  8.     let charge: number
  9.     let split: number[]
  10.  
  11.     charge = pins.analogReadPin(AnalogPin.P0)
  12.     split = divide(charge)
  13.     led.plotBarGraph(charge, 1023)
  14.     pins.analogWritePin(AnalogPin.P1, split[0])
  15.     pins.analogWritePin(AnalogPin.P2, split[1])
  16.     pins.analogWritePin(AnalogPin.P8, split[2])
  17.     pins.analogWritePin(AnalogPin.P12, split[3])
  18. })
  19.  
  20. function divide(x: number) {
  21.     let a: number
  22.     let b: number
  23.     let c: number
  24.     let d: number
  25.     if (x <= 254) {
  26.         a = x * 4
  27.     } else if (x > 254 && x <= 512) {
  28.         a = 1023
  29.         b = ((x - 254) * 4)
  30.     } else if (x > 512 && x <= 762) {
  31.         a = 1023
  32.         b = 1023
  33.         c = ((x - 512) * 4)
  34.     } else if (x > 762) {
  35.         a = 1023
  36.         b = 1023
  37.         c = 1023
  38.         d = ((x - 762) * 4)
  39.     } else { //error indication
  40.         a = 0
  41.         b = 0
  42.         c = 0
  43.         d = 1023
  44.     }
  45.     return [a, b, c, d]
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement