Hi,
I am sure this is just a bug in my code and not an actual issue with the board, but I cannot see the mistake. I am using a MEGA2560 to control an array of 20 air intake solenoid valves and two output solenoid valves. Each intake valve should open for 5 minutes (with the next value in the series simultaneously opening to prime the following sample). The airflow from the solenoids goes into an sampling instrument hooked to a datalogger, which records the sample value at the end of each 5 minute period.
My issue is that the Arduino controller ends up being out of sync with the datalogger. After each loop (totaling 100 minutes) the controller is ~13 seconds behind the datalogger (compounding with each successive loop). It looks like this occurs during the last cycle of the solenoid array within the loop (activation/deactivation of pin 32).
Can anyone see whether this is a bug in my code? Any suggestions on how to fix would be much appreciated.
/*
Control of intake solenoids and output solenoids
solPins: intake solenoids
soloPins: output solenoids
*/
int solPins[] = {
53, 41, 49, 43, 47, 39, 51, 35, 45, 37, 52, 24, 50, 26, 48, 28, 46, 30, 44, 32}; // an array of pin numbers to which intake solenoids are attached
int pinCount = 20; // the number of pins (array length)
void setup() {
//initialize intake solenoids
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(solPins[thisPin], OUTPUT);
}
//initialize output solenoids
for (int soloPin = 12; soloPin < 14; soloPin++) {
pinMode(soloPin, OUTPUT);
}
}
void loop() { //loop through the solenoid array, switch back and forth between the two output solenoids
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
if(solPins[thisPin] > 43) { //pins >43 activate output solenoid #1, deactivate output solenoid #2
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
}
else { //pins <43 activate output solenoid #2, deactivate output solenoid #1
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
}
digitalWrite(solPins[thisPin], HIGH);
digitalWrite(solPins[thisPin + 1], HIGH);
delay(300000); //5 min = 300000
digitalWrite(solPins[thisPin], LOW);
digitalWrite(solPins[thisPin + 1], LOW);
}
}