Advertisement
bobbinz

Micro:bit "Lighting Desk"

Oct 26th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Micro:bit "Lighting Desk"
  2. //Adam Robbins 2017
  3.  
  4. //RGB LED Connected to pins 1, 2 and 8
  5. //Variable resistor to pin 0
  6.  
  7. let blue = 0
  8. let green = 0
  9. let value = 0
  10. let red = 0
  11. let select = 0
  12.  
  13. basic.forever(() => {
  14.     update()
  15.     value = pins.analogReadPin(AnalogPin.P0)
  16.     switch (select) {
  17.         case 0:
  18.             red = value
  19.             basic.showString("R")
  20.             break;
  21.         case 1:
  22.             green = value
  23.             basic.showString("G")
  24.             break;
  25.         case 2:
  26.             blue = value
  27.             basic.showString("B")
  28.             break;
  29.         case 99:
  30.             basic.showString(" ")
  31.             break;
  32.         default:
  33.             basic.showString("ERROR 1") //all errors are undocumented
  34.             break;
  35.     }
  36. })
  37. input.onButtonPressed(Button.A, () => {
  38.     select += 1
  39.     if (select >= 3) { //could be == if it was not for the special 99 case
  40.         select = 0
  41.     }
  42. })
  43. input.onButtonPressed(Button.B, () => {
  44.     red = Math.random(1023)
  45.     green = Math.random(1023)
  46.     blue = Math.random(1023)
  47.     select = 99
  48. })
  49. function update() {
  50.     pins.analogWritePin(AnalogPin.P1, red)
  51.     pins.analogWritePin(AnalogPin.P8, green)
  52.     pins.analogWritePin(AnalogPin.P2, blue)
  53. }
  54. input.onButtonPressed(Button.AB, () => {
  55.     basic.showString(".R")
  56.     basic.showNumber(red)
  57.     basic.showString(".G")
  58.     basic.showNumber(green)
  59.     basic.showString(".B")
  60.     basic.showNumber(blue)
  61.     basic.clearScreen()
  62. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement