Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Micro:bit Capacitor Charge
- //Adam Robbins 2017
- //Capacitor via Variable resistor and switch to Pin 0. Discharge via another switch
- //LEDs on Pins 1, 2, 8 and 12
- basic.forever(() => {
- let charge: number
- let split: number[]
- charge = pins.analogReadPin(AnalogPin.P0)
- split = divide(charge)
- led.plotBarGraph(charge, 1023)
- pins.analogWritePin(AnalogPin.P1, split[0])
- pins.analogWritePin(AnalogPin.P2, split[1])
- pins.analogWritePin(AnalogPin.P8, split[2])
- pins.analogWritePin(AnalogPin.P12, split[3])
- })
- function divide(x: number) {
- let a: number
- let b: number
- let c: number
- let d: number
- if (x <= 254) {
- a = x * 4
- } else if (x > 254 && x <= 512) {
- a = 1023
- b = ((x - 254) * 4)
- } else if (x > 512 && x <= 762) {
- a = 1023
- b = 1023
- c = ((x - 512) * 4)
- } else if (x > 762) {
- a = 1023
- b = 1023
- c = 1023
- d = ((x - 762) * 4)
- } else { //error indication
- a = 0
- b = 0
- c = 0
- d = 1023
- }
- return [a, b, c, d]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement