bool gotCmd=FALSE; bool gotData=FALSE; void loop() { if (Serial.available() > 0) { cmd = Serial.read(); gotCmd = true; } if (Serial.available() > 0 && gotCmd) { num = int(Serial.read()); gotData = true; } if (gotCmd && gotData) { // switch your stuff gotCmd = false; gotData = false; } }