Thanks every one for you (SUPERFAST !!!) replies. I'll look into your answers and try to find the one that is most appropriate for me... (Although more solutions are always welcome
)
But first this :
I've read everywhere that an ISR should be as short as possible. But I have to say that (in another arduino project that controls a solar panel) I use a SUPER LONG ISR, and it works like a charm...
in this ISR :
- I call several subroutines... (e.g. time reading from RTC and getting data over RS232)
- I do 20 Analog readings...
- I do some quite complex Floating Point calculations...
- I write to a TFT display...
- I even save data on a SD card while in this ISR...
- etc...
but I do NOT use an I2C (chain)...
and I just see that I trespass the 9000 character limit of this forum
(although MSWord only counts 8.825 characters ... ) so I'll reply with the second part in a 2nd reply ...
Am I just lucky ??
here's the code from this ISR : (I agree that this is not the most perfect code, but it works...)
////////////////////////////////////////////////////
//READ INPUTS - Interrupt Service Routine//
void readinputs()
{
samplecounter ++;
SD.remove("samples.txt");
SAMPLES = SD.open("samples.txt", FILE_WRITE);
SAMPLES.println(ltoa(samplecounter, sampleBuffer, 10));
SAMPLES.close();
myGLCD.setColor(0, 255, 0); // green square bottom right ON
myGLCD.fillRect(225, 305, 239, 319);
readTracer();
// Analog readings
//////////////////
Isolar = 0;
Icharger = 0;
for (n = 0; n < 10; n++)
{
Isolar = Isolar + analogRead(A7) - 512; // IchargeSolar (0-10A)
}
Isolar = Isolar / 10;
mapIsolar = map( Isolar * 2, 0, 1023, 0, 1000) / 40.0; // SOLAR PANEL CURRENT(0-10.0A)
Serial.println();
Serial.println(mapIsolar);
Serial.println();
if (mapIsolar < 0) mapIsolar = 0;
for (n = 0; n < 10; n++)
{
Icharger = Icharger + analogRead(A8) - 512; // Icharge (0-10A)
}
Icharger = Icharger / 10;
mapIcharger = map( Icharger * 2, 0, 1023, 0, 1000) / 40.0; // SOLAR PANEL CURRENT(0-10.0A)
if (mapIcharger < 0) mapIcharger = 0;
Serial.println();
Serial.println(mapIcharger);
Serial.println();
/*
if (ACcharging == 1)
{
mapIcharger = map(Icharger * 2, 0, 1023, -1000, 1000) / 40.0; // * stepdowncurrentadj; // STEPDOWN CURRENT (0-10.0A)
if (mapIcharger < 0.05)mapIcharger = 0;
}
else mapIcharger = 0;
*/
//overvoltage protection
////////////////////////
if (mapUbat > maxbatvolt)
{
chargerstopdelay ++;
if (chargerstopdelay > 5)
{
digitalWrite(11, LOW);
ACcharging = 0;
chargetime = 0;
chargerstopdelay = 0;
}
}
//KWH update
////////////
KWH_solar = KWH_solar + (mapUpanel * mapIsolar / 3600000);
KWH_net = KWH_net + (0 * mapIcharger);
KWH_out = KWH_out + (mapUbat * mapIbat);
readtime();
// if (MINute % 6 == 0) {}
if (SECond % 10 == 0)
{
myGLCD.fillRect(225, 305, 239, 319);
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(225, 305, 239, 319);
savecounter ++;
logwrite();
}
// CHARGER start/stop
/////////////////////
if (mapUbat < chargerstart)
{
chargetime = 0;
chargerstartdelay ++;
if (chargerstartdelay > 60)
{
digitalWrite(11, HIGH); // start charger
ACcharging = 1;
chargerstartdelay = 0;
}
}
if (ACcharging == 1)
{
if (chargetime == 1) ACcount++;
if (chargetime > chargeTimeSet)
{
digitalWrite(11, LOW); // stop charger
chargetime = 0;
ACcharging = 0;
chargerstartdelay = 0;
}
chargetime++;
}
// END CHARGER start/stop
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(225, 305, 239, 319);
displayTime();
switch (menu)
{
case 0:
myGLCD.printNumI(samplecounter, 0, 295, 10);
displayTime();
break;
case 1:
OVERVIEW_DATA();
displayTime();
break;
case 2:
printdatamenu2();
displayTime();
break;
case 3:
displayTime();
KWHdata();
break;
case 4:
displayTime();
break;
case 5:
displayTime();
break;
case 6:
displayTime();
break;
case 7:
displayTime();
break;
case 8:
myGLCD.setColor(255, 255, 0);
myGLCD.printNumI(samplecounter, 160, 192, 9);
myGLCD.printNumI(savecounter, 160, 204, 9);
if (samplecounter > 999) myGLCD.drawPixel(207, 202);
if (samplecounter > 999999) myGLCD.drawPixel(182, 202);
if (savecounter > 999) myGLCD.drawPixel(207, 214);
if (savecounter > 999999) myGLCD.drawPixel(182, 214);
break;
case 9:
displayTime();
break;
case 10:
currentadjustmentsdata();
break;
case 11:
displayTime();
break;
case 12:
displayTime();
break;
case 13:
myGLCD.setColor(255, 255, 255);
myGLCD.printNumI(samplecounter, 0, 0);
myGLCD.printNumF(mapIbat, 2, 188, 134, '.', 5, ' '); // charge current
myGLCD.setColor(255, 255, 0);
myGLCD.printNumF(mapUbat, 1, 40, 270, '.', 4, ' '); // battery voltage
myGLCD.setColor(0, 255, 50);
myGLCD.printNumF(mapIsolar , 2, 88, 122, '.', 5, ' '); // panel current
myGLCD.setColor(255, 0, 50);
myGLCD.printNumF(mapIcharger, 2, 88, 134, '.', 5, ' '); // charge current
myGLCD.setColor(255, 100, 0);
myGLCD.printNumI(chargetime, 88, 270, 4); // charger ON time
myGLCD.setColor(0, 255, 0);
myGLCD.printNumF(mapUpanel, 1, 56, 280, '.', 4, ' ');
// waitingtime (secs) before next sample
myGLCD.setColor(255, 255, 255);
myGLCD.printNumI((((samplecounter / interval) + 1) * interval) - samplecounter, 207, 271, 4);
if (samplecounter % interval == 0)
{
if (ACcharging == 1)
{
myGLCD.setColor(255, 100, 0);
myGLCD.drawLine(xx - 1, 260, xx - 1, 256);
myGLCD.fillCircle(5, 302, 5);
myGLCD.setColor(255, 255, 255);
}
if (ACcharging == 0)
{
myGLCD.setColor(255, 100, 0);
myGLCD.drawLine(xx - 1, 260, xx - 1, 261);
myGLCD.setColor(0, 0, 0);
myGLCD.fillCircle(5, 302, 5);
myGLCD.setColor(255, 255, 255);
}
---> continues on next reply ... 