Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int onOffB = 2;
- const int rotateB = 3;
- const int potIn = A0;
- const int speedOutPin = 6;
- const int directionPinOut = 4;
- const int onOffOut = 5;
- bool onOff = false;
- bool CW_CCW = true;
- bool debugOff = false;
- void setup() {
- pinMode(onOffB, INPUT);
- pinMode(rotateB, INPUT);
- pinMode(potIn, INPUT);
- pinMode(speedOutPin, OUTPUT);
- pinMode(directionPinOut, OUTPUT);
- pinMode(onOffOut, OUTPUT);
- Serial.begin(9600);
- do
- {
- Serial.println("Direction true is CW");
- }
- while(!Serial);
- }
- void loop() {
- onOff = debounce(onOff, onOffB);
- CW_CCW = debounce(CW_CCW, rotateB);
- int speedVal = analogRead(potIn);
- if(onOff)
- {
- debugOff = false;
- digitalWrite(onOffOut, HIGH);
- motorRun(CW_CCW, speedVal);
- Serial.println("Entered IF");
- }
- else
- {
- digitalWrite(onOffOut, LOW);
- debugOff = true;
- motorRun(CW_CCW, 0);
- }
- }
- void motorRun(bool dir, int speedVal)
- {
- speedVal = map(speedVal, 0, 1023, 0 , 255);
- digitalWrite(directionPinOut, dir);
- analogWrite(speedOutPin, speedVal);
- if(!debugOff)
- {
- Serial.println(dir);
- Serial.println(speedVal);
- }
- }
- bool debounce(bool prev, int pin)
- {
- bool currB = digitalRead(pin);
- delay(30);
- bool current = digitalRead(pin);
- if(currB != current)
- {
- prev = !prev;
- }
- return prev;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement