Hi all, just getting started on a new Arduino project here and I was wondering if you could help me out with this - I'm rather new to all of this.
I have a HC-SR04 sensor mounted on top of the stepper motor, and I want to turn the motor, take a reading (several then averaged), until a specified point is reached. I've got the ultrasonic sensor part worked out but I'm stuck with regards to the motor.
I'm using the AccelStepper and NewPing libraries.
Here's what I have so far:
#include "AccelStepper.h"
#include "NewPing.h"
#define TRIG_PIN 12 //Arduino pin for trigger
#define ECHO_PIN 11 //Arduino pin for ECHO_PIN
#define MAX_DISTANCE 200 //Max dist for ping in cm
AccelStepper steepper(); //Need to define pins
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE)
void setup()
{
stepper.setMaxSpeed(1000.0);
stepper.setSpeed();
Serial.begin(115200); //Open serial at 115200 baud
}
void loop()
{
delay(50); //Wait 50ms between pings
unsigned int uS = sonar.ping_median(5); //Send 5 pings, receive median ping time in microseconds
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); //Convert ping time to dist & print result
Serial.println("cm");
}
Not a lot - but I'm struggling quite hard with the motor part.
Alots on how to deal with stepper motors in the playground. You will need a motor shield (look in the products section. Also take a look at the sharp IR sensors - which are a lot easier to use. Take sure you understand "blink without delay" taking 5 US readings like that will give you problems in the end.
I've looked at the Playground but I'm still rather confused about how to code this for the time being, but I'll keep reading up on it! If you can provide and code snippets or even pseudocode it would help.
Can't use an IR sensor due to project spec reasons sadly. What kinds of problems will I run in to if I take 5 US readings? The motor will be off/position will be static during this time.
start with the code you found, this will give you a nice easy stepper.
if you step 20 times and take a sample with your pinger it is fine.
put the readings in an array and steer yor robot to the longest distance found in the array.
in the end the robot will get in the middle of the room.
accelstepper is fine when using multiple steppers at a time or when you need unsupervised motion.