No connection to Wifi if my code contains some particular pgm_read_word_near commands

Hello,

My code contains PROGMEM arrays that tell my servo motors which position to take every 0.05 seconds (with a loop). I have a particular file which allows to store these arrays. These arrays are called "arrayH2 ", "arrayV2 " etc. My servos are called “monServoH”, “monServoV”, etc.
One array = one servo.

Below is the content of my file :

// Reine de la Nuit


void Presentation() {
    Serial.println("Je suis la reine de la nuit");
    detachAll();

    arrayHi = pgm_read_word_near(arrayH2 + 1);
    arrayVi = pgm_read_word_near(arrayV2 + 1);
    arrayRi = pgm_read_word_near(arrayR2 + 1);
    arrayXi = pgm_read_word_near(arrayX2 + 1);
    arrayYi = pgm_read_word_near(arrayY2 + 1);
    **arrayLidsi = pgm_read_word_near(arrayLids2 + i);**
    attachAll();
    sweepH(arrayHi,50);
    sweepV(arrayVi,50);
    sweepR(50,20);
    sweepX(arrayXi,10);
    sweepY(arrayYi,10);
    sweepLids(minLids,2);
    monServoLids.write(minLids);
    sweepJaw(minJaw,10);
    detachAll();
    

    myMP3.stop();
    delay(100);

    if (x == 1) {
        myMP3.volume(30);
        myMP3.play(4);
    }
    
    int b = analogRead(pinBusy);
    int z = 1;
    while (z == 1) {
        delay(1000);
        b = analogRead(pinBusy);
        Serial.print("b=");
        Serial.println(b);
        if (b > 500) { delay(1); }
        if (b < 500) { z = 2; }
    }

    int i = 14;
    
    
    if (b < 500) {
        attachAll();

        unsigned long currentMillis = millis();
        unsigned long  previousMillis = millis();
        Serial.println("Debut");
        while (i < L2) {
        
            currentMillis = millis();
       
        
            arrayHi = pgm_read_word_near(arrayH2 + i);
            arrayVi = pgm_read_word_near(arrayV2 + i);
            arrayRi = pgm_read_word_near(arrayR2 + i);
            arrayXi = pgm_read_word_near(arrayX2 + i);
            arrayYi = pgm_read_word_near(arrayY2 + i);
            **arrayLidsi = pgm_read_word_near(arrayLids2 + i);**
            arrayJawi = pgm_read_word_near(arrayJaw2 + i);


            monServoH.write(arrayHi);
            monServoV.write(arrayVi);
            monServoR.write(arrayRi);
            monServoX.write(arrayXi);
            monServoY.write(arrayYi);
            monServoLids.write(arrayLidsi);
            monServoJaw.write(arrayJawi);

            b = analogRead(pinBusy);

            if (currentMillis - previousMillis >= TIME2) {
                i = i + 1;
                previousMillis = currentMillis;
            }

            if (i >= L2-1 ) {
                detachAll();
                detach = true;
            }
        }

        detachAll();
        detach = true;
        
    }

    if (i >= L2) { // obligée de faire ça pour stopper la musique
        myMP3.pause();
        myMP3.volume(0);



    }

 

 
}

Do you see the bold lines (edit: the lines with “**” )?
This particular command:

arrayLidsi = pgm_read_word_near(arrayLids2 + i);

If I comment them, my ESP8266 will be able to connect to Wifi.
If I don’t comment them, my ESP8266 won’t be able to connect.

I really don’t understand why.
I have tried several times, and I always have the same conclusion…

This loop works very well when I don’t use Blynk. And it works perfectly for the other arrays…

Thank you in advance for your help!

Post your whole sketch please.

Hello,

My whole script contains 16 files (I found it was easier)

Below is the main file.
I added the other files here: https://github.com/nebetbastet/Robobo_wifi

My problem is in this script: https://github.com/nebetbastet/Robobo_wifi/blob/main/mode_reine.h
The arrays are here: https://github.com/nebetbastet/Robobo_wifi/blob/main/servo_array_reine.h
(I converted the arrayLids2 PROGMEM array into a classical array).

// Paramètres
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "Robobo"
#define BLYNK_AUTH_TOKEN "xxx"
char ssid[] = "xxx";
char pass[] = "xxx";
char auth[] = "xxx";

#include ESP8266_Lib.h
#include BlynkSimpleShieldEsp8266.h
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
BlynkTimer timer;



