I need to run my Nano for about 24 hours collecting temperature readings and then to stop.
My understanding of void setup and void loop has been that setup runs only once while loop runs infinitely.
So I placed my code in void setup and left void loop blank. But during testing, I noticed that the code is running repeatedly and not just once as I had thought.
Is my understanding above incorrect or have I done something different in my program?
Please share suggestions on the recommended way to run my code only once. One method would be to put a ‘while 1’ condition inside void loop. That sounds inefficient. Is there any better method?
code below:
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Start up the Dallas temp sensor library
sensors.begin();
// number of readings in total (one evry 10 mins, for a total of 24 h = 60/10 x 24 = 144)
const int ARRAY_SIZE = 144;
const int STARTING_EEPROM_ADDRESS = 0;
int newNumbers[ARRAY_SIZE];
// readIntArrayFromEEPROM(STARTING_EEPROM_ADDRESS, newNumbers, ARRAY_SIZE);
for (int j=0; j< ARRAY_SIZE*2; j+=2) {
// section to disp all EEPROM readings
const int STARTING_EEPROM_ADDRESS = 0;
readIntArrayFromEEPROM(STARTING_EEPROM_ADDRESS, newNumbers, ARRAY_SIZE);
Serial.print("###Iteration:");
Serial.print(j/2 + 1 );
Serial.print(" of ");
Serial.println(ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
{
Serial.println(newNumbers[i]);
}
delay (60000);
sensors.requestTemperatures();
int reading = (int) sensors.getTempCByIndex(0) ;
//int reading = (int) 49.3;
writeIntIntoEEPROM(j, reading);
}
}
void loop () {
}
void writeIntIntoEEPROM(int address, int number)
{
byte byte1 = number >> 8;
byte byte2 = number & 0xFF;
EEPROM.write(address, byte1);
EEPROM.write(address + 1, byte2);
}
void readIntArrayFromEEPROM(int address, int numbers[], int arraySize)
{
int addressIndex = address;
for (int i = 0; i < arraySize; i++)
{
numbers[i] = (EEPROM.read(addressIndex) << 8) + EEPROM.read(addressIndex + 1);
addressIndex += 2;
}
}