Advertisement
inagantid20

Final Project Working Code

Jun 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. /*
  2. * Divya Inaganti
  3. * final project- Divya's Divine Fan Device
  4. */
  5.  
  6. const int controlPin1 = 2; // connected to pin 7 on the H-bridge
  7. const int controlPin2 = 3; // connected to pin 2 on the H-bridge
  8. const int enablePin = 9; // connected to pin 1 on the H-bridge
  9.  
  10.  
  11. // create some variables to hold values from your inputs
  12. int motorEnabled = 0; // current state of the On/Off switch
  13. int motorDirection = 0; // current state of the direction switch
  14. int motorSpeed = 255; // speed of the motor
  15. int timeVal;
  16.  
  17. int soundsensorPin = A0;
  18. int soundsensorValue= 0;
  19.  
  20. const int LEDred= 6;
  21. const int LEDblue= 4;
  22. const int LEDwhite= 5;
  23.  
  24. void setup() {
  25.  
  26. pinMode(LEDred, OUTPUT);
  27. pinMode(LEDblue, OUTPUT);
  28. pinMode(LEDwhite, OUTPUT);
  29. pinMode(controlPin1, OUTPUT);
  30. pinMode(controlPin2, OUTPUT);
  31. pinMode(enablePin, OUTPUT);
  32.  
  33. Serial.begin(9600);
  34.  
  35.  
  36. // pull the enable pin LOW to start
  37. digitalWrite(enablePin, LOW);
  38.  
  39.  
  40.  
  41. }
  42. void loop()
  43. {
  44.  
  45. soundsensorValue = analogRead(soundsensorPin);
  46.  
  47. if(soundsensorValue > 110)
  48. {
  49.  
  50. Serial.println(soundsensorValue, DEC);
  51. delay(250);
  52. digitalWrite(LEDred, HIGH);
  53.  
  54. digitalWrite(LEDblue, HIGH);
  55.  
  56. digitalWrite(LEDwhite, HIGH);
  57. clapcounting();
  58. }
  59.  
  60.  
  61. if (motorDirection == 1) {
  62. digitalWrite(controlPin1, HIGH);
  63. digitalWrite(controlPin2, LOW);
  64. } else {
  65. digitalWrite(controlPin1, LOW);
  66. digitalWrite(controlPin2, HIGH);
  67. }
  68.  
  69. // if the motor is supposed to be on
  70. if (motorEnabled == 1) {
  71. // PWM the enable pin to vary the speed
  72. analogWrite(enablePin, motorSpeed);
  73. } else { // if the motor is not supposed to be on
  74. //turn the motor off
  75. analogWrite(enablePin, 0);
  76. }
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. void clapcounting()
  89. {
  90. int claps = 1;
  91.  
  92.  
  93. timeVal = millis();
  94. while(millis()- timeVal < 3000)
  95. {
  96. soundsensorValue = analogRead(soundsensorPin);
  97.  
  98. if(soundsensorValue > 110)
  99. {
  100. claps++;
  101.  
  102. Serial.println(soundsensorValue, DEC);
  103. delay(250);
  104.  
  105.  
  106. }
  107.  
  108. }
  109. if (claps== 1)
  110. {
  111. motorEnabled= !motorEnabled;
  112. }
  113. else if (claps== 2)
  114. {
  115. if (motorSpeed== 255)
  116. {
  117. motorSpeed= 120;
  118. }
  119. else if (motorSpeed== 120)
  120. {
  121. motorSpeed= 255;
  122. }
  123. else if (claps== 2)
  124. {
  125. motorDirection= ! motorDirection;
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement