hbaxton:
I am using the new Ping library with my code and have hit a dead end. My code is running a SparkFun stepper motor in a loop where it slowly increments the RPM. I eventually want to control the speed based on the distance measurement from my Ping((( sensor, but I've dumbed it down to find the error. It's not the best code for my motor, but for example sake it works. The problem is that even though I use the ping_timer like in the example calling:
sonar.ping_timer(echoCheck);
stops the motor from moving. once I comment that line out the motor starts working again. below is the code. any ideas?
I'd start debugging this by removing the Serial.print/Serial.println stuff inside the echoCheck subroutine. This is only there for a test, you should eventually replace it with code that processes the ping result. But for now you can simply delete it. If it still doesn't work correctly, it could be that the AFMotor library is also using the same timer that NewPing is using to do the stepper motor control.
Also, cyclegadget is correct, you're not really using the pingTimer correctly. Both ping_timer() and the pingTimer variable are designed to be used WITHOUT delays. If you're using delays, it's going to not work correctly.
Basically, if you're going to use delays, you should not use any of the polling (pingTimer) stuff and shouldn't use the ping_timer() method. You should just use the standard ping() method. If you want to create a more event driven sketch, you need to be comfortable with not using delay commands.
I simplified your sketch and used the standard ping() method (well, ping_cm() which is ping that converts the result to centimeters). This may make more sense and be a closer fit to what you're trying to do.
#include <NewPing.h>
#include <AFMotor.h>
AF_Stepper motor(200, 1);
NewPing sonar(7, 7, 200); // NewPing setup of pins and maximum distance.
int rpm = 10;
void setup(){
Serial.begin(115200);
Serial.println("Stepper test!");
motor.setSpeed(100); // 100 rpm
motor.step(100, FORWARD, SINGLE);
Serial.println("step done");
motor.release();
delay(1000);
}
void loop(){
int cm = sonar.ping_cm();
Serial.print(cm);
Serial.println(" cm");
Serial.print("setting RMP = ");
Serial.println(rpm);
motor.setSpeed(rpm);
Serial.println();
Serial.println();
motor.step(100, FORWARD, SINGLE);
rpm += 10;
delay(1000);
}