How to run a code after pushing enter in your keyboard?

Well I realized that to execute a code in IDE arduino by pressing enter in the keyboard is not automatic, but must be programmed, could you please explain to me how I can set this function?
Here is a code that shows the sum of evens and odd of products just when you write a negative number, I'd like to change it; I mean I'd like to run the program by typing the enter key of mi computer keyboard please.

long SumOfEvens = 0;
long ProductOfOdds = 1;

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("Enter numbers, separated by spaces or commas.");
  Serial.println("Enter a negative number to indicate the end of input.");
}

void loop()
{
  if (Serial.available() == 0)
    return;  // Nothing to do

  int number = Serial.parseInt();

  // Throw away any line ending characters
  while (Serial.peek() == '\n' || Serial.peek() == '\r')
    Serial.read();

  if (number < 0)
  {
    // Negative number: End of input
    Serial.print("Sum of even numbers: ");
    Serial.println(SumOfEvens);
    Serial.print("Product of odd numbers: ");
    Serial.println(ProductOfOdds);

    // Reset
    SumOfEvens = 0;
    ProductOfOdds = 1;
  }
  else if ((number % 2) == 0)
  {
    // Even
    SumOfEvens += number;
  }
  else
  {
    // Odd
    ProductOfOdds *= number;
  }
}

someone please? I saw about writing KEY_ENTER and ASCII code, but it doesn't work

Just do a Serial.read() when Serial.available() indicates there is a character available and ignore everything except the line ending. When you see a line ending then proceed with the program. If you are only doing this once you could put it at the end of setup().

thanks a million my friend

This code looks familiar - is this a class assignment?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.