perhaps you can post your latest code and tell us (again? ) specifically what type of sensor you are using...
would help a lot
perhaps you can post your latest code and tell us (again? ) specifically what type of sensor you are using...
would help a lot
I'm using the Parallax PING))) Sensor
boolean delay_without_delaying(unsigned long time) {
// return false if we're still "delaying", true if time ms has passed.
// this should look a lot like "blink without delay"
static unsigned long previousmillis = 0;
unsigned long currentmillis = millis();
if (currentmillis - previousmillis >= time) {
previousmillis = currentmillis;
return true;
}
return false;
}
boolean delay_without_delaying(unsigned long &since, unsigned long time) {
// return false if we're still "delaying", true if time ms has passed.
// this should look a lot like "blink without delay"
unsigned long currentmillis = millis();
if (currentmillis - since >= time) {
since = currentmillis;
return true;
}
return false;
}
unsigned long ledtime = 0;
unsigned long atime, btime, ctime, nltime;
void measureDistance()
{
// set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
digitalWrite(signal, LOW);
if (delay_without_delaying(atime, 2)){
digitalWrite(signal, HIGH);
}
if (delay_without_delaying(btime, 3)){
digitalWrite(signal, LOW);
}
pinMode(signal, INPUT);
pulseduration=pulseIn(signal, HIGH) / 2;
distance = int(pulseduration/29);
indistance = microsecondsToInches(pulseduration); //<<<<<<<<<<< changed 'indistance' to FLOAT
// potValue = map(analogRead(potPin),0,1023,0,100);
}
void printDistances(){
// Serial.print(indistance);
static uint8_t putIndex = 0;
if ((indistance + 3) <= curdistance || (indistance - 3) >= curdistance)
{curdistance = indistance;
dtostrf(curdistance,4,0,buffer);
// print the text and the variable to newMessage
sprintf(newMessage, "Dist: %s inches\0", buffer);
newMessageAvailable = true;
}
}
[i][...][/i]
void loop()
{
measureDistance();
printDistances();
readSerial();
if (P.displayAnimate())
{
if (newMessageAvailable)
{
strcpy(curMessage, newMessage);
newMessageAvailable = false;
}
P.displayReset();
}
}
As you may have guessed, this is part of a larger code. However, what's odd, is that the code works ...except every few seconds (counting it, I assume it's the code looping, so every loop) the display and PING readings stop, and continue. If I put my hand over the PING sensor, everything slows to a crawl.
Thanks for all of the help - I think I need to just look at the whole thing on my own, and narrow down what's going on so I can describe it more accurately. I don't mean to waste anyone's time. I'm trying many things, but will just report back once I get it down to more specific problems. Thanks again everyone ![]()
pinMode(signal, OUTPUT);
digitalWrite(signal, LOW);
if (delay_without_delaying(atime, 2)){
digitalWrite(signal, HIGH);
}
if (delay_without_delaying(btime, 3)){
digitalWrite(signal, LOW);
}
pinMode(signal, INPUT);
pulseduration=pulseIn(signal, HIGH) / 2;
This is stupid. You do NOT know that any microsecond delay happened, so you do not know if you actually sent a pulse. You don't know that any microsecond delay happened, so you don't know that you stopped sending a pulse. And, yet, you use a blocking function to wait for a reply.
As I mentioned earlier, delayMicroseconds() is NOT what is causing the problem. It is pulseIn() WAITING for a reply that IS. Get rid of the stupid delay_without_delaying() functions.
You REALLY need to look at the NewPing() library which uses interrupts to get the echo time, in a non blocking way.