Hi all. I apologize in advance for my lack of knowledge of both electronics and programming. I am trying to learn, but Google searches only get me so far.
I bought an OLED LED module for my Arduino UNO Rev3, and probably should have waited to experiment with it until I'd gotten some other key concepts under my belt, but I was just too excited. This question is about the programming, not the OLED LED display.
I wanted to combine the standard example sketch "Read Analog Voltage" with the "Hello World" example from the u8glib library included for the display. My hope was to have the voltage shown on the display, in addition to the serial monitor.
Here is the code I'm using (note: I have removed a number of lines of commented display-specific options):
/*
HelloWorld.pde
"Hello World!" example code.
Universal 8bit Graphics Library, http://code.google.com/p/u8glib/
Copyright (c) 2012, olikraus@gmail.com
All rights reserved.
*/
#include "U8glib.h"
/* SLboat Add Device */
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C 128x64(col2-col129) SH1106,Like HeiTec 1.3' I2C OLED
void draw(void) {
int vltgread = analogRead(A0);
float vltg = vltgread * (5.0 / 1023.0);
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr( 0, 10, "VOLTAGE:");
u8g.setFont(u8g_font_fub17);
char buf[9];
sprintf (buf, "%d", vltg);
u8g.drawStr( 0, 58, buf);
Serial.println(buf);
}
void setup(void) {
Serial.begin(9600);
// flip screen, if required
// u8g.setRot180();
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
// rebuild the picture after some delay
delay(2000);
}
My question has to do with my attempt to convert the float variable containing the calculated voltage (using a solution I found on more than one message board on the topic):
int vltgread = analogRead(A0);
float vltg = vltgread * (5.0 / 1023.0);
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr( 0, 10, "VOLTAGE:");
u8g.setFont(u8g_font_fub17);
char buf[9]; <--------
sprintf (buf, "%d", vltg); <--------
u8g.drawStr( 0, 58, buf); <--------
Serial.println(buf);
When this sketch is run, the serial monitor shows two types of output: A single line showing the voltage calculated in the void loop section, which is a decimal number displaying to the hundredths. Then eight lines in succession with four-to-five-digit integers. For example (these are not real values):
1.53
8663
10224
8663
7940
12084
8663
8664
8664
This repeats every 2000ms (as the program indicates). The reading of the eight integers happens simultaneously, and causes the LED to display a jumble of broken numbers.
Here is what I can't figure out, because my programming knowledge is not deep, and Google isn't helping:
What is the purpose of assigning the variable buf as an array? And what is the relevance of "9"?
I changed that value to 0, 1, 4, and 12. Values 4 and 12 seem to behave exactly the same, with eight simultaneous vltg readings being displayed to the serial monitor. Values 0 and 1 cause an endless stream of readings to be displayed (if I recall correctly). Is "9" arbitrary?
Follow up question....Why are eight simultaneous readings being sent to the serial monitor, and why are they not decimal values exactly like the value of voltage, since they are both derived the same way (with the only exception of vltg being converted to a string for use with the u8g.drawStr().
I truly appreciate any insight into this behavior.
(Although I'm certainly not asking for workarounds or alternative solutions, I wouldn't say no to any advice.)