Advertisement
inagantid20

Fan- DC motor and Potentiometer Activity

Jun 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. /*
  2. * Divya Inaganti
  3. * June 19, 2017
  4. * Fan DC Motor and Integrated Circuit Activity
  5. */
  6. const int controlPin1 = 2;
  7. const int controlPin2 = 3;
  8. const int enablePin = 9;
  9. const int directionSwitchPin = 4;
  10. const int onOffSwitchStatePin = 6;
  11. const int potPin = A0;
  12. int onOffSwitchState= 0;
  13. int onOffSwitchStateSwitchPin = 0;
  14. int previousOnOffSwitchState = 0;
  15. int directionSwitchState = 0;
  16. int previousDirectionSwitchState = 0;
  17. int motorEnabled = 0;
  18. int motorSpeed = 0;
  19. int motorDirection = 1;
  20.  
  21. void setup()
  22. {
  23. pinMode(potPin, INPUT);
  24. pinMode(directionSwitchPin, INPUT);
  25. pinMode(onOffSwitchStatePin, INPUT);
  26. pinMode(onOffSwitchStateSwitchPin, INPUT);
  27. pinMode(controlPin1, OUTPUT);
  28. pinMode(controlPin2, OUTPUT);
  29. pinMode(enablePin, OUTPUT);
  30. digitalWrite(enablePin, LOW);
  31. Serial.begin(9600);
  32. }
  33. void loop()
  34. {
  35. onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  36. delay (1);
  37. directionSwitchState = digitalRead(directionSwitchPin);
  38. motorSpeed= analogRead(potPin);
  39. motorSpeed = map(motorSpeed, 0, 1023, 0, 255);
  40. motorSpeed = constrain(motorSpeed, 0, 255);
  41.  
  42. if (onOffSwitchStatePin != previousOnOffSwitchState)
  43. {
  44. if(onOffSwitchStatePin == HIGH)
  45. {
  46. motorEnabled = !motorEnabled;
  47. }
  48. }
  49.  
  50. if(directionSwitchState != previousDirectionSwitchState)
  51. {
  52. if(directionSwitchState == HIGH)
  53. {
  54. motorDirection = !motorDirection;
  55. }
  56. }
  57.  
  58. if(motorDirection == 1)
  59. {
  60. digitalWrite(controlPin1, HIGH);
  61. digitalWrite(controlPin2, LOW);
  62. }
  63.  
  64. else
  65. {
  66. digitalWrite(controlPin1, LOW);
  67. digitalWrite(controlPin2, HIGH);
  68. }
  69.  
  70. if(motorEnabled == 1)
  71. {
  72. analogWrite(enablePin, motorSpeed);
  73. }
  74. else
  75. {
  76. analogWrite(enablePin, 0);
  77. }
  78. {
  79. previousDirectionSwitchState = directionSwitchState;
  80. previousOnOffSwitchState = onOffSwitchStatePin;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement