Pulling my hair out!

Ok. Here is part of my sketch. My problem is that I cannot get the WriteEEPROM() function to work. It takes a string of digits, inputed by serial, converts it to digits, and attempts to write it to EEPROM. In this case, the number to be inputed is an integer.

// THIS ACCEPTS A CHARACTER DEFINING WHICH PARAMETER TO PROMPT THE USER TO PROGRAM INTO THE EEPROM
void WriteEEPROM(char selection)
{
  int i = 0;
  int result = 0;        

  Serial.println("Enter numerical values into the prompted field.\n");
  if ('e' == selection)
    {
      // ANNOUNCE AND PROMPT USER FOR ENGINE PARAMETER
      Serial.print("\tNo. of Cylinders:  ");
      Serial.print(numCylinders, DEC);
      Serial.print("?\t");

      // CALL THE SERIAL FUNCTION TO GRAB ALL SERIAL.READ BEFORE '\R' (Enter)
      grabSerialStr();                                            // assume the user is not an idiot and can enter all numbers into the field

      // TAKE THE STRING AND CONVERT INTO A INT.  IT TAKES THE LAST DIGIT AND CONVERTS TO A INT AND ADDS IT TO RESULT.
      // IT TAKES THE SECOND TO LAST DIGIT, MULTIPLIES IT BY 10 (FOR 10'S DIGIT) AND IT TO RESULTS, etc, etc, etc
      result = 0;      
      for (int i = 0; i > incomingLength; i++)
        {
          result = pow(10,i) * (incomingChars[incomingLength - i - 1] - '0') + result;
        }

      Serial.print(" ...writing ");                              // tell the user that it is writing the value
      Serial.println(result, DEC);                               // display the value that is being written
      EEPROM.write(16, result);                                  // write the value to the memory address.
    }  
    
    // ====================================================================
    // ADD THE REMAINING CODE FOR THE VARIOUS FUNCTIONS TO WRITE TO EEPROM.
    // ====================================================================

}


// CALL THE SERIAL FUNCTION TO GRAB ALL SERIAL.READ BEFORE '\R' (Enter)
void grabSerialStr()
{
  incomingLength = 0;
 do
  {
    while (!Serial.available())
      {
        ;                                              // sit here and wait until serial is no longer valid and then proceed to the remaining loop.
      }
    incomingChars[incomingLength] = Serial.read();     // read the serial port and input it into the array.
    if ('\r' == incomingChars[incomingLength])         // if '\r' (Enter) is encountered, end the loop to exit the function
      {
        Serial.println();                              // clear out the line
        break;
      }
    Serial.print(incomingChars[incomingLength]);       // print each character as it is typed in
    incomingLength ++;                                 // increment to the next available string position for input from serial
    if (incomingLength > 8)                            // check to see if input reaches max resonable input length and terminate array with carriage return.
      {
        incomingChars[incomingLength] = '\r';          // add carriage return.  
        break;
      }
   
    // note: i add a carriage return to any string so it acts as a marker for knowing the end of the valid data.
  }
  while (1);                                           // forever loop which requires a break when a '\r' is meet or added.
}

Hi Frank,

It would be helpful to see the whole sketch, and perhaps a better description of what is going wrong rather than “it doesn’t work”. However, off the top of my head I note that this loop will never execute unless incomingLength is negative:

for (int i = 0; i > incomingLength; i++) 
{
  result = pow(10,i) * (incomingChars[incomingLength - i - 1] - '0') + result;
}

Better is something LIKE:

int result = 0;
for (int i=0; i<incomingLength; ++i)
{
  result = 10 * result + incomingChars[i] - '0'; 
}

And are you aware that

EEPROM.write(16, result);

writes only a BYTE of data (0-255) to the EEPROM at offset 16? So if “result” is ever greater than 255, you will be losing data. If so, it the next iteration we can talk about storing larger values in EEPROM.

Mikal

Wow.... that makes me want to punch myself in the face. That has been a the problem... filped sign!

:) Sometimes it just takes someone else to take a peek.

Good luck,

Mikal