Strange Characters when using serial print

I am getting strange characters when I Serial Print. The first print is as expected but all the following printing of the text "avginput" are scrambled.

I've included the code and a screen shot of the serial monitor and any help would be appreciated.

Thanks,

Tom

/*
  TAS Design 11-28-21
*/

//Initialise LCD display

#include <LiquidCrystal.h>
#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library
LiquidCrystal lcd(4, 6, 9, 3, 5, 7);

File myFile;

//Setup Variables

const int sensorPin = A1; //Defines the pin that the anemometer output is connected
float sensorInput; // Raw data fron sensor input
float sensorvalue;
int numbercount = 10; //Number of inputs to average
float inputsum = 0; //sum of inputs during count cycle
float avginput = 0; // average input value for (numbercount) inputs
int mapval = 0; //Value of avginput mapped to 0 to 72
int delaysec = 10000;   // Time for delay in updating reading

int chipSelect = 10;  // chipselect pin for the SD card reader

void setup()
{

  //Setup LCD display with welcome screen

  lcd.begin(16, 2);
  lcd.print("TAS 11-28-21");
  lcd.setCursor(0, 1);
  lcd.print("Windspeed Sensor");
  lcd.setCursor(0, 0);
  Serial.begin(9600);  //Start the serial connection

  // Check for SD card initialization

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}

void loop()
{
  sensorInput = analogRead(sensorPin); //Get a value between 0 and 1023 from the analog pin connected to the anemometer
  sensorvalue = sensorInput;
  inputsum = 0;

  for (int i = 1; i <= numbercount; i++)  //Count number of cycles taken for average
  {

    inputsum = inputsum + sensorvalue; //Accumulate readings for average

  }

  avginput = inputsum / numbercount; // compute average input value for (numbercount) inputs

  Serial.print("   avginput: ");
  Serial.println(avginput);
  mapval = avginput;
  mapval = map(mapval, 87, 1023, 0, 72); // mmap averag value to wind speed
  if (mapval < 0) {
    mapval = 0;
  }
  Serial.print("Wind MPH: ");
  Serial.println(mapval);

  lcd.setCursor(0, 0);
  lcd.print("Wind MPH = ");
  lcd.print( mapval);

  // SD Card Write
  Serial.begin(9600);
  //Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  //open the file.
  myFile = SD.open("Weather.txt", FILE_WRITE);

  //write to file
  if (myFile)
  {
    Serial.print("Writing to Weather.txt...");
    myFile.print("Wind MPH   ,");
    myFile.println(mapval);
    //close the file
    myFile.close();
    Serial.println("done.");
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("error opening Weather.txt file");
  }

  delay (delaysec);  // Delay so reading updates every x seconds

}

Why are you doing this in the loop function?

It looks like this string constant is being clobbered, some time between the first time it is used and the second time it is used. I don't see anything in your sketch that looks like it would be writing into random memory so I don't know how the problem occurs.

How much memory does the compiler show that the sketch is using? The SD library will need at least 512 bytes of ram for its buffer.

Use the F() macro for the text in the print function.

Thanks for the responses.

As to why am I doing that loop function. I'm not good at coding so most of my efforts are cut and paste from others code and then try and make it work for my application. That's why that bit of code is there.

As for the memory, attached is a screen shot that seems to say there is not a memory issue but I really don't know.

Also, I don't know what the F() macro is or to use it.

I looked up the F() macro, added it to all the serial print functions and the result is the same. Garbled characters the second time it prints that string.

OK, but if you ever see code using that command again in anything that is not the setup function or a function that is only called in the setup function, then run away from that code as fast as possible. It is almost certain that whoever wrote the code has absolutely no idea what they are doing, so the rest of the code is likely to be a big pile of poo too.

Remove it permanently from your code.

Have you removed the Serial.begin from loop? That is likely interfering with the serial output.

if you are using the F() macro with that particular text, then it is not being overwritten, the serial buffer is probably getting reset by the serial.begin.

Thanks everyone,

Removing that code from the loop solved the problem.

Best regards,

Tom

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