Need to upgade to mega?

Hi All,
I’m doing a relatively simple project with an Uno and it runs out of memory.
It reads a frequency from an LMH1981 Sync decoder and displays the incoming signal frequency on an 1,3" oled display.
I’m not an experienced programmer so maybe there are ways to make my code more efficient?
Or do i need to upgrade to a bigger processor? I only need a few pins so it feels a bit strange for me to use a Mega.

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <FreqCount.h>

/*
  U8g2lib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.
    
*/

// Please UNCOMMENT one of the contructor lines below
// U8g2 Contructor List (Frame Buffer)
// The complete list is available here: https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
// Please update the pin numbers according to your setup. Use U8X8_PIN_NONE if the reset pin is not connected

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);



void setup() {
  u8g2.begin();
    
  FreqCount.begin(1000);

    u8g2.clearBuffer();  
    u8g2.setFont(u8g2_font_6x10_mr); 
    u8g2.drawStr(10,10,"GENLOCK  READER"); 
    u8g2.setCursor(90,30); 

    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.drawStr(10,55,"BOOTING.."); 
    u8g2.sendBuffer();          
delay(2000); 


}


void loop() {


  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();

  if (count < 15500) {

    u8g2.clearBuffer();  
    u8g2.setFont(u8g2_font_6x10_mr); // choose a suitable font
    u8g2.drawStr(10,10,"GENLOCK  READER");  // write something to the internal memory

    u8g2.setFont(u8g2_font_10x20_mr);  // choose a suitable font
    u8g2.drawStr(10,55,"NO SIGNAL"); 
    u8g2.sendBuffer();          // transfer internal memory to the display
  }
    
  if (count > 15500 && count < 15700){
    
    u8g2.clearBuffer();     
    u8g2.setFont(u8g2_font_6x10_mr);
    u8g2.drawStr(10,10,"GENLOCK  READER");  
    u8g2.drawStr(10,25,"Signal: PAL");
    u8g2.drawStr(10,35,"Frequency:");
    
    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.setCursor(10,55); 
    u8g2.print(count/312.64);
    u8g2.println(" Hz");
    
    u8g2.sendBuffer();  
     }

 if (count > 15700 && count < 20000){
    
    u8g2.clearBuffer();     
    u8g2.setFont(u8g2_font_6x10_mr);
    u8g2.drawStr(10,10,"GENLOCK  READER");  
    u8g2.drawStr(10,25,"Signal: NTSC");
    u8g2.drawStr(10,35,"Frequency:");
    
    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.setCursor(10,55); 
    u8g2.print(count/262.61);
    u8g2.println(" Hz");
    
    u8g2.sendBuffer();  
     }

if (count > 20000 && count < 27500){
    
    u8g2.clearBuffer();     
    u8g2.setFont(u8g2_font_6x10_mr);
    u8g2.drawStr(10,10,"GENLOCK  READER");  
    u8g2.drawStr(10,25,"Signal: 1080 PsF");
    u8g2.drawStr(10,35,"Frequency:");
    
    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.setCursor(10,55); 
    u8g2.print(count/1125.40);
    u8g2.println(" Hz");
    
    u8g2.sendBuffer();  
     }

if (count > 27500 && count < 35000){
    
    u8g2.clearBuffer();     
    u8g2.setFont(u8g2_font_6x10_mr);
    u8g2.drawStr(10,10,"GENLOCK  READER");  
    u8g2.drawStr(10,25,"Signal: 1080 i");
    u8g2.drawStr(10,35,"Frequency:");
    
    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.setCursor(10,55); 
    u8g2.print(count/562.75);
    u8g2.println(" Hz");
    
    u8g2.sendBuffer();  
     }

if (count > 35000 && count < 50000){
    
    u8g2.clearBuffer();     
    u8g2.setFont(u8g2_font_6x10_mr);
    u8g2.drawStr(10,10,"GENLOCK  READER");  
    u8g2.drawStr(10,25,"Signal: 720 P");
    u8g2.drawStr(10,35,"Frequency:");
    
    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.setCursor(10,55); 
    u8g2.print(count/750.34);
    u8g2.println(" Hz");
    
    u8g2.sendBuffer();  
     }

  if (count > 50000) {

    u8g2.clearBuffer();  
    u8g2.setFont(u8g2_font_6x10_mr);
    u8g2.drawStr(10,10,"GENLOCK  READER");  

    u8g2.setFont(u8g2_font_10x20_mr); 
    u8g2.drawStr(10,55,"INVALID SGN"); 
    u8g2.sendBuffer();       
  }


  }

}

Thanks in advance!

Tim

Maybe try a Nano Every, which has 3 times the SRAM of a Uno.

In your code what do you think happens when:
count == 20000 or
count == 27500 or
count == 35000 or
count == 50000
?

For the next stage in your learning look up how ‘else’ and ‘else if’ work.
https://www.arduino.cc/reference/en/#structure

Hi Perry,

Thanks for that! I didn't knew the Nano Every, but seems to be the solution!

Yes i know the code counts a lot. And thats what it needs to do, but maybe there could be smarter ways.

For now I will order a nano every.

displays the incoming signal frequency on an 1,3" oled display.

I've used an I2C OLED display briefly in the past. The library for it consumes quite a bit of RAM if I recall. If you are not doing anything graphical - i.e. just displaying text, no graphs, trends etc - there is a modified I2C OLED library here that just does text. It has a lower RAM requirement.

tim_hamberg:
Yes I know the code counts a lot.

I think you missed my point. Read what I said again and think about what I am asking.

For now I will order a nano every.

Well, I made the mistake of only ordering 1. Then I went back and ordered a pack of 3, so now I have 4. I should have ordered the 3 pack to start with.

Here's one of mine working

Look at the u8g2 examples for using a page buffer instead of a frame buffer. The u8x8 examples are also a possibility for monochrome text without graphics.
Instead of drawStr, use setCursor and print, along with the F() macro for the quoted text.

PerryBebbington:
I think you missed my point. Read what I said again and think about what I am asking.

Do you mean that if i start with the highest amount and if that is "true", that i can skip the other ones, because they will also be true?
I will also try the tips for oled library, i'm not in the need for graphics so could save some there.
Thanks all!

tim_hamberg:
Do you mean that if i start with the highest amount and if that is "true", that i can skip the other ones, because they will also be true?

No, but something along those lines is something you can explore using else if.

Look carefully at the specific values of count that I listed and look at your code. What does your code do for those specific values?

I would have thought a Uno should be fine for a job like this , and I would be suss of your code. I don't think anybody uses arduino.h and it may be more a hindrance than a help.
You might also look at using Adafruit's ascii library. There is no need to use u8g for text.

If you are not going to be adding anything to the sketch it does not look bad as-is, the ram usage is a bit over 1700 bytes, which gives a low memory warning, but you do not appear to be doing anything that would use an extensive amount of what is left. Changing from a frame buffer to a page buffer for the display will use substantially less memory, a frame buffer on a 128x64 display uses 1024 bytes of memory, half of the available ram on an UNO.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.