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
}