const int FWD = 0;
const int REV = 1;
const int H1 = 3;
const int H2 = 5;
const int L1 = 6;
const int L2 = 9;
const int DWELLTIME = 2000;
const int PWMSTPTIME = 10;
const int PWMMAXVAL = 255;
int direction = FWD;
void motion(int dir, int pwm)
{
if (dir == FWD)
{
analogWrite(H1, pwm);
analogWrite(H2, 0);
analogWrite(L1, 0);
analogWrite(L2, pwm);
}
else if (dir == REV)
{
analogWrite(H1, 0);
analogWrite(H2, pwm);
analogWrite(L1, pwm);
analogWrite(L2, 0);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(H1, OUTPUT);
pinMode(H2, OUTPUT);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for (int pwmvalue=0; pwmvalue <= PWMMAXVAL; pwmvalue++)
{
motion(direction, pwmvalue);
delay(PWMSTPTIME);
}
delay(DWELLTIME);
for (int pwmvalue=PWMMAXVAL; pwmvalue >= 0; pwmvalue--)
{
motion(direction, pwmvalue);
delay(PWMSTPTIME);
}
direction ^= 1;
}
Comments