int dirA = 12;
int dirB = 13; // not used in this example
int speedA = 10;
int speedB = 11; // not used in this example
void setup()
{
pinMode (dirA, OUTPUT);
pinMode (dirB, OUTPUT);
pinMode (speedA, OUTPUT);
pinMode (speedB, OUTPUT);
}
void loop()
{
// move the motor A to one direction increasing speed
digitalWrite (dirA, HIGH);
for (int j = 0; j < 255; j += 10) {
analogWrite (speedA, j);
delay (100);
}
delay(1000); // keep the motor rolling for one second
// move the motor A to one direction decreasing speed
digitalWrite (dirA, HIGH);
for (int j = 255; j >= 0; j -= 10) {
analogWrite (speedA, j);
delay (100);
}
// stop the motor
digitalWrite(speedA, LOW);
delay(1000); // keep the motor stopped for one second
}
I tried fiddling with the codes, to ensure that the motor can run continuously (or a bit longer than the original setting). Though, I get the exact same results.
The one below is the 'modified' code
int dirA = 12;
int dirB = 13; // not used in this example
int speedA = 10;
int speedB = 11; // not used in this example
void setup()
{
pinMode (dirA, OUTPUT);
pinMode (dirB, OUTPUT);
pinMode (speedA, OUTPUT);
pinMode (speedB, OUTPUT);
}
void loop()
{
// move the motor A to one direction increasing speed
digitalWrite (dirA, HIGH);
for (int j = 0; j >= 255; j+=50) {
analogWrite (speedA, j);
// delay (100);
}
delay(10000); //
// move the motor A to one direction decreasing speed
// digitalWrite (dirA, HIGH);
// for (int j = 255; j >= 0; j -= 10) {
// analogWrite (speedA, j);
// delay (100);
// }
// stop the motor
digitalWrite(speedA, LOW);
// delay(1000); // keep the motor stopped for one second
}
The first for loop used to slowly increase the speed of the motor (a 1/10th of a second pause at each speed), up to full speed. Then, the speed remained constant while the delay executed, for 1 second.
The second loop then decreased the speed of the motor, slowly, until it was stopped. Then, it waited one second, and started over.
In your modified code, the motor speed is not slowly increased. The loop executes very quickly (a few millionths of a second). At the end of the loop, the motor is supposed to be going full speed. Being a physical device, with friction and inertia, etc., it takes a while to get to full speed.
The, there should be a 10 second period where the motor runs at full speed (once it gets there).
Then, the loop ends, and begins again. The speeds is very quickly ramped up (far too fast for the motor to notice that the power was changed). The motor should basically remain at full speed.
If this is not what is happening, you need to explain what motor shield you have, what motor(s) you have, how the motors are connected to the shield, and how they are powered, and what is happening.
That is not happening. I assume that this code (plus your explanation here) should run the motor endlessly?
....you need to explain what motor shield you have, what motor(s) you have, how the motors are connected to the shield, and how they are powered, and what is happening.
The motor shield used is exactly like the one used in the example.
Motors are connected by a JST to the following terminals (circled in red - as the original image suggests).
The link in your original posts shows a picture of a shield installed on an Arduino. I didn't see a link to the specs for the shield, though.
It shows a small DC motor attached. Your reply shows a much larger motor, and no details about where you are supplying the necessary power. The picture in the first post doesn't show external power being applied, either.
If you are expecting the Arduino to supply the power, I think you are going to be disappointed.