#include "CapacitiveSensor.h"
#include "Servo.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include "SR04.h"
#include "header.h"
#include "config_servos.h"
#include "config.h"
#include "smallGestures.h"
#include "servo_array_doris.h"
#include "mode_perhaps.h"
#include "servo_array_reine.h"
#include "mode_reine.h"
#include "servo_array_etoile.h"
#include "mode_etoile.h"
#include "servo_array_lacrimosa.h"
#include "mode_lacrimosa.h"
#include "servo_array_oui.h"
#include "mode_oui_non.h"
#include "mode_normal.h"


  
void setup()
{
  Setup();
 // Dodo();
  Initialize2(false); // position "neutre", yeux ouverts

  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);

  detachAll();

  //timer.setInterval(1000L, myTimerEvent);


}

// Loop
void loop() {
    Blynk.run();
    timer.run();
}


// Lacrimosa
BLYNK_WRITE(V0)
{
    if (param.asInt() == 1)
    {
      attachAll();
      ModeLacrimosa();
      detachAll();
    }

}

// Perhaps Perhaps
BLYNK_WRITE(V1)
{
    if (param.asInt() == 1)
    {
        attachAll();
        ModePerhaps();
        attachAll();
        Dodo();
        detachAll();
    }

}


// Paupières
BLYNK_WRITE(V2)
{
    monServoLids.attach(pinLids);
    int pinValue = param.asInt();
    int pos = map(pinValue, 0, 100, minLids, maxLids);
    sweepLids(pos, 10);
    monServoLids.detach();
}

// Slider vertical neck
BLYNK_WRITE(V3)
{
    monServoV.attach(pinV);
    int pinValue = param.asInt();
    int pos = map(pinValue, 0, 100, minV, maxV);
    sweepV(pos, 15);
    monServoV.detach();
}

// Slider rolling neck
BLYNK_WRITE(V4)
{
    monServoR.attach(pinR);
    int pinValue = param.asInt();
    int pos = map(pinValue, 0, 100, minR, maxR);
    sweepR(pos, 30);
    monServoR.detach();
}


// Slider horizontal neck
BLYNK_WRITE(V5)
{
    monServoH.attach(pinH);
    int pinValue = param.asInt();
    int pos = map(pinValue, 0, 100, minH, maxH);
    sweepH(pos, 15);
    monServoH.detach();
}

// Slider Eyes X
BLYNK_WRITE(V6)
{
    int posLids = monServoLids.read();
    int valueLids = map(posLids, minLids, maxLids, 0, 100);
    if (valueLids <= 35) { // On bouge l'oeil seulement si l'oeil n'est pas trop fermé
        monServoX.attach(pinX);
        int pinValue = param.asInt();
        int pos = map(pinValue, 0, 100, minX, maxX);
        sweepX(pos, 15);
    }
    monServoX.detach();
}

// Slider Eyes Y
BLYNK_WRITE(V7)
{
    int posLids = monServoLids.read();
    int valueLids = map(posLids, minLids, maxLids, 0, 100);
    if (valueLids <= 35) { // On bouge l'oeil seulement si l'oeil n'est pas trop fermé
        monServoY.attach(pinY);
        int pinValue = param.asInt();
        int pos = map(pinValue, 0, 100, minY, maxY);
        sweepY(pos, 15);
    }
    monServoX.detach();
}


// Slider Jaw
BLYNK_WRITE(V8)
{
    monServoJaw.attach(pinJaw);
    int pinValue = param.asInt();
    int pos = map(pinValue, 0, 100, minJaw, maxJaw);
    sweepJaw(pos, 15);
    monServoJaw.detach();
}

// Poème Etoile
BLYNK_WRITE(V9)
{
    if (param.asInt() == 1)
    {
        attachAll();
        PoemeEtoile();
        detachAll();
    }

}
//
//BLYNK_READ(V10) // Distance
//{
//    a = sr04.Distance();
//    Blynk.virtualWrite(V10, a); //sending to Blynk
//}

//void myTimerEvent()
//{
//    // You can send any value at any time.
//    // Please don't send more that 10 values per second.
//    Blynk.virtualWrite(V10, a);
//}

// Petit geste aléatoire
BLYNK_WRITE(V11)
{
    if (param.asInt() == 1)
    {
        attachAll();
        Initialize2(false);
        int i;
        RandomGesture2();
        detachAll();
        Initialize2(false);
    }

}

// Position neutre
BLYNK_WRITE(V12)
{
    if (param.asInt() == 1)
    {
        attachAll();
        Initialize2(false);
        detachAll();
    }

}

//// Presentation
BLYNK_WRITE(V13)
{
    if (param.asInt() == 1)
    {
        attachAll();
        Presentation();
        detachAll();
    }

}


//// Presentation
BLYNK_WRITE(V14)
{
    if (param.asInt() == 1)
    {
        attachAll();
        Initialize2(false);
        Oui();
        detachAll();
    }

}

Hello,
I still have this problem…

Memory issue maybe?

Pete.

Thank you Pete for your answer!

Well, I guess my only solution is to switch to ESP82 instead of Arduino Mega (but it will be very complicated…)

I assume that you mean ESP32?

Have you tried monitoring free memory to confirm that this is the issue?

Pete.

Yes, indeed, I wanted to write “ESP32” :slight_smile:

Unfortunately I don’t know how to do this (I’m really a beginner) so I haven’t done it yet but I’m looking for tutorials, etc.

https://playground.arduino.cc/Code/AvailableMemory/

Pete.

Thank you very much Pete.

I will give some context that may help to understand my problem.

PRINCIPLE OF MY CODE
I built (partially) an Inmoov robot that I control with an Arduino Mega. I wanted to make a singing robot. I wrote a script that has a fairly simple principle: I created arrays that tell my robot which positions the 7 servos should take.

So for each song, there are 7 arrays (for 7 servos). These arrays need a lot of memory to be stored because they include thousands of integers.

At first, I used the “classic” arrays. But quickly, I encountered a problem: my arrays were too “heavy” to fit into my Arduino Mega (not enough SRAM).
I discovered that I could store my arrays in flash memory using “PROGMEM” and my problem was (temporarily) solved.

Afterwards, I installed Blynk and I have the impression that if I use “too much” the flash memory, Blynk can no longer connect to Wifi. I converted some of my PROGMEM arrays back to regular arrays and it worked again. But I’m starting to be limited because I don’t have enough SRAM memory…

If I don’t use Blynk (and I activate my robot in another way), there is no particular concern.

MEMORY MONITORING
So I did some tests

  • Test 1: with the latest version of my code that works (2 songs, I can’t add a third one, 14 long (>1500 intergers) PROGMEM arrays to control 7 servos.)
  • Test 2: I remove a song (1 song, 7 long (>1500 intergers) PROGMEM arrays to control 7 servos.)

TEST 1 memory information
Compiling ‘Robobo_Wifi’ for ‘ATmega2560 (Mega 2560) (Arduino Mega)’
Program size: 99,100 bytes (used 39% of a 253,952 byte maximum) (5.80 secs)
Minimum Memory Usage: 5829 bytes (71% of a 8192 byte maximum)

Uploading ‘Robobo_Wifi’ to ‘ATmega2560 (Mega 2560) (Arduino Mega)’ using ‘COM4’

freeMemory()=2221 (info by MemoryFree.h)

TEST 2 memory information
Compiling ‘Robobo_Wifi’ for ‘ATmega2560 (Mega 2560) (Arduino Mega)’
Program size: 76,774 bytes (used 30% of a 253,952 byte maximum) (5.39 secs)
Minimum Memory Usage: 5817 bytes (71% of a 8192 byte maximum)

Uploading ‘Robobo_Wifi’ to ‘ATmega2560 (Mega 2560) (Arduino Mega)’ using ‘COM4’

freeMemory()=2247 (info by MemoryFree.h)

And what happens if you include the Blynk libraries and attempt to connect to WiFi/Blynk?

It seems to me that memory issues are restricting you, so an ESP32 would be a good move.

Pete.

With the last version of my script (2 songs), I can connect to Wifi

Opening port
Port open
08:22:49:455 → [3359]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v1.0.1 on Arduino Mega

08:22:50:012 → [3867] Connecting to Redmi Note 8
08:22:53:067 → [6917] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
08:22:54:064 → [7933] Failed to enable MUX
08:23:03:155 → [16978] +CIFSR:STAIP,“192.168.12.48”
+CIFSR:STAMAC,“ec:fa:bc:10:5c:22”
[16979] Connected to WiFi
08:23:13:378 → [27267] Ready (ping: 34ms).

If I add just a very small PROGMEM array (one integer), I got this problem:

Opening port
Port open
09:13:36:912 → [3360]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v1.0.1 on Arduino Mega

09:13:37:357 → [3868] Connecting to Redmi Note 8
09:13:40:464 → [6918] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
09:13:50:468 → [16959] Failed to connect WiFi

(I think – from previous tests – that even adding “F()” into Serial.print function creates the same problem)

so an ESP32 would be a good move.

Yes, you are right!