Author Topic: Newbie to Arduino - DigisparkOLED.h - 128X64 Oled Printing Variables  (Read 539 times)

devilfrmheaven

  • Newbie
  • *
  • Posts: 1
Hi All,

I am new to Arduino environment, I was trying to use 128x64 oled with the digispark.

But I am unsure how to print a variable in it, I do see the text that can be entered as

Code: [Select]
   enum {BufSize=9}; // If a is short use a smaller number, eg 5 or 6
  char buf[BufSize];
  oled.clear(); //all black
  oled.setCursor(0, 0); //top left
  oled.setFont(FONT8X16);
  oled.print(F("Voltage")); //wrap strings in F() to save RAM!
  oled.setFont(FONT6X8);
  oled.setCursor(0, 2); //two rows down because the 8x16 font takes two rows of 8
  volt = analogRead(analogPin);
  volt=100;
  snprintf (buf, BufSize, "%d", volt);
  oled.println(buf); //println will move the cursor 8 or 16 pixels down (based on the front) and back to X=0

How can I print the value of the voltage that I read from the analog pin?

Thanks in Advance.

PeterF

  • Hero Member
  • *****
  • Posts: 870
Re: Newbie to Arduino - DigisparkOLED.h - 128X64 Oled Printing Variables
« Reply #1 on: December 27, 2017, 08:33:05 pm »
For starters, you'd get rid of the " volt=100;" line, as that simply sets volt to equal 100, and hence you will only display that, not what you are measuring from the analog pin.

In a nutshell, Arduino analogRead's don't return a voltage as a neat decimal number. It instead maps input voltages between 0 and VCC (typically 5 volts) into integer values between 0 and 1023 ... "This yields a resolution between readings of 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit" ... so basically you will get back a number that can be converted to a voltage, and then you can display it. (Read the Arduino ReadAnalogVoltage tutorial for more detail and example code).

So... the below snippet *should* work if oled.println accepts a float type variable, else it will need to be converted from a float to a char before it can be displayed.

Code: [Select]
  // 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:
  oled.clear(); //all black
  oled.setCursor(0, 0); //top left
  oled.setFont(FONT8X16);
  oled.print(F("Voltage")); //wrap strings in F() to save RAM!
  oled.setFont(FONT6X8);
  oled.setCursor(0, 2); //two rows down because the 8x16 font takes two rows of 8
  oled.println(voltage); //println will move the cursor 8 or 16 pixels down (based on the front) and back to X=0