Update of the bombatuino with a pro micro board with native usb midi support.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8 KiB

  1. #include "Arduino.h"
  2. #include "bombatuino_INPUT_74HC4051.h"
  3. //should be called in setup()
  4. void INPUT_74HC4051::begin(uint8_t analog, uint8_t s0, uint8_t s1, uint8_t s2,CallbackFunction cbF) {
  5. //analog pin which output Z is connected to
  6. _analog = analog;
  7. //3 digital pins where S0,S1,S2 are connected to
  8. _s0 = s0;
  9. _s1 = s1;
  10. _s2 = s2;
  11. pinMode(_analog,INPUT);
  12. pinMode(_s0,OUTPUT);
  13. pinMode(_s1,OUTPUT);
  14. pinMode(_s2,OUTPUT);
  15. _callbackFunction = cbF;
  16. //initalize start values
  17. uint8_t pin,r0,r1,r2;
  18. //run through the 8 I/O pins
  19. for (pin=0; pin<8; pin++) {
  20. r0 = bitRead(pin,0);
  21. r1 = bitRead(pin,1);
  22. r2 = bitRead(pin,2);
  23. digitalWrite(_s0, r0);
  24. digitalWrite(_s1, r1);
  25. digitalWrite(_s2, r2);
  26. //delayMicroseconds(10);
  27. _value[pin] = analogRead(_analog);
  28. }
  29. }
  30. void INPUT_74HC4051::setPins(uint8_t pins) {
  31. _pins = pins;
  32. }
  33. //should be called in loop(), read values and call callback function on change
  34. void INPUT_74HC4051::loop() {
  35. uint8_t pin,r0,r1,r2;
  36. int value;
  37. for (pin=0; pin<8; pin++) {
  38. if (_pins & (1<<pin)) {
  39. r0 = bitRead(pin,0);
  40. r1 = bitRead(pin,1);
  41. r2 = bitRead(pin,2);
  42. digitalWrite(_s0, r0);
  43. digitalWrite(_s1, r1);
  44. digitalWrite(_s2, r2);
  45. //divide by 8 to get value beetween 0-127 for MIDI
  46. value = analogRead(_analog) / 8;
  47. if (_value[pin] < value || _value[pin] > value) {
  48. _value[pin] = value;
  49. (*_callbackFunction)(_analog,pin,value);
  50. }
  51. }
  52. }
  53. }
  54. //maybe useful later
  55. int INPUT_74HC4051::getSpecificValue(uint8_t pin) {
  56. if (pin >= 8 || !(_pins & (1<<pin)))
  57. return -1;
  58. uint8_t r0,r1,r2;
  59. r0 = bitRead(pin,0);
  60. r1 = bitRead(pin,1);
  61. r2 = bitRead(pin,2);
  62. digitalWrite(_s0, r0);
  63. digitalWrite(_s1, r1);
  64. digitalWrite(_s2, r2);
  65. //delayMicroseconds(10);
  66. int value = analogRead(_analog) / 8;
  67. _value[pin] = value;
  68. return value;
  69. }