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;
- bool oldDir=false;
- bool newDir=false;
- bool enable=false;
- bool dir=false;
- int motorSpeed;
- void setup() {
- 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);
- if(newPressON==1 && newPressON !=oldPressON)
- {
- enable=!enable;
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement