sources and files for the bombatuino project
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.

75 lines
1.7 KiB

12 years ago
12 years ago
  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. //should be called in loop(), read values and call callback function on change
  31. void INPUT_74HC4051::loop() {
  32. uint8_t pin,r0,r1,r2;
  33. int value;
  34. for (pin=0; pin<8; pin++) {
  35. r0 = bitRead(pin,0);
  36. r1 = bitRead(pin,1);
  37. r2 = bitRead(pin,2);
  38. digitalWrite(_s0, r0);
  39. digitalWrite(_s1, r1);
  40. digitalWrite(_s2, r2);
  41. //divide by 8 to get value beetween 0-127 for MIDI
  42. value = analogRead(_analog) / 8;
  43. if (_value[pin] < value || _value[pin] > value) {
  44. _value[pin] = value;
  45. (*_callbackFunction)(_analog,pin,value);
  46. }
  47. }
  48. }
  49. //maybe useful later
  50. int INPUT_74HC4051::getSpecificValue(uint8_t pin) {
  51. if (pin >= 8)
  52. return -1;
  53. uint8_t r0,r1,r2;
  54. r0 = bitRead(pin,0);
  55. r1 = bitRead(pin,1);
  56. r2 = bitRead(pin,2);
  57. digitalWrite(_s0, r0);
  58. digitalWrite(_s1, r1);
  59. digitalWrite(_s2, r2);
  60. //delayMicroseconds(10);
  61. int value = analogRead(_analog) / 8;
  62. _value[pin] = value;
  63. return value;
  64. }