Advertisement
iyera20

DC Motor and Fan

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