Hey, I've been trying to get a simple counter set up, but for some reason it doesn't really do anything.
void loop() {
char cmd; // Command input by user
int humidVal; // Humidity value read from SHT1x
int tempVal; // Temperature value from SHT1x
int sensorNum; // Sensor number
float fHumidity; // Working value for humidity calculation
float fTemperature; // Working value for temperature calculation
unsigned char error; // Return value for routine calls
unsigned int port;
int counter;
while (Serial.available () > 0) { // When a serial connection exists
cmd = Serial.read (); // Read user comand
error = 0;
if (cmd == TIME_HEADER) { // If the command starts with "T"
processSyncMessage(); // Set the time
}
if (timeStatus() != timeNotSet) { // If the time is set
digitalClockDisplay(); // Display the new time
}
else {
setTime(1420070400);
}
if (cmd == SENSOR_CALL) { // if the command starts with "S"
sensorNum = Serial.parseInt(); // Set sensorNum as input
port = sensorNum;
if (port < 2) { // If port 2 was called
Serial.println ("Unable to access serial communication port!\n");
break; // Exit this statement
}
if (port == CLOCK) { // If the clock port was called
Serial.println ("Unable to access SHT10 CLOCK port!\n");
break; // Exit this statement
}
// Read request - read in temperature and humidity
Serial.print ("Accessing port ");
Serial.println (port); // Print the port being accessed
error += // Increment the error value
sMeasure ((unsigned char *) &humidVal, MEASURE_HUMI, 0, port);
error +=
sMeasure ((unsigned char *) &tempVal, MEASURE_TEMP, 0, port);
if (error)
sConnectionReset (port);
else {
digitalClockDisplay();
fHumidity = float (humidVal);
fTemperature = float (tempVal);
calcTempHumid (&fHumidity, &fTemperature);
printReading ("Reading: ", &fTemperature, "*C");
// String test1 = printReading ("Reading: ", &fTemperature, "*C");
printReading (" at ", &fHumidity, "% Humidity");
//dataFileA = SD.open("data_A.txt");
//if (dataFileA) { //if the file is open
// dataFileA.println(printReading("Reading: ", &fTemperature, "*C")); //write the output string for T/C1 to the card
// dataFileA.println(printReading(" at ", &fHumidity, "% Humidity")); //write the output string for T/C2 to the card
// dataFileA.close(); //close the dataFile
//Serial.print(output1String); // print to the serial port too
}
Serial.println();
}
Serial.println ();
break;
}
if (second() == 40 || second() == 10) { // Auto reading every 30 seconds
counter++; //COUNTER DOESN"T WORK!
if (timeStatus() != timeNotSet) { // If the time is set
digitalClockDisplay(); // Display the new time
}
Serial.println(counter);
for (int xy = 5; xy < 7; xy++){ // For loop to go through all the sensor ports. Change this upper value for increased number of sensors
port = xy; // Set the port to the current value of the For loop
Serial.print ("Port ");
Serial.println (port); // Print the port being accessed
error += // Increment the error value
sMeasure ((unsigned char *) &humidVal, MEASURE_HUMI, 0, port);
error +=
sMeasure ((unsigned char *) &tempVal, MEASURE_TEMP, 0, port);
if (error) {
Serial.println();
sConnectionReset (port);
break;
}
else {
fHumidity = float (humidVal);
fTemperature = float (tempVal);
calcTempHumid (&fHumidity, &fTemperature);
printReading ("Reading: ", &fTemperature, "*C");
printReading (" at ", &fHumidity, "% Humidity");
Serial.println();
delay(200);
}
Serial.println();
}
}
}
Roughly 2/3s of the way down is where I have it currently, and it'll count to 1, but keep spitting out 1 every time. I want it to count every time the 40 or 10 second if statement is run.
I tried having it at the end of the code, after the for loop but still inside the if statement, but it just returns a 0 every time. Any ideas of what's going on? I mean, something like this shouldn't be so complicated, but I cannot seem to figure it out for some reason.