Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int pinDir1 = 9;
- const int pinDir2= 3;
- const int pinmotor = 2; //on and off
- const int onoffbutton = 12; //this button is used to turn the motor on and off
- const int changedirectionbutton = 11; //this button is used to change the direction of the motor; so in turn changing direction fan is blowing.
- const int potpin = A0;
- boolean currentonoffval = false;
- boolean lastonoffval = false;
- boolean currentchangeval = false;
- boolean lastchangeval = false;//change direction
- boolean motoron = false;
- boolean Direction;
- int speeding = false;
- void setup()
- {
- pinMode (onoffbutton, INPUT);
- pinMode (changedirectionbutton, INPUT);
- pinMode (potpin, INPUT);
- pinMode (pinmotor, OUTPUT);
- Serial.begin (9600);
- digitalWrite (pinmotor, LOW);//make sure the motor is off in the beginning
- }
- boolean debounce (boolean last, int button)
- {
- boolean current = digitalRead (button);
- if (last != current)//if lastbutton is different than currentbutton then delay for 5 milliseconds and read the currentbutton
- {
- delay (5);
- current = digitalRead (button);
- }
- return current;
- }
- void loop()
- {
- currentonoffval = debounce (lastonoffval, onoffbutton);
- Serial.println (currentonoffval);
- if (lastonoffval == LOW && currentonoffval == HIGH)
- {
- motoron =! motoron;//press button turn motor on
- }
- currentchangeval = debounce (lastchangeval, changedirectionbutton);
- if (lastchangeval == LOW && currentchangeval == HIGH)//pressed button so change direction
- {
- Direction =! Direction;
- }
- if (Direction)//makes it true and false and determines the flow. changes what openings are open
- {
- digitalWrite (pinDir1, HIGH);//true: forward
- digitalWrite (pinDir2, LOW);
- }
- else
- {
- digitalWrite (pinDir1, LOW);//false: backwards
- digitalWrite (pinDir2, HIGH);
- }
- speeding = analogRead (potpin);
- Serial.println (speeding);
- speeding = map (speeding, 0, 1023, 0,255);
- speeding = constrain (speeding, 0, 255);
- if (motoron)
- {
- analogWrite (pinmotor, speeding);//on: make it go at this speed
- }
- else
- {
- analogWrite (pinmotor, 0);//off: then stop(can't use low because the analog would have to be digital)
- }
- lastchangeval = currentchangeval;
- lastonoffval = currentonoffval;
- }
Add Comment
Please, Sign In to add comment