Low memory available

Hey evrybody

I try to use arduino uno wifi. the program is working.

but if I add Ciao to write date to thingspeak.com server I get this error:

Sketch uses 16016 bytes (49%) of program storage space. Maximum is 32256 bytes.
Global variables use 1663 bytes (81%) of dynamic memory, leaving 385 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

I guess it's tako a lot of memory , but it just (49%), (81%)

If I try only writing ciao without my code it is workink excelent

what can I do?

to be a clear the data not send after that error

Do you have a lot of literal strings in your sketch?

As in Serial.println("XXXXXXXXXXXX");

If so try going through your code and replacing "XXXXXXXXXXXX" with F("XXXXXXXXXXXX")

The F macro relocates the literal string to flash memory, where your sketch is stored in the Arduino, and frees up the limited space in global memory.

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <UnoWiFiDevEd.h>
 
#define CONNECTOR     "rest" 
#define SERVER_ADDR   "api.thingspeak.com"
#define APIKEY_THINGSPEAK  "********" //Insert your API Key 
//variables declaration
AlarmId id;
short stateTakePills = 0;

//=PINS
byte alarmLedPin = 13;
byte openBoxPin = 12;
byte switchPin = 2;  
byte buzzerPin = 8; 

// = SWITCH STATE     
byte state = HIGH;     
byte reading;           
byte previous = LOW;    
byte isSnooze = 1;

//buzzer TIME OUT
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";                           
int count = 0;
int count2 = 0;
int count3 = 0;
int TimeOutCaounter = 0;
int MAX_COUNT = 24;
int statePin = LOW;

// open box buzzer
int length = 28; // the number of notes
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8,8, 16, 1, 2,2,8,8,8,8,16, 1,2,2,8,8,8,16 };
int tempo = 150;


void setup() {
 Ciao.begin(); // CIAO INIT
 Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  //set alarms: morning, noon and evening
  setTime(8,29,50,1,1,11); 
  // === MORNNING
  Alarm.alarmRepeat(8,30,0, startAlarm);  
  Alarm.alarmRepeat(8,30,30, TimeOut); 
  Alarm.alarmRepeat(8,30,50, TimeOut);
  // === NOON
  Alarm.alarmRepeat(8,32,0, startAlarm); 
  Alarm.alarmRepeat(8,32,30, TimeOut);  
  Alarm.alarmRepeat(8,32,50, TimeOut);
  // === EVENING
  Alarm.alarmRepeat(8,34,0, startAlarm);  
  Alarm.alarmRepeat(8,34,30, TimeOut);  
  Alarm.alarmRepeat(8,34,50, TimeOut); 
  //pins mode
  pinMode(switchPin, INPUT);
  pinMode(alarmLedPin, OUTPUT);
  pinMode(openBoxPin, OUTPUT);
  digitalWrite(alarmLedPin, LOW);
  digitalWrite(openBoxPin, LOW);
 // digitalWrite(switchPin, HIGH);
}

void loop() {
  Serial.print(".");
    
  Alarm.delay(1000); 
 
}//loop 



void startAlarm() {
    Serial.println("start"); 
    String uri = "/update?api_key=";
    uri += APIKEY_THINGSPEAK;
    uri += "&field1=";
    uri += String(stateTakePills);
    uri += "&field2=";
    uri += String(stateTakePills);
  //Ciao.println("Send data on ThingSpeak Channel"); 
      
  Ciao.write(CONNECTOR, SERVER_ADDR, uri);
    delay(30000); // Thinkspeak policy

    Alarm.delay(100);  
    id = Alarm.timerRepeat(1, Repeats);// timer for every 1 seconds, check switch state
    isSnooze = 1;//if the box  didn't open 
    delay(110);

}//startAlarm


void Repeats() {
     reading = digitalRead(switchPin); 
     Serial.print(reading);
     if (reading == LOW) 
      { 
        openBox();  
      }       
     else 
       digitalWrite(openBoxPin, LOW);
   
}//Repeats


void TimeOut(){
  Serial.println("TimeOut");
  if(isSnooze == 1)
  {
     if(TimeOutCaounter == 0)
     {
        TimeOutCaounter++;
     }
     else
     {
       Alarm.free(id); 
       isSnooze = 0;
       TimeOutCaounter--;
     }
     
     /*
      * Play buzzer alarm and blink red led 
      * in case that box didn't open.
      */
     analogWrite(buzzerPin, 0);     
       for (count = 0; count < MAX_COUNT; count++) {
            statePin = !statePin;
            digitalWrite(alarmLedPin, statePin);
            for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
                for (count2=0;count2<8;count2++) {
                    if (names[count2] == melody[count*2 + 1]) {       
                        analogWrite(buzzerPin,500);
                        delayMicroseconds(tones[count2]);
                        analogWrite(buzzerPin, 0);
                        delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(buzzerPin, 0);
          delayMicroseconds(500);
        }
      }//for1
    }//for2
  }//for3 
}//if
}//timeout

void LedOff(){
  digitalWrite(openBoxPin, LOW);
}


void playTone(int tone, int duration) {
  //play
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {  
     digitalWrite(buzzerPin, HIGH);
     delayMicroseconds(tone);
     digitalWrite(buzzerPin, LOW);
     delayMicroseconds(tone);

  }//for
}//playmode

void playNote(char note, int duration) {
  char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'x', 'y' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956,  834,  765,  593,  468,  346,  224, 655 , 715 };
  int SPEE = 5;
   // play the tone corresponding to the note name
   for (int i = 0; i < 17; i++) {
      if (names[i] == note) {
        int newduration = duration/SPEE;
        playTone(tones[i], newduration);
      }
    }//for
}//playNote


void openBox() {
  digitalWrite(openBoxPin, HIGH);
  Alarm.free(id);
  // id = dtINVALID_ALARM_ID;
  isSnooze = 0;
  Alarm.timerOnce(15, LedOff); // called once after 10 seconds
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
       delay(beats[i] * tempo); // rest
   } else {
     playNote(notes[i], beats[i] * tempo);
   }
   // pause between notes
   delay(tempo);
   }//for
}//openBox




 // digitalWrite(2, HIGH); // Set up internal Pull-up Resistor

//long time = 0;         // the last time the output pin was toggled
//long debounce = 200;   // the debounce time, increase if the output flickers

thanks Can i convert this to flash also?

String uri = "/update?api_key=";
    uri += APIKEY_THINGSPEAK;
    uri += "&field1=";
    uri += String(stateTakePills);
    uri += "&field2=";
    uri += String(stateTakePills);
  //Ciao.println("Send data on ThingSpeak Channel"); 
      
  Ciao.write(CONNECTOR, SERVER_ADDR, uri);

You seem to have two arrays with more or less similar values in it:

byte names = {‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’};
int tones = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};

Consider having just one array each with all notes and all tones in that all your functions use. That would save a bit of space.

You can also relocate the elements in these arrays to flash memory, assuming you never change the value of any array elements, by using PROGMEM. The is a whole page on the arduino website explaining how to use PROGMEM for arrays along with the special functions you use to index an array relocated to flash memory.

Also none of your literal strings are enclosed with the F macro - F(""). Make that change and it will save you a fair amount of memory.

thanks

I tried ,delete variables, put into the func etc. but the sketch that do only sending data by Ciao take 66% of dynamic memor

there is a way to to decrease the Uno WiFi Dev. Edition Library, func or like that? http://platformio.org/lib/show/1440/Uno%20WiFi%20Dev.%20Edition%20Library