softwareSerial buffer not clearing

can anybody see why i am getting stuck in a continual loop?

#include <SoftwareSerial.h>                                   //Software lbrary - keeps rx/tx lines open for uploads
SoftwareSerial simc(5, 4);           
const int choke_on = 9;                                      // motor driver input
const int choke_off = 8;                                     // motor driver input
  pinMode(choke_on, OUTPUT);
  pinMode(choke_off, OUTPUT);

void loop() {                                                                                                     // loop()
  int j = analogRead(A0);
 // Serial.println(j);
  int DTMF = checkDTMF();                                                                                         // check incoming DTMF tone

  if (DTMF == 1) {
    delay(10);
    battery_sense();
  }
  if (DTMF == 3) {
    delay(10);
    engine_off();
  }
  if (DTMF == 2) {
    delay(10);
    parameters();
  }
  if (DTMF == 8) {
    delay(10);
    manflag = 1;
    Start();
  }
  if (DTMF == 9) {
    delay(10);
    choke_in();
  }
  if (DTMF == 7) {
    delay(10);  
    choke_out();
  }
  if (DTMF == 5) {
    delay(10);
    timer_off();
  }
  if (DTMF == 6) {
    delay(10);
    timer_up5();
  }
  if (DTMF == 4) {
    delay(10);
    timer_up15();
  }
}

void simFlush() {                                                                                                           // simFlush()
  delay(20);
       while (simc.available ())
       simc.read ();
}

int checkDTMF() {                                                                                                             //checkDTMF()
  if (simc.find("+DTMF:")) {
    return simc.parseInt();
    simFlush();
  }
}
void choke_in() {
  digitalWrite(choke_off, LOW);
  digitalWrite(choke_on, HIGH);                                                                                             // Fully close the choke
  delay(500);                                                                                                               // Give servo time to act
  digitalWrite(choke_on, LOW);
  delay(2000);
  v(10); delay (1000);                                                                                                      // "CHOKE"
  v(11); delay (1200);                                                                                                      // "ON"
  Serial.println("choke in"); 
}

if i call any of the functions they will just continuously repeat, is this something to do with the way i am calling the functions (using IF statements?).

even if i include the flush at the end of a function it still repeats?

You need to post the complete program so we can see how everything is defined.

The flush() function does not empty the input buffer - it is intended to block the Arduino until all the outgoing data is sent.

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

Robin2:
You need to post the complete program so we can see how everything is defined.

EDITED

Robin2:
The flush() function does not empty the input buffer - it is intended to block the Arduino until all the outgoing data is sent.

Sorry i was not implementing the flush() function. What i meant to say is that even if i try to flush the buffer with:
while (simc.available ())
simc.read ();
at the end of the function the it still repeats,

at this moment i'm looking at the line ending, but thought that the buffer would be cleared through the simc.read()....

Get rid of all flush() and delay() calls and see if that helps any.

Instead of calling this function: simFlush(); to flush/empty the serial buffer, use the following codes:

do
{
    (void)simc.read();
}
while(simc.available() != 0);

GolamMostafa:
Instead of calling this function: simFlush(); to flush/empty the serial buffer, use the following codes:

do

{
    (void)simc.read();
}
while(simc.available() != 0);

The OP's simFlush() function does exactly the same as your code.

...R

@andyroid, you have this line of code

  int DTMF = checkDTMF();

but you have nothing to let you see what values it is receiving.

Have you carefully studied the examples in the link I gave you in Reply #1

...R

Robin2:
The OP's simFlush() function does exactly the same as your code.

void simFlush() {                                                                                                           // simFlush()

delay(20);
      while (simc.available ())
      simc.read ();
}

Except that the OP's function/codes contain undesired/disturbing delay().

Robin2:
@andyroid, you have this line of code

  int DTMF = checkDTMF();

but you have nothing to let you see what values it is receiving.

Have you carefully studied the examples in the link I gave you in Reply #1

...R

having a read through those Serial input basics and tested out the second example:

receiving a char array like this:

+DTMF: 1

could i then use int x = receivedChars[7]; to isolate the key(DTMF) press?

andyroid:
receiving a char array like this:

+DTMF: 1

could i then use int x = receivedChars[7]; to isolate the key(DTMF) press?

Nearly, it needs to be

int xVal = receivedChars[7] - '0';

receivedChars[7] will have the Ascii value 49. Subtracting the Ascii value for '0' will give you the value 1

And it is a bad idea to use single character variable names - they can be impossible to find with search or search and replace. Always use meaningful variable names.

...R

Robin2:
Nearly, it needs to be

int xVal = receivedChars[7] - '0';

receivedChars[7] will have the Ascii value 49. Subtracting the Ascii value for '0' will give you the value 1

And it is a bad idea to use single character variable names - they can be impossible to find with search or search and replace. Always use meaningful variable names.

...R

OK thanks robin for the help, rather than doing the math ill just get it to look for the ascii values instead, this way i can incorporate the # and * keys as below:

#include <SoftwareSerial.h>
SoftwareSerial simc(5, 4);
const byte numChars = 12;
char receivedChars[numChars];
bool newData = 0;
char s;
void setup() {

  Serial.begin(9600);
  simc.begin(9600);
  Serial.println("starting");
  delay(3000);
  simc.println("ATS0=1");
  delay(3000);
  simc.println("AT+DDET=1,1000,0,0");
  delay(3000);
}

void loop() {
  recvWithEndMarker();
  showNewData();
}
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (simc.available() > 0 && newData == false) {
    rc = simc.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    int s = receivedChars[7];
    newData = false;
    if (s == 35) {
      Serial.println("#########");
    }
    if (s == 48) {
      Serial.println("000000000");
    }
    if (s == 42) {
      Serial.println("***********");
    }
    if (s == 51) {
      Serial.println("333333333");
    }
    if (s == 52) {
      Serial.println("444444");
    }
    if (s == 53) {
      Serial.println("5555");
    }
    if (s == 54) {
      Serial.println("6666666666");
    }
    if (s == 55) {
      Serial.println("777777777");
    }
    if (s == 56) {
      Serial.println("88888888");
    }
    if (s == 57) {
      Serial.println("99999999");
    }
    if (s == 50) {
      Serial.println("222222222222");
    }
    if (s == 49) {
      Serial.println("1111111111111111");
      delay(3000);
      Serial.println("done");
    }
  }
}

However each subsequent key press will repeat the function, any key press after the first will print the old value first and then the new value:

starting
000000000
000000000
1111111111111111
done
1111111111111111
done
222222222222
222222222222

So i'm looking as to why the previous data is carried with the new called data?

Try this version and tell us what happens

void showNewData() {
  if (newData == true) {
    int s = receivedChars[7];
    Serial.print("Raw data received ");
    Serial.println(s);
    newData = false;
  }
}

By the way rather than this style

if (s == 35) {
      Serial.println("#########");
}

your program will be much easier to understand if you write it like this

if (s == '#') {
      Serial.println("#########");
}

It will save you having to check what character has the Ascii value of 35

...R