Advertisement
manvi_m

DC motor fan code

Jun 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. /*
  2. * Manvi Mittal
  3. * DC motor
  4. */
  5.  
  6. const int controlPin1 = 2;
  7. const int controlPin2 = 3;
  8. const int enablePin = 9;
  9. const int directionSwitchPin = 4;
  10. const int onOffSwitchStateSwitchPin = 5;
  11. const int potPin = A0;
  12.  
  13. int onOffSwitchState = 0;
  14. int previousOnOffSwitchState = 0;
  15. int directionSwitchState = 0;
  16. int previousDirectionSwitchState = 0;
  17. int motorEnabled = 0; //Turns the motor on/off
  18. int motorSpeed = 0;
  19. int motorDirection = 1;
  20.  
  21. void setup()
  22. {
  23. // put your setup code here, to run once:
  24. pinMode (directionSwitchPin, INPUT);
  25. pinMode (onOffSwitchStateSwitchPin, INPUT);
  26. pinMode (potPin, INPUT);
  27. pinMode (controlPin1, OUTPUT);
  28. pinMode (controlPin2, OUTPUT);
  29. pinMode (enablePin, OUTPUT);
  30.  
  31. Serial.begin (9600);
  32.  
  33. digitalWrite (enablePin, LOW);
  34. }
  35.  
  36. void loop()
  37. {
  38. // put your main code here, to run repeatedly:
  39. onOffSwitchState = digitalRead (onOffSwitchStateSwitchPin); // read the value of the on/off switch
  40. delay (1);
  41. Serial.print(onOffSwitchState);
  42. directionSwitchState = digitalRead (directionSwitchPin); // read the value of the direction switch
  43. Serial.println (directionSwitchState);
  44. motorSpeed = analogRead (potPin) / 4; // read the value of the pot and divide by 4 to get a value
  45.  
  46. if (onOffSwitchState != previousOnOffSwitchState)
  47. {
  48. if (onOffSwitchState == HIGH)
  49. {
  50. motorEnabled = !motorEnabled;
  51. }
  52. }
  53. if (directionSwitchState != previousDirectionSwitchState)
  54. {
  55. if (directionSwitchState == HIGH)
  56. {
  57. motorDirection = !motorDirection;
  58. }
  59. }
  60. if (motorDirection == 1)
  61. {
  62. digitalWrite (controlPin1, HIGH);
  63. digitalWrite (controlPin2, LOW);
  64. }
  65. else
  66. {
  67. digitalWrite (controlPin1, LOW);
  68. digitalWrite (controlPin2, HIGH);
  69. }
  70. if (motorEnabled == 1)
  71. {
  72. analogWrite (enablePin, motorSpeed);
  73. }
  74. else
  75. {
  76. analogWrite (enablePin, 0);
  77. }
  78. previousDirectionSwitchState = directionSwitchState;
  79. previousOnOffSwitchState = onOffSwitchState;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement