Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Divya Inaganti
- * June 19, 2017
- * Fan DC Motor and Integrated Circuit Activity
- */
- const int controlPin1 = 2;
- const int controlPin2 = 3;
- const int enablePin = 9;
- const int directionSwitchPin = 4;
- const int onOffSwitchStatePin = 6;
- const int potPin = A0;
- int onOffSwitchState= 0;
- int onOffSwitchStateSwitchPin = 0;
- int previousOnOffSwitchState = 0;
- int directionSwitchState = 0;
- int previousDirectionSwitchState = 0;
- int motorEnabled = 0;
- int motorSpeed = 0;
- int motorDirection = 1;
- void setup()
- {
- pinMode(potPin, INPUT);
- pinMode(directionSwitchPin, INPUT);
- pinMode(onOffSwitchStatePin, INPUT);
- pinMode(onOffSwitchStateSwitchPin, INPUT);
- pinMode(controlPin1, OUTPUT);
- pinMode(controlPin2, OUTPUT);
- pinMode(enablePin, OUTPUT);
- digitalWrite(enablePin, LOW);
- Serial.begin(9600);
- }
- void loop()
- {
- onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
- delay (1);
- directionSwitchState = digitalRead(directionSwitchPin);
- motorSpeed= analogRead(potPin);
- motorSpeed = map(motorSpeed, 0, 1023, 0, 255);
- motorSpeed = constrain(motorSpeed, 0, 255);
- if (onOffSwitchStatePin != previousOnOffSwitchState)
- {
- if(onOffSwitchStatePin == HIGH)
- {
- motorEnabled = !motorEnabled;
- }
- }
- if(directionSwitchState != previousDirectionSwitchState)
- {
- if(directionSwitchState == HIGH)
- {
- motorDirection = !motorDirection;
- }
- }
- if(motorDirection == 1)
- {
- digitalWrite(controlPin1, HIGH);
- digitalWrite(controlPin2, LOW);
- }
- else
- {
- digitalWrite(controlPin1, LOW);
- digitalWrite(controlPin2, HIGH);
- }
- if(motorEnabled == 1)
- {
- analogWrite(enablePin, motorSpeed);
- }
- else
- {
- analogWrite(enablePin, 0);
- }
- {
- previousDirectionSwitchState = directionSwitchState;
- previousOnOffSwitchState = onOffSwitchStatePin;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement