Arduino/pingsensor, prints ONCE but not constantly

I'm using an arduino with a ping sensor, that prints out to a gobetwino and to flash. It seems like it's giving too much information and gets lost in how much info the ping sensor gives out... but I also need it to detect things constantly. I was wondering if there is a way where the arduino prints a letter once the ping sensor picks up something under certain variables, but doesn't constantly say something is there. So if something is between 1 and 5 inches, it says "A" once, but will say "B" once the object is out of the range.

Thanks. I hope that makes sense.

This alternates between 'A' and 'B', even though the funciton that returns sensorState evaluates true/false multiple times, neither 'A' nor 'B' will print multiple times.

boolean unique = true; boolean sensorState = false; //FOR DEBUGGING

void setup(){ Serial.begin(9600); }

void loop(){ //if sensor reading indicates a breach, AND the breach is unique - print A if (readSensor()&&unique){ Serial.println("A"); unique=false; } //if sensor reading does not indicate a breach, AND the breach is not unique (or rather, was unique) - print B else if(!readSensor()&&!unique){ Serial.println("B"); unique=true; } }

boolean readSensor(){ millis()%1000 == 0 ? sensorState=!sensorState : sensorState; //simulated randomness return sensorState; }

I’m having trouble incorporating the code with this…

Should I put the if statement found in the void loop into the readSensor() function?

edit: Basically I don’t understand the readSensor part

int pingPin = 4;
int serInLen = 25;
char serInString[25];
int pId =0;
int result;

boolean unique = true;
boolean sensorState = false;

void setup() 
{ 
  Serial.begin(9600); 
  Serial.println("#S|SPXL|[]#");        // start EXCEL
    pId= atoi(serInString);  
             // convert result to integer 
} 
 
void loop() 
{ 
   long duration, inches, cm;

char buffer[5];
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  if(cm>=1 && inches<=5)
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  
} 


  delay(500);
}

  


//read a string from the serial and store it in an array
//you must supply the array variable - will return if timeOut ms passes before the sting is read so you should
//check the contents of the char array before making any assumptions.

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

boolean readSensor(){
  millis()%1000 == 0 ? sensorState=!sensorState : sensorState;
  return sensorState;
}

void readSerialString (char *strArray,long timeOut) 
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}

I think I got it.

int pingPin = 4;
int serInLen = 25;
char serInString[25];
int pId =0;
int result;

boolean unique = true;

void setup() 
{ 
  Serial.begin(9600); 
  Serial.println("#S|SPXL|[]#");        // start EXCEL
    pId= atoi(serInString);  
             // convert result to integer 
} 
 
void loop() 
{ 
   long duration, inches, cm;

char buffer[5];
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  if(cm>=1 && inches<=5 &&unique)
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=false;
  
} 
else if (inches>5 &&!unique)
 {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("b");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=true;
  
} 
  

  delay(100);
}

  


//read a string from the serial and store it in an array
//you must supply the array variable - will return if timeOut ms passes before the sting is read so you should
//check the contents of the char array before making any assumptions.

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


void readSerialString (char *strArray,long timeOut) 
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}