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 motorSpeed;
- 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 onoff = false;
- // to make motor on or off
- bool dir = false;
- 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);
- Serial.println (newPressON);
- delay (5);
- //tested button that controls on or off
- if (newPressON ==1 && newPressON=!oldPressON)
- //&& means and -- both conditions have to be true
- {
- onoff = !onoff;
- //! means opposite (true to false, and false to true)
- //booleans are digital inputs
- }
- newDir = digitalRead (directionPin);
- Serial.println (newDir);
- delay (5);
- //tested button that controls direction
- motorSpeed = analogRead(potPin);
- motorSpeed = map(motorSpeed, 0, 1023, 0, 255);
- Serial.println (motorSpeed);
- //tested potentiometer
- if (newDir ==1 && newDir = !oldDir)
- {
- dir = !dir;
- }
- if (onoff)
- //if enable is true -- then turn motor on, map potentiometer, and go at speed of motorSpeed
- {
- motorSpeed = analogRead(potPin);
- motorSpeed = map(motorSpeed, 0, 1023, 0, 255);
- if (dir)
- //if dir is true, then we move in this direction
- {
- digitalWrite (controlPin1, HIGH);
- digitalWrite (controlPin2, LOW);
- //either high, low or low, high -- switches positives and negatives
- analogWrite (enablePin, motorSpeed);
- }
- else {
- digitalWrite (controlPin1, LOW);
- digitalWrite (controlPin2, HIGH);
- //either high, low or low, high -- switches positives and negatives
- analogWrite (enablePin, motorSpeed);
- }
- }
- else {
- analogWrite (enablePin,0) ;
- }
- newPressON = oldPressON;
- newDir = oldDir;
- }
Advertisement
Add Comment
Please, Sign In to add comment