How do you plan to specify the speed?
Is it supposed to run at different speeds or do just want it running at a speed slower than it does now?
// Wait for pulse on echo pin while ( digitalRead(ECHO_PIN) == 0 ); // Measure how long the echo pin was held high (pulse width) // Note: the micros() counter will overflow after ~70 min t1 = micros(); while ( digitalRead(ECHO_PIN) == 1); t2 = micros(); pulse_width = t2 - t1;
All of this can be replaced with:
pulse_width = puleIn(ECHO_PIN, HIGH, MAX_DIST);
And the NewPing library can handle all of the low-level stuff for you and deliver distances in cm.
One problem with the code as written. If no echo is seen the Echo pin will never go HIGH and your first while() loop will hang forever.
You forgot to say which Arduino you are using. Pin13 isn't a PWM pin on many of them so analogWrite won't do what you expect.
What exactly are the motors you are using because an L298N will only run small motors (and not very well). "Kids ride on car motors" sound like they will be way too big.
O.k. you need to move enA to a different pin. Valid PWM pins for speed control are 3, 5, 6, 9, 10, 11.
12000rpm 12V isn't really a motor specification. Do you have a link to the motors you're using?
What are the full load and stall current of those motors?
Thanks for the replies, I'm using an Arduino Uno, I just need to lower the speed in both directions, also using two 12000 rpm 12v motors.
Change the number you pass to analogWrite (after you've redone the wiring as suggested). Try 128 to start with and experiment with it until you get the speed you need. Be aware that at some point, the number will be too low to move the motors at all.
Sorry, just noticed you're using Servo.h so that means you can't use PWM on pins 9 and 10 either. So enA needs to be on one of 3,5,6 or 11.