Loop skipping if statements, or problem with ASCII lettering.

Well, here's the code.

const int inbuiltled = 13;
const int sw1 = 2;
const int sw2 = 3;
const int ledred  = 5;
const int ledgreen = 4;
int DataReceived = 0;
long previousMillis = 0;
long interval = 1000;
int message = 0;
int isChere = 0;
int ismessagehere = 0;
int isEhere = 0;
int ledstate = LOW;
int Qcount = 0;
int lastmessagesent = 0;
int key;

void setup() {
  pinMode(inbuiltled, OUTPUT);
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  pinMode(ledgreen, OUTPUT);
  pinMode(ledred, OUTPUT);


  Serial.begin(1200);
  Serial.flush();
}

void loop() {
  unsigned long currentMillis = millis();
  DataReceived = Serial.read();
  if (Serial.available() > 3) {

    Serial.end();
    Serial.begin(1200);
    Serial.flush()
    isChere = 0;
    isEhere = 0;
    ismessagehere = 0;

  }
  if (DataReceived == 'C') {
    isChere = 1;
    Serial.print('2');
  }
  if (isChere == 1) {
    message = DataReceived;
    Serial.print('1');
    ismessagehere = 1;

  }
  if (ismessagehere == 1) {
    if (DataReceived == 'E') {
      isEhere = 1;
      isChere = 0;
      key = 1;
      Serial.print('3');
    }
  }
  if (key == 1) {
    Serial.print('4');
    if (message == 'Q') {
      Qcount = Qcount + 1;
      message = lastmessagesent;
      Serial.print('6');
    }
    if (Qcount == 5) {
      message = 0;
      isChere = 0;
      isEhere = 0;
      ismessagehere = 0;
      key = 0;
      Serial.print('5');
    }
    if (message == 'M') {
      digitalWrite(ledgreen, HIGH);
      Serial.print('C');
      Serial.print('K');
      Serial.print('E');
    }
    if (message == 'N') {
      if (currentMillis - previousMillis > interval) {
        if (ledstate == LOW) {
          ledstate = HIGH;
        }
        else {
          ledstate == LOW;
        }
        digitalWrite(ledgreen, ledstate);
      }
      Serial.print('C');
      Serial.print('K');
      Serial.print('E');
    }
    if (message == 'D') {
      digitalWrite(ledgreen, LOW);
      Serial.print('C');
      Serial.print('K');
      Serial.print('E');
    }
    if (message == 'R') {
      digitalWrite(ledred, HIGH);
      Serial.print('C');
      Serial.print('K');
      Serial.print('E');
    }
    if (message == 'F' || message == 'D') {
      digitalWrite(ledred, LOW);
      Serial.print('C');
      Serial.print('K');
      Serial.print('E');
    }
    if (message == 'K') {
      Qcount = 0;
    }
  }
  if (sw1 == LOW) {
    Serial.print('C');
    Serial.print('T');
    Serial.print('E');
  }
  if (sw2 == LOW) {
    Serial.print('C');
    Serial.print('X');
    Serial.print('E');

  }
}

The code's meant to send little data packets marked out by Cs and Es, and receive them from the computer, and do various tasks depending on the letter. All of the number Serial.print() are just floaters to show me where the code has got up to. For whatever reason it continuously just goes around the "key == 1" if statement. It won't branch out into any of the other codes no matter what. As far as I know the breadboard layout isn't that important, but I'm using resistors. Thanks if you post any helpful stuff. I have no idea why it may be skipping, and I know it isn't a lack of ram, as both types are only at 10% capacity.

  DataReceived = Serial.read();
  if (Serial.available() > 3) {

Read something and then check that there's something to read - that's . . . novel.

I'm not sure what you're expecting the code to do. Tell us what input you are entering into the Serial monitor and what you expect to get in return.

time elements should be unsigned long, as that is what millis() and micros() return:

long previousMillis = 0; → unsigned long previousMillis;
long interval = 1000; → unsigned interval = 1000UL;

and no doubt most of these, the ones that will remain <= 255, can be byte instead of int:
const int inbuiltled = 13;

don't need unsigned long here

void loop() {
unsigned long currentMillis = millis();

if it's already been declared before setup.

@CrossRoads:

don't need unsigned long here

Am I missing something. I don't see the definition anywhere else.

I didn't look back - I normally have all the type declarations at the top before setup.

The examples in serial input basics are simple and reliable. They take in all the available data and then you can decide what to do with it.

...R

"I'm not sure what you're expecting the code to do. Tell us what input you are entering into the Serial monitor and what you expect to get in return."

I'm sending letters that should do various things back and forth, and using C & E to pad them, so that the computer knows where they are. For example, M should make the green light turn on, and N should make the green light blink. The buttons send data back to the computer, Z and something else, I can't remember. The Qcount is there in the event that the computer fails to interpret for message, as it will send "CQE" to show that the last message received was corrupt or not understandable for whatever reason. I forgot to add the lastmessagesent = message; under the if statements though. Also, the reason why I'm checking if there's more than three things in the cache is because I'm not sure whether or not having more than what I need in the serial cache will break something.

Flyro:
various things back and forth, and using C & E to pad them,

Have you looked at the examples in serial input basics. The 3rd example uses < and > to surround the data. You could easily change that to C and E

…R