What is PWM? What is the benefit of using PWM pins? Which pins can be used for PWM output?
Look at this video below around 12:48 minutes into it and follow what he does.
Now try again the square pattern with more accurate performance. Copy and paste your program here.
// Square Pattern with "PWM" is pulse width modulation.
// Using the PWM pins allows the programmer to control the pulses sent to the motors.
// Pins 5 and 6 are used for PWM output.
// Darcy Chang
// 6/6/19
// Arduino 1.8.5
// Set up pins
const int OnBoardLED = 13;
int Dlay = 500;
void setup()
{
pinMode(9,OUTPUT); // set the pin modes to output
pinMode(10, OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(OnBoardLED,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
}
void loop()
{
blinky(); // blink the light
forward(1000,120,120); // move forward
stopBot(1000); // briefly pause
turnRight(1,80); // turn at a right angle
}
void blinky()
{
digitalWrite(OnBoardLED, LOW); // flash on board LED
delay(Dlay);
digitalWrite(OnBoardLED, HIGH);
delay(Dlay);
}
void forward(int amt, int lWheel, int rWheel)
{
analogWrite(6,lWheel);
analogWrite(5,rWheel);
digitalWrite(9,HIGH); // make left motor move forward
digitalWrite(10,LOW);
digitalWrite(11,HIGH); // make right motor move forward
digitalWrite(12,LOW);
delay(amt);
stopBot(500);
}
void stopBot(int pause)
{
digitalWrite(9,LOW); // stop both motors
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
void turnRight(int dlay,int lWheelSpeed)
{
analogWrite(6,lWheelSpeed);
digitalWrite(9,HIGH); // make the left motor move forward
digitalWrite(10,LOW);
digitalWrite(11,LOW); // stop the right motor
digitalWrite(12,LOW);
delay(dlay);
}