#define PWM 3
#define DIRECTION 2
#define RIGHT 4
#define LEFT 5
#define RIGHT_DIRECTION 1
#define LEFT_DIRECTION 0
#define MAX_SPEED 255
#define SWITCH_DETECCT 0
short int pwmValue = 0;
void setup() {
// put your setup code here, to run once:
pinMode(DIRECTION, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(RIGHT, INPUT_PULLUP);
pinMode(LEFT, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
byte rightDirection = 0;
byte leftDirection = 0;
byte directionOfTheMotor = 0;
rightDirection = digitalRead(RIGHT);
leftDirection = digitalRead(LEFT);
if (rightDirection == SWITCH_DETECCT)
{
directionOfTheMotor = RIGHT_DIRECTION;
++pwmValue;
pwmValue = pwmValue < MAX_SPEED? pwmValue: MAX_SPEED;
}
else if (leftDirection == SWITCH_DETECCT)
{
directionOfTheMotor = LEFT_DIRECTION;
++pwmValue;
pwmValue = pwmValue < MAX_SPEED? pwmValue: MAX_SPEED;
}
else
{
pwmValue = pwmValue > 1? pwmValue: 1;
--pwmValue;
}
analogWrite(PWM, pwmValue);
digitalWrite(DIRECTION, directionOfTheMotor);
delay(100);
}