Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /*
  2. Make a DC Motor Move to Sound.
  3. This example code is in the public domain.
  4. Created by Donald Bell, Maker Project Lab (2016).
  5. Based on Sound to Servo by Cenk zdemir (2012)
  6. and DCMotorTest by Adafruit
  7. */
  8. // include the Adafruit motor shield library
  9. #include <Wire.h>
  10. #include <Adafruit_MotorShield.h>
  11. #include "utility/Adafruit_MS_PWMServoDriver.h"
  12.  
  13. // Create the motor shield object with the default I2C address
  14. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  15. // Or, create it with a different I2C address (say for stacking)
  16. // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
  17.  
  18. // Select which 'port' M1, M2, M3 or M4. In this case, M1 for mouth and M2 for tail
  19. Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
  20. Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
  21.  
  22. // Some other Variables we need
  23. int SoundInPin = A0;
  24. int LedPin = 12; //in case you want an LED to activate while mouth moves
  25.  
  26. // the setup routine runs once when you press reset:
  27. void setup() {
  28. Serial.begin(9600); // set up Serial library at 9600 bps
  29.  
  30.  
  31. AFMS.begin(); // create with the default frequency 1.6KHz
  32. //AFMS.begin(1000); // OR with a different frequency, say 1KHz
  33.  
  34. // Set the speed to start, from 0 (off) to 255 (max speed)
  35. myMotor->setSpeed(0); //mouth motor
  36. myMotor->run(FORWARD);
  37. // turn on motor
  38. myMotor->run(RELEASE);
  39. pinMode(SoundInPin, INPUT);
  40. pinMode(LedPin, OUTPUT);
  41. myOtherMotor->setSpeed(0); //tail motor
  42. myOtherMotor->run(FORWARD);
  43. // turn on motor
  44. myOtherMotor->run(RELEASE);
  45. pinMode(SoundInPin, INPUT);
  46. }
  47.  
  48. // the loop routine runs over and over again forever:
  49. void loop() {
  50. uint8_t i;
  51.  
  52. // read the input on analog pin 0:
  53. int sensorValue = analogRead(SoundInPin);
  54. // we Map another value of this for LED that can be a integer betwen 0..255
  55. int LEDValue = map(sensorValue,0,512,0,255);
  56. // We Map it here down to the possible range of movement.
  57. sensorValue = map(sensorValue,0,512,0,180);
  58. // note normally the 512 is 1023 because of analog reading should go so far, but I changed that to get better readings.
  59. int MoveDelayValue = map(sensorValue,0,255,0,sensorValue);
  60.  
  61. // maping the same reading a little bit more down to calculate the time your motor gets
  62. if (sensorValue > 10) { // to cut off some static readings
  63. delay(1); // a static delay to smooth things out...
  64. // now move the motor
  65. myMotor->run(FORWARD);
  66. for (i=140; i<255; i++) {
  67. myMotor->setSpeed(i);
  68.  
  69. }
  70.  
  71. //for (i=200; i!=0; i--) {
  72. // myMotor->setSpeed(i);
  73. // delay(10);
  74. // }
  75.  
  76. analogWrite(LedPin, sensorValue);
  77. // and do that move in this delay time
  78.  
  79. myMotor->run(RELEASE);
  80. myOtherMotor->run(RELEASE);
  81. delay(1);
  82. } // Done.
  83. // turn off the led again.
  84. analogWrite(LedPin, 0);
  85. // and this repeats all the time.
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement