Array in PROGMEM, appel via une variable

Bonjour,
Dans un projet pour allumé et éteindre un lampe en faisant varier la photopériode, j'ai un tableau 2D avec 367 entrées (jours de l'année) (et pour chaque entrées 3 paramètres (heure allumage, heure éteinction, durée de la photopériode)
Comme le tableau est grand je l'ai mis dans la memoire Flash via PROGMEM.
J'ai une fonction qui me calcule le numéro du jours par rapport au 1er janvier (DN)

Je souhaite appeler l'heure d'allumage via cette fonction.
Voici le code que j'utilise mais il n'est pas fini c'ets mon brouillons mais pour le moment je suis sur ce point.

#include <Wire.h>
#include <RTClib.h>
#include <DHT22.h>
#include <LiquidCrystal_I2C.h>
#include <avr/pgmspace.h>

RTC_DS1307 RTC;


prog_int16_t  Photoperiode[367][3] PROGMEM ={
{810,1810,1000},
{810,1810,1000},
{551,2158,1607},
{551,2159,1608},
{550,2159,1609},
{550,2200,1610},
{549,2201,1612},
{549,2201,1612},
{549,2202,1613},
{549,2203,1614},
{549,2203,1614},
{549,2204,1615},
{549,2204,1615},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2206,1617},
{549,2206,1617},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2204,1615},
{549,2204,1615},
{549,2203,1614},
{549,2203,1614},
{549,2202,1613},
{549,2201,1612},
{549,2201,1612},
{550,2200,1610},
{550,2159,1609},
{551,2159,1608},
{551,2158,1607},
{551,2157,1606},
{552,2156,1604},
{553,2155,1602},
{553,2154,1601},
{554,2153,1559},
{555,2152,1557},
{555,2151,1556},
{556,2150,1554},
{557,2149,1552},
{558,2148,1550},
{559,2147,1548},
{600,2146,1546},
{601,2144,1543},
{602,2143,1541},
{603,2142,1539},
{604,2141,1537},
{605,2139,1534},
{606,2138,1532},
{607,2137,1530},
{609,2135,1526},
{610,2134,1524},
{611,2133,1522},
{613,2131,1518},
{614,2130,1516},
{615,2129,1514},
{617,2127,1510},
{618,2126,1508},
{620,2124,1504},
{621,2123,1502},
{623,2121,1458},
{625,2120,1455},
{626,2118,1452},
{628,2117,1449},
{630,2115,1445},
{631,2114,1443},
{633,2112,1439},
{635,2111,1436},
{636,2109,1433},
{638,2108,1430},
{640,2106,1426},
{642,2105,1423},
{644,2103,1419},
{646,2102,1416},
{647,2100,1413},
{649,2059,1410},
{651,2057,1406},
{653,2056,1403},
{655,2054,1359},
{657,2053,1356},
{659,2051,1352},
{701,2050,1349},
{703,2048,1345},
{705,2047,1342},
{707,2045,1338},
{709,2044,1335},
{711,2042,1331},
{713,2041,1328},
{715,2039,1324},
{717,2038,1321},
{719,2036,1317},
{722,2034,1312},
{724,2033,1309},
{726,2031,1305},
{728,2030,1302},
{730,2028,1258},
{732,2027,1255},
{634,1925,1251},
{636,1924,1248},
{638,1922,1244},
{641,1921,1240},
{643,1919,1236},
{645,1918,1233},
{647,1916,1229},
{649,1915,1226},
{651,1913,1222},
{653,1911,1218},
{656,1910,1214},
{658,1908,1210},
{700,1907,1207},
{702,1905,1203},
{704,1904,1200},
{706,1902,1156},
{708,1901,1153},
{711,1859,1148},
{713,1857,1144},
{715,1856,1141},
{717,1854,1137},
{719,1853,1134},
{721,1851,1130},
{723,1850,1127},
{725,1848,1123},
{727,1846,1119},
{729,1845,1116},
{731,1843,1112}
}; ////////Tableau tronqué
int HeureStart =1830;
int HeureStop =900;

// Variable
int DN;                             //Returns the number of day in the year
int WN; 

// DHT22
// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 7
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);

///////////////////////////////////////////// SETUP ////////////////////////////////
void setup()
{   Serial.begin(9600);
    
    Wire.begin();
    RTC.begin();

}
///////////////////////////////////////////// LOOP ////////////////////////////////
void loop()
{

DateTime now =RTC.now();// lecture de la date sur le DS1307
day=now.day();
month=now.month();
year=now.year();
hour=now.hour();
minute=now.minute();
dayOfWeek=now.dayOfWeek();
ActualTime=(hour * 100) + minute;   
DayWeekNumber(now.year(),now.month(),now.day(),now.dayOfWeek()); // calcule de DN et DW nb jours de l'annee

  Serial.print("Day #");
  Serial.print(DN);
  Serial.print(" at week # ");
  Serial.print(WN);
  Serial.print(" of ");
  Serial.println(year);


ClimateData();      // mesure de la Temperature
StockageClimate ();
displayData();
  Serial.print("Days ");
  Serial.println(DN);
  Serial.print("Current Time : ");
  Serial.println(ActualTime);
  Serial.print("Switch on Time (i): ");
  int i =  118;
  Serial.println(Photoperiode[i][0]);
  Serial.print("Switch on Time (DN): ");
  Serial.println(Photoperiode[DN][0]);
  Serial.print("Switch off Time : ");
  Serial.println(Photoperiode[118][1]);
  delay(2000);
  Serial.println("##########################");
}


void DayWeekNumber(unsigned int y, unsigned int m, unsigned int d, unsigned int w)
{
  int days[]={0,31,59,90,120,151,181,212,243,273,304,334};    // Number of days at the beginning of the month in a not leap year.
        //Start to calculate the number of day
  if (m==1 || m==2)
    {
        DN = days[(m-1)]+d;			   //for any type of year, it calculate the number of days for January or february
    }
        // Now, try to calculate for the other months
  else if ((y % 4 == 0 && y % 100 != 0) ||  y % 400 == 0)
    {  //those are the conditions to have a leap year
        DN = days[(m-1)]+d+1;        // if leap year, calculate in the same way but increasing one day
    }
  else
    {
        DN = days[(m-1)]+d;          //if not a leap year, calculate in the normal way, such as January or February
    }
        // Now start to calculate Week number
  if (w==0)
    {
        WN = (DN-7+10)/7;  	        //if it is sunday (time library returns 0)
    }
  else
      {
        WN = (DN-w+10)/7;	        // for the other days of week
      }
}

///////////////////////////////////////////////////

Ca sort:

Days 118
Current Time : 1409
Switch on Time (i): 636
Switch on Time (DN): 273
Switch off Time : 2109
##########################

Voila le probleme: lorsque j'utilise DN la valeurs n'est pas la bonne
Donc je suppose que la variable DN pose problème,
puisque i passe bien
selon mon upload et les modifications que je réalise le Switch on Time (DN) n'est pas toujours le même
Pouvez vous m'expliquer ou ça bloque ?
Le but final est de comparer ActualTime et Photoperiode[DN][0] / Photoperiode[DN][1] pour allumer ou etiendre
Merci de vos retour.
Damien

Peux tu mettre toute la trame de sortie entre deux

##########################

?

Bonjour,

C'est normal tu ne peux pas récupérer la valeur comme ça. C'est décrit en français ici :
http://www.mon-club-elec.fr/pmwiki_reference_arduino/pmwiki.php?n=Main.PROGMEM

Merci de vos reponse rapide.

John_ lenfr :
la trame de sortie est toujours la meme (enfin si j'ai bien compris de ce qu'est la trame de sortie)

######################
Days 118
Current Time : 1409
Switch on Time (i): 636
Switch on Time (DN): 273
Switch off Time : 2109
#######################

B@tto
Jee viens de lire le doc. Je vois bien effectivement que lacces est plus complique MAIS pourquoi je sort bien la valeur avec i=118 et pas avec DN issue de la fonction et qui vaut 118 ???

Merci

Damien

Bonjour,
J'ai éditer mon code pour lire ce qui est stocké dans la Flash:

prog_int16_t  Photoperiode[367][3] PROGMEM ={
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{808,1812,1004},
{806,1814,1008},
{804,1815,1011},
{803,1817,1014},
{801,1819,1018},
{759,1820,1021},
{757,1822,1025},
{755,1824,1029},
{753,1825,1032},
{751,1827,1036},
{749,1829,1040},
{747,1830,1043},
{746,1832,1046},
{744,1833,1049},
{742,1835,1053},
{740,1837,1057},
{738,1838,1100},
{736,1840,1104},
{734,1842,1108},
{731,1843,1112},
{729,1845,1116},
{727,1846,1119},
{725,1848,1123},
{723,1850,1127},
{721,1851,1130},
{719,1853,1134},
{717,1854,1137},
{715,1856,1141},
{713,1857,1144},
{711,1859,1148},
{708,1901,1153},
{706,1902,1156},
{704,1904,1200},
{702,1905,1203},
{700,1907,1207},
{658,1908,1210},
{656,1910,1214},
{653,1911,1218},
{651,1913,1222},
{649,1915,1226},
{647,1916,1229},
{645,1918,1233},
{643,1919,1236},
{641,1921,1240},
{638,1922,1244},
{636,1924,1248},
{634,1925,1251},
{732,2027,1255},
{730,2028,1258},
{728,2030,1302},
{726,2031,1305},
{724,2033,1309},
{722,2034,1312},
{719,2036,1317},
{717,2038,1321},
{715,2039,1324},
{713,2041,1328},
{711,2042,1331},
{709,2044,1335},
{707,2045,1338},
{705,2047,1342},
{703,2048,1345},
{701,2050,1349},
{659,2051,1352},
{657,2053,1356},
{655,2054,1359},
{653,2056,1403},
{651,2057,1406},
{649,2059,1410},
{647,2100,1413},
{646,2102,1416},
{644,2103,1419},
{642,2105,1423},
{640,2106,1426},
{638,2108,1430},
{636,2109,1433},
{635,2111,1436},
{633,2112,1439},
{631,2114,1443},
{630,2115,1445},
{628,2117,1449},
{626,2118,1452},
{625,2120,1455},
{623,2121,1458},
{621,2123,1502},
{620,2124,1504},
{618,2126,1508},
{617,2127,1510},
{615,2129,1514},
{614,2130,1516},
{613,2131,1518},
{611,2133,1522},
{610,2134,1524},
{609,2135,1526},
{607,2137,1530},
{606,2138,1532},
{605,2139,1534},
{604,2141,1537},
{603,2142,1539},
{602,2143,1541},
{601,2144,1543},
{600,2146,1546},
{559,2147,1548},
{558,2148,1550},
{557,2149,1552},
{556,2150,1554},
{555,2151,1556},
{555,2152,1557},
{554,2153,1559},
{553,2154,1601},
{553,2155,1602},
{552,2156,1604},
{551,2157,1606},
{551,2158,1607},
{551,2159,1608},
{550,2159,1609},
{550,2200,1610},
{549,2201,1612},
{549,2201,1612},
{549,2202,1613},
{549,2203,1614},
{549,2203,1614},
{549,2204,1615},
{549,2204,1615},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2206,1617},
{549,2206,1617},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2205,1616},
{549,2204,1615},
{549,2204,1615},
{549,2203,1614},
{549,2203,1614},
{549,2202,1613},
{549,2201,1612},
{549,2201,1612},
{550,2200,1610},
{550,2159,1609},
{551,2159,1608},
{551,2158,1607},
{551,2157,1606},
{552,2156,1604},
{553,2155,1602},
{553,2154,1601},
{554,2153,1559},
{555,2152,1557},
{555,2151,1556},
{556,2150,1554},
{557,2149,1552},
{558,2148,1550},
{559,2147,1548},
{600,2146,1546},
{601,2144,1543},
{602,2143,1541},
{603,2142,1539},
{604,2141,1537},
{605,2139,1534},
{606,2138,1532},
{607,2137,1530},
{609,2135,1526},
{610,2134,1524},
{611,2133,1522},
{613,2131,1518},
{614,2130,1516},
{615,2129,1514},
{617,2127,1510},
{618,2126,1508},
{620,2124,1504},
{621,2123,1502},
{623,2121,1458},
{625,2120,1455},
{626,2118,1452},
{628,2117,1449},
{630,2115,1445},
{631,2114,1443},
{633,2112,1439},
{635,2111,1436},
{636,2109,1433},
{638,2108,1430},
{640,2106,1426},
{642,2105,1423},
{644,2103,1419},
{646,2102,1416},
{647,2100,1413},
{649,2059,1410},
{651,2057,1406},
{653,2056,1403},
{655,2054,1359},
{657,2053,1356},
{659,2051,1352},
{701,2050,1349},
{703,2048,1345},
{705,2047,1342},
{707,2045,1338},
{709,2044,1335},
{711,2042,1331},
{713,2041,1328},
{715,2039,1324},
{717,2038,1321},
{719,2036,1317},
{722,2034,1312},
{724,2033,1309},
{726,2031,1305},
{728,2030,1302},
{730,2028,1258},
{732,2027,1255},
{634,1925,1251},
{636,1924,1248},
{638,1922,1244},
{641,1921,1240},
{643,1919,1236},
{645,1918,1233},
{647,1916,1229},
{649,1915,1226},
{651,1913,1222},
{653,1911,1218},
{656,1910,1214},
{658,1908,1210},
{700,1907,1207},
{702,1905,1203},
{704,1904,1200},
{706,1902,1156},
{708,1901,1153},
{711,1859,1148},
{713,1857,1144},
{715,1856,1141},
{717,1854,1137},
{719,1853,1134},
{721,1851,1130},
{723,1850,1127},
{725,1848,1123},
{727,1846,1119},
{729,1845,1116},
{731,1843,1112},
{734,1842,1108},
{736,1840,1104},
{738,1838,1100},
{740,1837,1057},
{742,1835,1053},
{744,1833,1049},
{746,1832,1046},
{747,1830,1043},
{749,1829,1040},
{751,1827,1036},
{753,1825,1032},
{755,1824,1029},
{757,1822,1025},
{759,1820,1021},
{801,1819,1018},
{803,1817,1014},
{804,1815,1011},
{806,1814,1008},
{808,1812,1004},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000},
{810,1810,1000}
};

int DN;
int WN;
unsigned int displayInt; 
int k;    // counter variable
// read back a 2-byte int
 displayInt = pgm_read_word_near(Photoperiode[DN][0]);

  Serial.print("Days ");
  Serial.println(DN);
  Serial.print("Current Time : ");
  Serial.println(ActualTime);
  Serial.print("Switch on Time (i): ");
  int i =  119;
  Serial.println(Photoperiode[i][0]);
  Serial.print("Switch on Time (DN): ");
  Serial.println(Photoperiode[DN][0]);
  Serial.print("Switch on Time pgm-read : ");
  Serial.println(displayInt);
  Serial.print("Switch off Time : ");
  Serial.println(Photoperiode[119][1]);
  Serial.println("##########################");
  delay(2000);


// fonction numéro du jours ou semaine
void DayWeekNumber(unsigned int y, unsigned int m, unsigned int d, unsigned int w)
{
  int days[]={0,31,59,90,120,151,181,212,243,273,304,334};    // Number of days at the beginning of the month in a not leap year.
        //Start to calculate the number of day
  if (m==1 || m==2)
    {
        DN = days[(m-1)]+d;			   //for any type of year, it calculate the number of days for January or february
    }
        // Now, try to calculate for the other months
  else if ((y % 4 == 0 && y % 100 != 0) ||  y % 400 == 0)
    {  //those are the conditions to have a leap year
        DN = days[(m-1)]+d+1;        // if leap year, calculate in the same way but increasing one day
    }
  else
    {
        DN = days[(m-1)]+d;          //if not a leap year, calculate in the normal way, such as January or February
    }
        // Now start to calculate Week number
  if (w==0)
    {
        WN = (DN-7+10)/7;  	        //if it is sunday (time library returns 0)
    }
  else
      {
        WN = (DN-w+10)/7;	        // for the other days of week
      }
}

Et ca donne:

##########################
Days 119
Current Time : 1841
Switch on Time (i): 635
Switch on Time (DN): 1536
Switch on Time pgm-read : 1349
Switch off Time : 2111
##########################

Ca ne correspond toujours pas.
C'est bizzare que lorque j'utilise Photoperiode[i][0]ou directement Photoperiode[119][0] , j'obtiens la bonne valeur mais pas avec DN (ni prgm_read)....

ca fonctionne j'avais oublier le &

pgm_read_word(&Photoperiode[DN][1]);