Hi all,
first i dont want to be the ‘subject’ of cross-posting — but i got some help in the sensor forum, but it’s now only about programming so I thought id post a new topic here, Ill update the other topic with a link to this post. Link to old post http://forum.arduino.cc/index.php?topic=186279.0
So, im using a distance sensor, since i want to scan 180° i got some great tips of mounting the sensor on a servo, reading the sensor values to an array.
Ill quote 'groundfungus ’
‘Scan the ranges into an initial array. Save that array to oldRanges. After the next scan, newRanges, you can compare the arrays for changes. Be aware that you can’t scan very fast. Look at the servo specs and they should have a time to slew 60°. Usually on the order of 200mS so 180° is a minimum of 600mS without time to stop and get a range, ideally.’
So i want to scan from 0 to 180 for oldRanges then reverse the oldRanges array and start collecting (180 to 0) newRanges so the array indexes match. Before the next scan, copy the newRanges to oldRanges (overwrite the old oldRanges) and collect a new set of newRanges.
So if oldRanges and newRanges are different at the servo pos of 100° the servo will stop at that position and the sensor will scan, when the object is gone (without range) it the servo will start rotating again. The sonar is output as ‘sonarValue’
So i need some guidance setting up the writing to the oldRanges & newRanges array comparing revering and comparing it.
When i increased the speed of the servo it only goes to 150°.
Id be most glad for any help!
Best
#include <NewPing.h>
#include <VarSpeedServo.h>
VarSpeedServo MyServo; // servo objects
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// Declare variables
const unsigned long distansInterval = 50;
const unsigned long distansTracker = 10;
const unsigned long servoTracker = 1;
int sonarValue;
int uS;
int servoSpeeds = 30; // sweep speed, 1 is slowest, 255 fastest)
int servoMinPosition = 0; // the minumum servo angle
int servoMaxPosition = 180; // the maximum servo angle
unsigned long distansTimer;
unsigned long distansTrackerTimer;
unsigned long servoTimer;
// End vars
void setup()
{
Serial.begin(115200);
distansTimer = millis ();
distansTrackerTimer = millis ();
MyServo.attach(9);
MyServo.slowmove(servoMinPosition,servoSpeeds) ; // start sweeping from min position
}
void runServo()
{
if( MyServo.read() == servoMinPosition)
{
MyServo.slowmove(servoMaxPosition,servoSpeeds) ;
}
else if( MyServo.read() == servoMaxPosition)
{
MyServo.slowmove(servoMinPosition,servoSpeeds) ;
}
}
/* //////////////
Update sensor every 50 ms
*//////////////
void distansIntervalFunction ()
{
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
sonarValue = uS / US_ROUNDTRIP_CM;
distansTimer = millis ();
}
/* //////////////
Send to exbee
*//////////////
void distansTrackerFunction ()
{
}
/* //////////////
START LOOOOP
*/
void loop ()
{
Serial.println("MILLIS");
if ( (millis () - distansTimer) >= distansInterval)
{
// updates the sonar
distansIntervalFunction ();
}
if ( (millis () - servoTimer) >= servoTracker)
{
runServo ();
}
}