Its a single channel datalogger application. With the Adafruit 32U4 Basic Proto... its a menu driven code interfacing with a LabVIEW app and all works as expected. Except the stability of the AnalogRead... the sensor is a LM35 chip and steady as a rock by itself.
After lots of struggle I decided to segregate only the Analog read part to check if that by itself is steady. That code is attached . This is very steady. SO this elimnates the following :
- Random Noise pickup by sensor wiring.
- The Analog input pin and the MCU itself.
- The code used for reading / averaging / displaying.
As you can see from the code attached there are lots of commented parts in the loop - these have nothing to do with the analog read part as mostly they are for the user menu interface.
The size difference between the attached code and the actual code is huge ... the actual is at 99% of the available flash. And the dynamic variables take 1700 bytes. Still OK for the 32U4.
I have observed the raw count as returned by the attached code - its steady at 96 counts. Which is approx 30 Deg C .
Whereas the actual code returns anything from 60 to 180 counts and pretty wildly varying.
Does code size have anything to do with the analog reads ?
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include "RTClib.h"
#include "RunningAverage.h"
#include <phi_interfaces.h>
#include <EEPROM.h>
#include <SD.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7); // 0x3F is the I2C bus address of LCD
RTC_PCF8523 rtc;
File logFile;
File root;
#define newFilePB A1
#define apendFilePB A2
#define sendFilePB A3
#define LogOnSW A4
# define VBATPIN 9
const byte scan_LED = 11;
const byte SDCselect = 10;
const int lm35Pin = A0; // Analog pin to use.
#define total_buttons 3
char mapDIN[] = {'N', 'D', 'S'}; // This is a list of names for each button.
byte pinDIN[] = { newFilePB, apendFilePB, sendFilePB }; // The digital pins connected to the 4 buttons.
phi_button_groups MyDIN(mapDIN, pinDIN, total_buttons);
boolean homeDispGate = true, setClockBit ;
boolean beginLogGate = true, fileChooseGate, newFileGate, oldFileGate, dumpDataGate ;
boolean eraseConfGate, doLoggingGate, logSwitchGate, waitOnSerGate, slTransmittGate, prepareTransfer;
boolean started, ended, readyToReceiveDump;
byte prevSecond;
byte index1;
char validDIN = '0';
char incomingByte ; //Variable to store the incoming Serial1 byte
char tBuffer[10];
char clockStringNow[17];
char LCDmsg[17];
char dataToLog[40];
char msgFromLVapp[40]; // Storage for message received from LabVIEW app
unsigned int loopInterval = 50;
unsigned int loggingInterval ; // Enter the interval in second
unsigned int epromAddress = 10;
unsigned int RA_SampleSize = 50;
unsigned int Val_00 = 0; // Variable to hold raw AI data
unsigned long scanMs = millis();
float measuredvbat;
float analog_Ch0Val, tempDegC;
float battVolts ;
RunningAverage RA_Ch00(RA_SampleSize);
//================= SETUP =========================
void setup() {
// START SERIAL FOR DEBUGGING
Serial.begin(9600);
Serial.println( "Starting TempLogger Application !!");
pinMode(scan_LED, OUTPUT);
// START THE LCD INTERFACE
lcd.begin(16, 2);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
lcd.clear();
lcd.print(F("TempratureLogger" ));
lcd.setCursor(0, 1);
lcd.print(F(" Version :DLite " ));
delay (1000);
lcd.clear();
EEPROM.get(epromAddress, loggingInterval);
if ( loggingInterval > 600 || loggingInterval < 1) {
loggingInterval = 1;
EEPROM.put(epromAddress, loggingInterval);
}
lcd.print(F("Logging Interval" ));
lcd.setCursor(0, 1);
char logInterval[17];
sprintf( logInterval, " %03u %s", loggingInterval, "second");
lcd.print(logInterval);
delay (1000);
lcd.clear();
// START THE RTC INTERFACE
if (! rtc.begin()) {
lcd.print(F("Couldn't find RTC"));
while (1);
}
if (! rtc.initialized()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set the RTC to the date & time this sketch was compiled
}
// CHECK THE PRESENCE OF SD CARD
lcd.print(F("Checking SDcard"));
lcd.setCursor(0, 1);
lcd.print(F("Interface.. Wait"));
delay (1000);
lcd.clear();
if (!SD.begin(SDCselect)) { // See if the card can be initialized:
Serial.print(F("SD Card Fail !!"));
}
else {
lcd.print(F(" SDCard Pass!!"));
}
}
//================ SCAN LOOP ======================
void loop()
{
//if (homeDispGate || waitOnSerGate) readSerial(); // Do not put this inside any timed loop...
acquireTempVal(); // Read the Temperature
if (scanMs - millis() > loopInterval) // Loop interval = 100ms now.
{
scanMs = millis();
validDIN = MyDIN.getKey(); // Read the Push buttons for every scan..
displayHome();
// readBatVolt();
// if (measuredvbat < 3.5 ) {
// digitalWrite(lowBatt_LED, HIGH);
// }
// else {
// digitalWrite(lowBatt_LED, LOW);
// }
// Serial.print("Battery voltage is ");
// Serial.println(measuredvbat);
//checkKeys(); // Enable to check key wiring...to use uncomment this line & function also
// startLogging(); // Handle all SD card routines and logging here
} // Scan end
} // loop end
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void displayHome()
{
DateTime now = rtc.now(); // Get the current date time details ..
if ( now.second() != prevSecond )
{
prevSecond = now.second();
lcd.setCursor(0, 0); // Print to LCD
sprintf(clockStringNow, " %02d-%02d %02d:%02d:%02d ", now.day(), now.month(), now.hour(), now.minute(), now.second());
lcd.print(clockStringNow);
dtostrf(tempDegC, 5, 2, tBuffer);
lcd.setCursor(0, 1);
sprintf(clockStringNow, " Deg C = %s ", tBuffer);
lcd.print(clockStringNow);
}
}
//*********************************************
// Function to Acquire the analog value and average it..
void acquireTempVal()
{
battVolts = 3.3;
Val_00 = analogRead(lm35Pin);
RA_Ch00.addValue(Val_00);
analog_Ch0Val = RA_Ch00.getAverage();
tempDegC = 100 * (mapf(RA_Ch00.getAverage(), 0, 1023, 0, battVolts));
}
//************************************************
// Function to map the analog count to required voltage level.
float mapf(long x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//************************************************