-2

i am fighting with this problem for all night long, and nothing working for me... I have tried with alot of methods founded on the internet, but i'm still locked here.

All I want is to write an number in the middle of an char string to display it on an oled screen using an nodemcu v3 based on ESP8266. This is what I want to have on my screen: S7:3.55V
3.55 could be an number between 3.02 and 4.87.


Actual error is: invalid conversion from 'long int' to 'char*' [-fpermissive]

#include <Wire.h>
#include "OLED.h"
#include <sstream>
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <locale>
        
OLED display(2, 14); //OLED Declarare SDA, SCL
long randNumber;

void setup() {
  Serial.begin(9600);
  Serial.println("OLED test!");

  // Initialize display
  display.begin();
  //display.off();
  //display.on();
  display.clear();
  delay(3*1000);
}

void loop() {
  randNumber = random(3.02, 4.87);

  display.print("S7:", 5);
  display.print(randNumber, 5);
  display.print("V", 5);
  //display.print("Banc: ", 7);
  //display.print(randNumber1, 7);
  //display.print("V", 7);
  delay(3*200);
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Marcel Domuta
  • 400
  • 2
  • 11

1 Answers1

3

sprintf is your friend here, write the number to a buffer first and then print that buffer. However you still need a different way to get to that number, random returns a long value, so to get your desired result you should adjust your parameters first:

randNumber = random(302, 487);

Don't worry about the decimals we'll get them back by appropriate formatting (in any case avoiding floating point entirely avoids at the same time quite a number of trouble mainly concerning rounding issues).

char buffer[16];
snprintf
(
    buffer, sizeof(buffer),
    "S7:%ld.%.2ldV", randNumber / 100, randNumber % 100
//      ^3^ ^55 ^    ^------ 3 -----^  ^----- 55 -----^
);

Now simply print out buffer...

Alternatives involve e.g. std::ostringstream, but these are not as convenient (and not as efficient) as good old C stdio API:

std::ostringstream buffer;
buffer << "S7:" << randNumber / 100 << '.'
       << std::setw(2) << std::setfill('0') << randNumber % 100 << 'V';
print(buffer.str().c_str());
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • Thankyou buddy, it is amazing, i have spend over 12 hrs to get this work, i am working with web based codes, but there you have no problems with this kind of conversions... – Marcel Domuta Oct 11 '21 at 09:27
  • can you explain me what represent "S7:%ld.%.2ldV" %.ld and %.2ld? I want to work with example: S7:3.55V/S8:3.62V but i cant get it to work. – Marcel Domuta Oct 11 '21 at 11:06
  • 1
    @MarcelDomuta See link in question: `%` introduces a format where a text will be inserted (`%%` inserts a percent sign) and the `d` signifies 'decimal output', if no length modifier is used an `int` has to be passed to. `l` is such a length modifier and tells the formatter the variable passed to is a `long int`. `.` introduces a precision and signifies for `int` how many digits to print at least (`%02ld` has similar meaning: align for two characters and use `0` as fill character; the result is the same then...). – Aconcagua Oct 11 '21 at 11:50
  • 1
    @MarcelDomuta `"S7:%ld.%.2ldV/S8:%ld.%.2ldV", v1/100, v1%100, v2/100, v2%100` – Aconcagua Oct 11 '21 at 11:56