I am working on a solar tracking project for school.
I am using four infrared sensors to the detect position of the sun.
In my code, I need to recheck these sensor values every second in order to turn off the linear actuators once the sensors decide to move the solar panels.
I am also using the arduino to record the current and voltage of the solar panels to an SD card. However, I do not want to record the current and voltage data every second which is how often my loop will run.
I used a counter variable and initialized it to 0. Everytime the loop runs I increment counter. I setup an (if counter==60) statement to record the current and voltage data to the SD card to make it so the data is recorded every minute.
My problem is that I cannot reset counter to 0 after recording the data. It keeps incrementing past 60 because inside my IF statement, having counter=0 doesnt affect the counter variable outside the block. How can I reset my counter variable to 0 everytime my current voltage data is recorded?
I know that I can set it so that the if statement will check if the counter is a multiple of 60, which will make it so I dont need to reset the counter variable, but I feel like that is bad coding practice since if I wanted to record data over a long period of time the counter variable would get very large. There must be an easier way to do this.
int Counter= 0;
if (Counter == 60)
{
// Open a file to write header to
File myFile = SD.open("test.csv", FILE_WRITE);
if (myFile)
{
myFile.println(dataString);
myFile.close();
Serial.println(dataString);
Serial.println("Sensor Data Recorded");
int Counter=0;
}
else
{
Serial.println("Couldn't open file");
}
}
Counter++;
As you are discovering, it is generally a bad idea to have local variables with the same names as global variables. This approach causes no end of confusion, especially if you come back to this code a year later and try to remember what you did.
You do realize, I hope, that the "int Counter =0;" statement below defines a new local variable within the if block:
Serial.println("Sensor Data Recorded");
int Counter=0;
Alright, I have figured it out. Posting my solution here in case anyone searches this problem.
What I did was used a command feature to jump out of the if statement block in order to set the counter variable to zero in a seperate routine. Similar to an interrupt feature, this routine will only execute when it is called and run once. To jump to this seperate routine, simply call the name of the routine as shown below.
if (Counter == 5)
{
// Open a file to write header to
File myFile = SD.open("test1.csv", FILE_WRITE);
if (myFile)
{
myFile.println(dataString);
myFile.close();
Serial.println(dataString);
Serial.println("Sensor Data Recorded");
CounterReset();
}
else
{
Serial.println("Couldn't open file");
}
}
// Seperate routine for resetting counter variables when IV data is recorded to SD card
void CounterReset()
{
// Reset counter variable when data is recorded
Counter=0;
}
Bad solution. The function call can simply be replaced by
Counter = 0;
and the function itself eliminated.
That is what I was trying to do initially with my int Counter=0; I was trying to reset Counter to 0, not define a new variabe.
Didn't know that having int was the problem. Thanks a lot.
Arfreezy:
That is what I was trying to do initially with my int Counter=0; I was trying to reset Counter to 0, not define a new variabe.
Didn't know that having int was the problem. Thanks a lot.
When you prefix a variable with the type, the C compiler determines the locale that the variable is within ... Global or private. Since you defined your variable within an if{;;} construct, the compiler created a 2nd Counter variable within that scope. Had you omitted the int type declaration, the Global variable would have been used.
Continue reading here: Variable Scope in C++
Ray