Doing Something Once in a Loop?

Okay, so i am pretty unexperienced with all this coding stuff but am just looking for a little advice on a small piece of code.
If i have this in the void loop of my code, how can I have it so that it only prints 'k' in serial ONCE everytime x is less than -70?

  if (x < -70) {
    Serial.println('k');
    delay(750);
    }

Thanks in advance.

GamecubePerson111:
Okay, so i am pretty unexperienced with all this coding stuff but am just looking for a little advice on a small piece of code.
If i have this in the void loop of my code, how can I have it so that it only prints 'k' in serial ONCE everytime x is less than -70?

  if (x < -70) {

Serial.println('k');
    delay(750);
    }




Thanks in advance.

You need to look for the signal edge. Assuming x is a signed int, you need to keep track of the last value you checked. If it's now less than -70 and wasn't the last time, then the signal edge occurred, and you can print what you need.

  static boolean beenprinted;

  if (x < -70) 
  {
    if ( ! beenprinted )
    {
      beenprinted = true;
      Serial.println('k');
      delay(750);
    }
  }
  else
  {
    beenprinted = false;
  }

Thanks for the reply, for that code (Coding Badly) it says that beenprinted has not been declared in the scope, what do I need to declare it as?

Oops, forgot to copy the 'static boolean beenprinted' line, thank you so much for your help!

I tried incorporating your method of coding because I need this to happen for several different values, but it seems to still print in serial more than once.

Here is my code

    if (x < -70) 
  {
    if ( ! beenprinted )
    {
      beenprinted = true;
      Serial.println('k');
      delay(750);
    }
  }
  else
  {
    beenprinted = false;
  }
  
   if (x > 90) 
  {
    if ( ! beenprinted )
    {
      beenprinted = true;
      Serial.println('a');
      delay(750);
    }
  }
  else
  {
    beenprinted = false;
  }
  
  if ((x > -70) && (x < 90)) {
    Serial.println('n');
  }
  
   if (y < -70) 
  {
    if ( ! beenprinted )
    {
      beenprinted = true;
      Serial.println('t');
      delay(750);
    }
  }
  else
  {
    beenprinted = false;
  }
  
   if (y > 80) 
  {
    if ( ! beenprinted )
    {
      beenprinted = true;
      Serial.println('v');
      delay(750);
    }
  }
  else
  {
    beenprinted = false;
  }
  
  if ((y > -70) && (y < 80)) {
    Serial.println('m');
  }

The code only keeps the 'k' from printing twice in a row. If you want it to print only once -- ever -- then remove the else statement like this:

bool  beenprinted = false;

...

  if ((x < -70) && (! beenprinted ))
    {
      beenprinted = true;
      Serial.println('k');
      delay(750);
    }

Also, you'll need different "beenprinted" variables for each letter. Something like:

bool beenprinted_k = false;
bool beenprinted_a = false;