Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int controlPin1 =2;
- const int controlPin2 =3;
- const int enablePin =9;
- const int directionPin =4;
- const int switchOnOff =5;
- const int potPin =A0;
- int readVal;
- bool oldPressON =false;
- bool newPressON = false;
- // used to distinguish btw button presses - if the old and the new values are different, it's a new press, if it is the same value, then it is not a new press
- //booleans are either true or false, 0 or 1 -- on or off
- //need to define two new boolean vars for the direction button too
- bool oldDir = false;
- bool newDir = false;
- bool enable = false;
- bool dir =false;
- int motorSpeed;
- // to make motor on or off
- void setup() {
- // put your setup code here, to run once:
- pinMode (enablePin, OUTPUT);
- pinMode (controlPin1, OUTPUT);
- pinMode (controlPin2, OUTPUT);
- pinMode (directionPin, INPUT);
- pinMode (switchOnOff, INPUT);
- pinMode (potPin, INPUT);
- Serial.begin (9600);
- }
- void loop() {
- newPressON=digitalRead (switchOnOff);
- delay (5);
- //tested button that controls on or off
- if (newPressON ==1 && newPressON != oldPressON)
- {
- enable = !enable;
- //! means opposite (true to false, and false to true)
- //booleans are digital inputs
- }
- newDir = digitalRead (directionPin);
- delay (5);
- if(newDir == 1 && newDir != oldDir)
- {
- dir = !dir;
- }
- if (enable)
- {
- motorSpeed = analogRead(potPin);
- motorSpeed =map(motorSpeed, 0, 1023, 0, 255);
- if (dir)
- {
- digitalWrite(controlPin1, HIGH);
- digitalWrite(controlPin2, LOW);
- analogWrite(enablePin, motorSpeed);
- }
- else
- {
- digitalWrite(controlPin1, LOW);
- digitalWrite(controlPin2, HIGH);
- analogWrite(enablePin, motorSpeed);
- }
- }
- else
- {
- analogWrite(enablePin,0);
- }
- oldPressON =newPressON;
- oldDir =newDir;
- //tested button that controls direction
- if (newDir ==1)
- {
- enable = !enable;
- }
- readVal = analogRead(potPin);
- readVal = map(readVal, 0, 1023, 0, 255);
- }
- //tested potentiometer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement