I am trying to adapt a temperature history monitor program to use the U8GLIB library.
The program basically reads & saves the current temperature to "tcurrent", shifts a 113 element array 1 position, and sticks the new value at the 0th spot.
Then the array is drawn, pixel by pixel, starting at a=15 (to get past the "Y-axis" labels).
I expected to see 1 pixel drawn at a time, but instead get "multiple pixels".
I slowed down the routine with delays at appropriate spots, and then used the serial monitor and looked at "a". Instead of a going through the loop once (from 15 to 127) it is doing this loop 8 times.
Each and every time...8 times through the loop. I looked back at the code to see what could cause this to occur, but I am stumped. Which is easy, because I'm fairly new to Arduino and even newer to U8GLIB.
I also get a compile warning with this routine...not other routines, just this one.
The warning is:
U8glib\utility\u8g_rot.c:48:1 warning (near initialization for 'u8g_dev_rot.dev_fn' [enabled by default]
(googled the above but still unsure of the meaning; doesn't appear to be a fatal error though)
Thanks for any insights...Al
#include "U8glib.h"
//constructor
U8GLIB_KS0108_128 u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 14, 15, 17, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs1=14, cs2=15,di=17,rw=16
int tcurrent;
int tempArray[113];
void setup(void) {
u8g.setFont(u8g_font_5x7);
//u8g.setRot180();
}
void getTemp() // function to read temperature from TMP36
{
float sum = 0;
float voltage = 0;
float sensor = 0;
float celsius;
// read the temperature sensor and convert the result to degrees C
sensor = analogRead(5);
voltage = (sensor * 5000) / 1024;
voltage = voltage - 500;
celsius = voltage / 10;
tcurrent = int(celsius);
// insert the new temperature at the start of the array of past temperatures, shift array 1 element position
for (int a = 112 ; a >= 0 ; --a )
{
tempArray[a] = tempArray[a-1];
}
tempArray[0] = tcurrent;
}
void draw(void) {
// draw labels at top and on y-axis
int q=0;
//label at top with current value of tcurrent
u8g.drawStr( 40, 7, "Current:");
u8g.setPrintPos(80, 7);
u8g.print(tcurrent);
//y-axis labels
u8g.drawStr( 0, 14, "40");
u8g.drawStr( 0, 24, "32");
u8g.drawStr( 0, 34, "24");
u8g.drawStr( 0, 44, "16");
u8g.drawStr( 0, 54, "8");
u8g.drawStr( 0, 64, "0");
//value of a[0] at a=15...a[1] at a=16 etc
//start drawing 1st pixel at a=15 so to be past y-axis labels
for (int a = 15 ; a < 127 ; a++)
{
q = (55 - tempArray[a-15]);
u8g.drawPixel(a,q);
}
}
void loop(void) {
// picture loop
u8g.firstPage();
do {
getTemp();
draw();
} while( u8g.nextPage() );
// rebuild the picture after some delay
delay(10000);
}