KRITSADA

clone microbit math sample

Sep 16th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let max = 0
  2. let base2exp = 0
  3. let n = 0
  4. let tick = 100 // refresh rate in milliseconds
  5. input.onButtonPressed(Button.A, () => {
  6.     decrement()
  7. })
  8. input.onButtonPressed(Button.B, () => {
  9.     increment()
  10. })
  11. function decrement() {
  12.     if (n < 0) {
  13.         n = max
  14.     } else {
  15.         n = n - 1
  16.     }
  17. }
  18. function increment() {
  19.     if (n > max) {
  20.         n = 0
  21.     } else {
  22.         n += 1
  23.     }
  24. }
  25.  
  26. n = 0 // counter starts at zero
  27. base2exp = 30 //number of binary 'switches'
  28. max = 2 ** base2exp // 2^base2exp
  29.  
  30. function dec2bin(dec: number) {
  31.     let binN = (dec >>> 0).toString(/*should be able to pass in radix*/)
  32.     basic.showString(binN)
  33.     return binN
  34. }
  35.  
  36. basic.forever(() => {
  37.     basic.pause(tick)
  38.     basic.showString(dec2bin(n))
  39. })
Advertisement
Add Comment
Please, Sign In to add comment