Hello,
I'm working on a project using an Arduino ONE board, an RTC module, a servo motor, and an OLED display. The project involves serving food to pets at specified times. I've written a code that checks the current time against predefined feeding times and activates the servo motor accordingly. However, I'm facing memory constraints and stability issues and basically the screen freeze after 4 o 5 seconds.
I would appreciate any suggestions or tips on how I can optimize my code to reduce memory usage and improve stability. Here's a simplified version of my code:
// Include libraries
#include <RTClib.h>
#include <Adafruit_SH1106.h>
#include <Servo.h>
// Define pins and objects
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
RTC_DS3231 RTC;
Servo servo;
// Define feeding times
int feedingTimes[3][3] = {
{8, 0, 0}, // Hour, minute, second for first feeding time
{12, 30, 0}, // Hour, minute, second for second feeding time
{18, 0, 0} // Hour, minute, second for third feeding time
};
void setup() {
// Initialize serial communication, RTC, display, and servo
Serial.begin(9600);
RTC.begin();
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
servo.attach(8);
servo.write(0);
}
void loop() {
// Get current time
DateTime now = RTC.now();
// Check if it's a feeding time
for (int i = 0; i < 3; i++) {
int hour = feedingTimes[i][0];
int minute = feedingTimes[i][1];
int second = feedingTimes[i][2];
if (hour == now.hour() && minute == now.minute() && second == now.second()) {
// Display feeding message on OLED display
display.clearDisplay();
display.setRotation(0);
display.setCursor(10, 10);
display.setTextColor(WHITE);
display.setTextSize(2);
display.println("FEEDING TIME");
display.display();
// Activate servo motor
servo.write(90);
delay(1300);
servo.write(0);
delay(200);
}
}
}
I suspect that the use of arrays and the OLED display might be contributing to the memory issues. Any advice on how to optimize this code would be greatly appreciated.
Thank you!
1 Like
hera are the conection details
yes all is working for 3 o 4 seconds but after that the screen stars crashing and the project not responds anymore
the screen and the servo crashes at the same time
What does the compilation, download, report tell about memory usage?
Feeding any motor from the Arduino board is a great mistake. Use separate powering.
1 Like
i have a short video that shows the behavior
i didn´t know i could try using a diferent powering, about your question im new in arduino, how do i get that info?
Regards..
Maybe if you don't refresh the screen it could work
Really?
What about the full version?
i dont understand im not refreshing the screen i just run the code into the arduino it works for 3 second and then stop, on the IDE says Low memory available, stability problems may occur.
#include <RTClib.h>
#include <Adafruit_SH1106.h>
#include <Servo.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
RTC_DS3231 RTC; // objeto del modulo RTC
Servo servo;
int tiempos[3][3] = {
{16, 27, 0}, // Hora, minuto, segundo para el primer tiempo
{17, 30, 0}, // Hora, minuto, segundo para el segundo tiempo
{18, 45, 0} // Hora, minuto, segundo para el tercer tiempo
};
int servoPin = 8;
void setup() {
Serial.begin(9600);
RTC.begin();
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
servo.attach(servoPin);
servo.write(0);
// RTC.adjust(DateTime(__DATE__,__TIME__));
}
void loop() {
display.clearDisplay();
DateTime now = RTC.now();
for (int i = 0; i < 3; i++) {
int hora = tiempos[i][0];
int minuto = tiempos[i][1];
int segundo = tiempos[i][2];
if ((hora == now.hour() && minuto == now.minute() && segundo == now.second()) || digitalRead(7) == 1) {
display.setRotation(0);
display.setCursor(10, 10);
display.setTextColor(WHITE);
display.setTextSize(2);
display.println("SIRVIENDO");
display.setRotation(0);
display.setCursor(25, 35);
display.setTextColor(WHITE);
display.setTextSize(2);
display.println("COMIDA");
display.display();
servo.write(90);
delay(1300);
servo.write(0);
delay(200);
}
}
if (!isAnyTimeMatch(now)) {
servo.write(0);
display.clearDisplay();
display.setRotation(0);
display.setCursor(30, 0);
display.setTextColor(WHITE);
display.setTextSize(1);
display.println("HORA ACTUAL:");
display.setRotation(0);
display.setCursor(30, 15);
display.setTextColor(WHITE);
display.setTextSize(1);
display.println(now.hour());
display.setRotation(0);
display.setCursor(45, 15);
display.setTextColor(WHITE);
display.setTextSize(1);
display.println(":");
display.setRotation(0);
display.setCursor(55, 15);
display.setTextColor(WHITE);
display.setTextSize(1);
display.println(now.minute());
display.setRotation(0);
display.setCursor(75, 15);
display.setTextColor(WHITE);
display.setTextSize(1);
display.println(":");
display.setRotation(0);
display.setCursor(85, 15);
display.setTextColor(WHITE);
display.setTextSize(1);
display.println(now.second());
display.display();
}
}
bool isAnyTimeMatch(DateTime now) {
for (int i = 0; i < 3; i++) {
int hora = tiempos[i][0];
int minuto = tiempos[i][1];
int segundo = tiempos[i][2];
if (hora == now.hour() && minuto == now.minute() && segundo == now.second()) {
return true;
}
}
return false;
}
The hour refreshe's the screen
Got it, i will try just use the next feed time instead of the countdown maybe it could help,
Thanks
It's shown when the compiled code is downloaded.
Do not power your servo from your Uno's 5V pin. Power it separately, only tie the gnd of the Uno to the GND of the external supply
ok, it says: Low memory available, stability problems may occur.
nothing else.
Thaks for the help, i will do it and let you know if it works
to test, just leave the servo off, if the rest of it runs you know where your problem lies. There may be more than one, but generally, powering a servo from the 5V pin is a recipe for exactly your symptoms - works, then fails quickly, because the regulator overheats.
1 Like