coltonspastebin

Changing direction of fan with Edge breach

Jan 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1.  
  2. const int pinDir1 = 9;
  3. const int pinDir2= 3;
  4. const int pinmotor = 2; //on and off
  5. const int onoffbutton = 12; //this button is used to turn the motor on and off
  6. const int changedirectionbutton = 11; //this button is used to change the direction of the motor; so in turn changing direction fan is blowing.
  7. const int potpin = A0;
  8. boolean currentonoffval = false;
  9. boolean lastonoffval = false;
  10. boolean currentchangeval = false;
  11. boolean lastchangeval = false;//change direction
  12. boolean motoron = false;
  13. boolean Direction;
  14. int speeding = false;
  15.  
  16.  
  17. void setup()
  18. {
  19. pinMode (onoffbutton, INPUT);
  20. pinMode (changedirectionbutton, INPUT);
  21. pinMode (potpin, INPUT);
  22. pinMode (pinmotor, OUTPUT);
  23. Serial.begin (9600);
  24. digitalWrite (pinmotor, LOW);//make sure the motor is off in the beginning
  25. }
  26. boolean debounce (boolean last, int button)
  27. {
  28. boolean current = digitalRead (button);
  29. if (last != current)//if lastbutton is different than currentbutton then delay for 5 milliseconds and read the currentbutton
  30. {
  31. delay (5);
  32. current = digitalRead (button);
  33. }
  34. return current;
  35. }
  36.  
  37. void loop()
  38. {
  39. currentonoffval = debounce (lastonoffval, onoffbutton);
  40. Serial.println (currentonoffval);
  41. if (lastonoffval == LOW && currentonoffval == HIGH)
  42. {
  43. motoron =! motoron;//press button turn motor on
  44. }
  45. currentchangeval = debounce (lastchangeval, changedirectionbutton);
  46. if (lastchangeval == LOW && currentchangeval == HIGH)//pressed button so change direction
  47. {
  48. Direction =! Direction;
  49. }
  50. if (Direction)//makes it true and false and determines the flow. changes what openings are open
  51. {
  52. digitalWrite (pinDir1, HIGH);//true: forward
  53. digitalWrite (pinDir2, LOW);
  54. }
  55. else
  56. {
  57. digitalWrite (pinDir1, LOW);//false: backwards
  58. digitalWrite (pinDir2, HIGH);
  59. }
  60. speeding = analogRead (potpin);
  61. Serial.println (speeding);
  62. speeding = map (speeding, 0, 1023, 0,255);
  63. speeding = constrain (speeding, 0, 255);
  64. if (motoron)
  65. {
  66. analogWrite (pinmotor, speeding);//on: make it go at this speed
  67. }
  68. else
  69. {
  70. analogWrite (pinmotor, 0);//off: then stop(can't use low because the analog would have to be digital)
  71. }
  72. lastchangeval = currentchangeval;
  73. lastonoffval = currentonoffval;
  74. }
Add Comment
Please, Sign In to add comment