Creating small project using Pro mini Atmega168 16Mhz and ssd1306.
While compilig project got a big project size message, and it uses about 122% device memory.
Determined part of code that eat memory
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
Hiding this part decrease size to 24%
int analogInput = 0;
int pcal = 0.0;
int pr = 0.0;
int value = 0;
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
void draw(void) {
u8g.setFont(u8g_font_osb26);
//u8g.setFont(u8g_font_osb21);
u8g.setPrintPos( 10, 11);
u8g.print(pr,1);
}
void setup()
{
pinMode(analogInput, INPUT);
}
void loop()
{
value = analogRead(analogInput);
pcal = (value * 5.05) / 1024.0;
pr = pcal*300 ;
if (pr<0.04) {
pr=0.0;
}
{
delay(500);
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
}
So how that possible 4 lines uses 98% devices memory? And how to solve this?
Using library U8glib at version 1.19.1 in folder: C:\Users\David\Documents\Arduino\libraries\U8glib
"C:\\Program Files\\Arduino-1.8.9\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\David\\AppData\\Local\\Temp\\arduino_build_484368/u8g_feeze.ino.elf"
Sketch uses 17702 bytes (54%) of program storage space. Maximum is 32256 bytes.
Global variables use 229 bytes (11%) of dynamic memory, leaving 1819 bytes for local variables. Maximum is 2048 bytes.
So I can't see where your problem lies.
It is always helpful to select File->Preferences->Verbose Compile
Ok, you answered why you hanvent find problems.
Sketch uses 17702 bytes (54%) of ardu uno with atmega328.
As i say i have Atmega168. So code is too big for 168 untill // 4 lines
Anyway, I built your sketch and ran it on a Uno. But I had to change several things e.g. use float variables and change print position. But it obviously still used too much Flash.
I re-wrote it for U8g2.
Using library arduino_243729 at version 2.26.14 in folder: C:\Users\David\Documents\Arduino\libraries\arduino_243729
Using library SPI at version 1.0 in folder: C:\Program Files\Arduino-1.8.9\hardware\arduino\avr\libraries\SPI
Using library Wire at version 1.0 in folder: C:\Program Files\Arduino-1.8.9\hardware\arduino\avr\libraries\Wire
"C:\\Program Files\\Arduino-1.8.9\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\David\\AppData\\Local\\Temp\\arduino_build_484368/u8g_feeze.ino.elf"
Sketch uses 10154 bytes (31%) of program storage space. Maximum is 32256 bytes.
Global variables use 656 bytes (32%) of dynamic memory, leaving 1392 bytes for local variables. Maximum is 2048 bytes.
These numbers should "fit" on your 168 chip. The reason for smaller size is down to using a number font.
Note that "arduino_243729" is the U8g2 library directory on this Win7-32 Laptop.
I am not sure why the Library Manager sometimes invents dummy directory names.
This is my u8g2 version. I hope you can see the differences.
int analogInput = 0;
float pcal = 0.0; //.kbv
float pr = 0.0; //.kbv
int value = 0;
//#include "U8glib.h"
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
#include "U8g2lib.h"
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void draw(void) {
//u8g.setFont(u8g_font_osb26);
u8g2.setFont(u8g2_font_osb26_tn); //.kbv use number only font
//u8g.setFont(u8g_font_osb21);
//u8g.setPrintPos( 10, 31); //.kbv was 11
u8g2.setCursor( 10, 31); //.kbv u8g2 has diff method
u8g2.print(pr, 1);
}
void setup()
{
pinMode(analogInput, INPUT);
u8g2.begin(); //.kbv
}
void loop()
{
value = analogRead(analogInput);
value = random(0, 1023); //.kbv make some data
pcal = (value * 5.05) / 1024.0;
pr = pcal * 300 ;
if (pr < 0.04) {
pr = 0.0;
}
{
delay(500);
u8g2.firstPage();
do {
draw();
} while ( u8g2.nextPage() );
}
}
My apologies again. I can understand people choosing a 5V or 3.3V Pro Mini (m328p).
I had not considered anyone ever choosing a m168 version
I am not sure why the Library Manager sometimes invents dummy directory names.
This happens regularly to me. It happens either if I had modified a library file, or if I have a library file open in an editor. It seems it happens if the Library Manager can't delete the old library and then rename the newly downloaded one. It can lead to strange compile errors, if the IDE then doesn't find the library, until restarted.
I can understand a "renaming" problem with a file "in use"
But I deliberately leave the IDE. Delete the dummy directory. Restart the IDE.
So as far as the IDE is concerned, it is installing a fresh library for the first time in a fresh directory.
If there was an "in use" problem my "delete old dummy directory" would have failed.
Hey-ho. I suspect there is a Java expert who can explain.
Thanks, U8g2 and your code really worked.
Another part of problem,analogread gives me crappy data withought input signal.
If i just measure voltage on analogpin it jumps like 0-5.0 - 0- 2.3 -0-3.7-0-4.7.
Why that happen if theres no input at all?
i // random line
AnalogRead() will give you "crappy" data if the pin is open-circuit.
You are just reading random noise.
If you are using analogRead() just make sure that you connect it to external electronics.
I used random() so that I could get some data without hooking up external electronics.
If you are reading a low-impedance voltage, you can enable the internal pullup
A pullup will ensure a known voltage when the analog pin is disconnected
A pullup will not upset the readings when connected (to a low impedance signal).
You can not use a pullup or pulldown with a high impedance signal.
I am not familiar with U8g fonts. If there is a number-only U8g font, it will reduce your Flash usage.
Since Oliver recommends U8g2 it seems wiser to use U8g2. And the fonts are small.
david_prentice:
AnalogRead() will give you "crappy" data if the pin is open-circuit.
You are just reading random noise.
If you are using analogRead() just make sure that you connect it to external electronics.
I used random() so that I could get some data without hooking up external electronics.
If you are reading a low-impedance voltage, you can enable the internal pullup
A pullup will ensure a known voltage when the analog pin is disconnected
A pullup will not upset the readings when connected (to a low impedance signal).
You can not use a pullup or pulldown with a high impedance signal.
I am not familiar with U8g fonts. If there is a number-only U8g font, it will reduce your Flash usage.
Since Oliver recommends U8g2 it seems wiser to use U8g2. And the fonts are small.
The real mystery is: Why did you buy a mega168 ?
David.
Well i use Uno mostly and got that pro mini because of project size, and 168 is the one that locally been in
shop. Dont want annoy you with noob questions, can i get link for internal pullup?
My project desired to measure voltage from 0 to 1 V, and it must be precision at low fluctuations.
Thanks for your answers again
If you are measuring a battery voltage or output voltage from an external chip, these are low-impedance sources. i.e. a 30k internal pullup will not alter the voltage read by the ADC.
If you are trying to measure the voltage generated by a Butterfly wing, it will be a high-impedance source.
Quite honestly, just connect A0 pin to your sensor without any pullup or pulldown.