Guest User

Untitled

a guest
Jan 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <DRV8833.h>
  2. #include <Wire.h>
  3.  
  4. DRV8833 driver = DRV8833();
  5. // Create an instance of the DRV8833:
  6. //const int inputA1 = 3, inputA2 = 6;
  7. const int motorSpeed = 128;
  8. // spectrum shield band arrays
  9.  
  10. const int LEDPIN=13; // light up when audio detected
  11. const int PIN_STROBE=4; // spectrum shield
  12. const int PIN_RESET=5; // spectrum shield
  13. const int PIN_MOTOR_L=3; //PWM to motor open mouth
  14. const int PIN_MOTOR_R=6; //PWM to motor close mouth
  15. const int PIN_MOTOR_SLEEP=7; //sleep fxn on motor board
  16. const int PIN_MOTOR_STALL=8; //stall warning on motor board
  17.  
  18. //analog pins
  19. const int PIN_LEFT=0; // L analog from spectrum shield
  20. const int PIN_RIGHT=1; // R analog from spectrum shield
  21. const int PIN_MOTOR_POT=3; //analog potentiometer on motor
  22.  
  23.  
  24. int left[7];
  25. int right[7];
  26. int minRotation = 400; //approximate reading when mouth closed
  27. int maxRotation = 600; //approximate reading when mouth open
  28. int targetL = 721;//measure the position
  29. int targetH = 166;
  30. int motor1Pin_O;
  31. int motor1Pin_C;
  32. int currentSpeed;
  33. int y=0;
  34.  
  35. void setup() {
  36. Wire.begin();
  37. //Wire.onReceive(receiveEvent);
  38. // driver.attachMotorA(inputA1, inputA2);
  39.  
  40. pinMode(LEDPIN, OUTPUT); // LED
  41. //initialize spectrum board related pins
  42. pinMode(PIN_RESET, OUTPUT); // reset
  43. pinMode(PIN_STROBE, OUTPUT); // strobe
  44. digitalWrite(PIN_RESET,LOW); // reset low
  45. digitalWrite(PIN_STROBE,HIGH); //pin 5 is RESET on the shield
  46.  
  47. Serial.begin(9600);
  48. Serial.println("Ready!");
  49.  
  50. }
  51.  
  52.  
  53. void loop() {
  54.  
  55. if(Wire.available()){
  56. Wire.beginTransmission(3);
  57. Wire.write(readMSGEQ7());
  58. Wire.endTransmission(3);
  59. }
  60.  
  61. }
  62.  
  63. int readMSGEQ7(){
  64. //reset the data
  65. digitalWrite(PIN_RESET, HIGH);
  66. digitalWrite(PIN_RESET, LOW);
  67.  
  68. //loop thru all 7 bands
  69. int sumRight = 0;
  70. int sumLeft = 0;
  71.  
  72. for(int band=0; band < 7; band++) {
  73. digitalWrite(PIN_STROBE,LOW); // go to the next band
  74. delayMicroseconds(20); //gather some data
  75. left[band] = analogRead(PIN_LEFT); // store left band reading
  76.  
  77. //right[band] = left[band]; //use this only for MONO!
  78. right[band] = analogRead(PIN_RIGHT); // store right band reading
  79.  
  80. digitalWrite(PIN_STROBE,HIGH); // reset the strobe pin
  81. sumRight = sumRight+right[band]; // get the sum from all bands
  82. sumLeft = sumLeft+left[band]; // get the sum from all bands
  83.  
  84. if(left[band] > 100 || right[band] > 100) {
  85. digitalWrite(LEDPIN,HIGH); // sound detected
  86. }
  87. else {
  88. digitalWrite(LEDPIN,LOW); // reset low
  89. }
  90. }
  91.  
  92. //Serial.println(sumLeft);
  93.  
  94. if(sumLeft > 2000) {sumLeft = 2000;}
  95. x = map(sumLeft, 0, 2000, 0, 300);
  96. //Serial.println(x);
  97. //targetH = target_mouthPosition + posError;
  98. //targetL = target_mouthPosition - posError;
  99.  
  100. return x;
  101. }
Add Comment
Please, Sign In to add comment