PlatformIO Community

"randomnumber" prints, I want randomnumber to print

I added BLE to a script that lights up LEDs with a random number,
As is, “randomnumber” is being advertised. I want the number to go out. That number is ‘long’, from 1 to 1,000,000.

If I remove the quotes around “randomNumber” on line 126, I get this message. I think I need to work a ‘long’ or something into the text.

How can I go from “randomnumber” to the random number (= randomnumber)?

Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
src\main.cpp: In function 'void loop()':
src\main.cpp:126:41: error: no matching function for call to 'setValue(long int&)'
   pCharacteristic->setValue(randomNumber);
                                         ^
In file included from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEServer.h:20:0,
                 from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEDevice.h:18,
                 from src\main.cpp:9:
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:75:7: note: candidate: void BLECharacteristic::setValue(uint16_t&) <near match>
  void setValue(uint16_t& data16);
       ^
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:75:7: note:   conversion of argument 1 would be ill-formed:
src\main.cpp:126:41: error: invalid initialization of non-const reference of type 'uint16_t& {aka short unsigned int&}' from an rvalue of type 'uint16_t {aka short unsigned int}'
   pCharacteristic->setValue(randomNumber);
                                         ^
In file included from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEServer.h:20:0,
                 from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEDevice.h:18,
                 from src\main.cpp:9:
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:76:7: note: candidate: void BLECharacteristic::setValue(uint32_t&) <near match>
  void setValue(uint32_t& data32);
       ^
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:76:7: note:   conversion of argument 1 would be ill-formed:
src\main.cpp:126:41: error: invalid initialization of non-const reference of type 'uint32_t& {aka unsigned int&}' from an rvalue of type 'uint32_t {aka unsigned int}'
   pCharacteristic->setValue(randomNumber);
                                         ^
In file included from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEServer.h:20:0,
                 from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEDevice.h:18,
                 from src\main.cpp:9:
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:77:7: note: candidate: void BLECharacteristic::setValue(int&) <near match>
  void setValue(int& data32);
       ^
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:77:7: note:   conversion of argument 1 would be ill-formed:
src\main.cpp:126:41: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
   pCharacteristic->setValue(randomNumber);
                                         ^
In file included from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEServer.h:20:0,
                 from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEDevice.h:18,
                 from src\main.cpp:9:
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:78:7: note: candidate: void BLECharacteristic::setValue(float&) <near match>
  void setValue(float& data32);
       ^
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:78:7: note:   conversion of argument 1 would be ill-formed:
src\main.cpp:126:41: error: invalid initialization of non-const reference of type 'float&' from an rvalue of type 'float'
   pCharacteristic->setValue(randomNumber);
                                         ^
In file included from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEServer.h:20:0,
                 from C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLEDevice.h:18,
                 from src\main.cpp:9:
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:79:7: note: candidate: void BLECharacteristic::setValue(double&) <near match>
  void setValue(double& data64);
       ^
C:\Users\joema\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src/BLECharacteristic.h:79:7: note:   conversion of argument 1 would be ill-formed:
src\main.cpp:126:41: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double'
   pCharacteristic->setValue(randomNumber);
                                         ^
*** [.pio\build\esp32dev\src\main.cpp.o] Error 1
==================================================================================================== [FAILED] 
#include <Arduino.h>
#include <Arduino.h>
#include <Time.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Tone32.h>
#include <Arduino.h>
#include<BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"



#define PAUSE 3000 // How long to delay?

#define LED01 26
#define LED02 25
#define LED03 17
#define LED04 16
#define LED05 27

int LEDone = LED01;
int LEDtwo = LED02;
int LEDthree = LED03;
int LEDfour = LED04;
int LEDfive = LED05;

const int BZR01 = 14;
const int BZR02 = 12;
const int BZR03 = 13;
const int BZR04 = 5;
const int BZR05 = 13;

long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.

Adafruit_SSD1306 display(128, 64, &Wire, 8); //Declaring the display name (display)

// the setup routine runs once when you press reset:
void setup()
{

  // initialize each of the digital pins as an output.
  pinMode(LEDone, OUTPUT);
  pinMode(LEDtwo, OUTPUT);
  pinMode(LEDthree, OUTPUT);
  pinMode(LEDfour, OUTPUT);
  pinMode(LEDone, OUTPUT);
  pinMode(LEDfive, OUTPUT);
  pinMode(BZR01, OUTPUT);
  pinMode(BZR02, OUTPUT);
  pinMode(BZR03, OUTPUT);
  pinMode(BZR04, OUTPUT);
  pinMode(BZR05, OUTPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(1, 1);

  display.print("SuperJoe"); //Show the name, you can remove it or replace it
  display.setCursor(32, 12);
  display.setTextSize(2);
  display.println("Monster");
  display.setCursor(32, 28);
  display.println("Labs");
  display.display();

  delay(5000);
  randomSeed(analogRead(0)); // This seeds the random number generator
}

long generateRandomNumber()
{
  return random(0, 1000000); // Generate random numbers between 0 and one million
}

void loop()
{

  // Start with lights off.
  digitalWrite(LEDone, LOW);
  digitalWrite(LEDtwo, LOW);
  digitalWrite(LEDthree, LOW);
  digitalWrite(LEDfour, LOW);
  digitalWrite(LEDfive, LOW);
  delay(PAUSE);

  randomNumber = generateRandomNumber();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(1, 1);
  display.println("Random Number:");
  display.println();
  display.println();
  display.setCursor(10, 10);
  display.setTextSize(3);
  display.println(randomNumber); //Show the name, you can remove it or replace it

  display.display();

  display.println(randomNumber);

  Serial.println(randomNumber);

  BLEDevice::init("Monster Laboratories");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
      CHARACTERISTIC_UUID,
      BLECharacteristic::PROPERTY_READ |
          BLECharacteristic::PROPERTY_WRITE);

  pCharacteristic->setValue("randomNumber");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();

  if (randomNumber % 2 == 0) //Is it even? (Divisible without remainder by 2?)
  {
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, HIGH);
    digitalWrite(LEDthree, LOW);
    digitalWrite(LEDfour, LOW);
    digitalWrite(LEDfive, LOW);
    digitalWrite(14, HIGH);

    tone(BZR01, 1000, 1000);
  }

  else if (randomNumber % 2 == 1) //If there's a remainder, it's odd.
  {
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, LOW);
    digitalWrite(LEDthree, LOW);
    digitalWrite(LEDfour, LOW);
    digitalWrite(LEDfive, LOW);
    tone(12, 1000, 1000);
  }

  if (randomNumber % 3 == 0)
  {
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, HIGH);
    digitalWrite(LEDthree, HIGH);
    digitalWrite(LEDfour, LOW);
    digitalWrite(LEDfive, LOW);
    tone(13, 1000, 1000);
  }

  if (randomNumber % 4 == 0)
  {
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, HIGH);
    digitalWrite(LEDthree, HIGH);
    digitalWrite(LEDfour, HIGH);
    digitalWrite(LEDfive, LOW);
    tone(5, 1000, 1000);
  }

  if (randomNumber % 5 == 0)
  {
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, HIGH);
    digitalWrite(LEDthree, HIGH);
    digitalWrite(LEDfour, HIGH);
    digitalWrite(LEDfive, HIGH);
    tone(13, 1000, 1000);
  }
  delay(PAUSE);

  if (randomNumber == 0 || randomNumber == 1) // Is it a prime number?

  {
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, HIGH);
    digitalWrite(LEDthree, HIGH);
    digitalWrite(LEDfour, HIGH);
    digitalWrite(LEDfive, HIGH);
    tone(BZR01, 300, 1000);
    tone(BZR02, 500, 1000);
    tone(BZR03, 700, 1000);
    tone(BZR04, 900, 1000);
    tone(BZR05, 1100, 1000);
    delay(500);
    digitalWrite(LEDone, LOW);
    digitalWrite(LEDtwo, LOW);
    digitalWrite(LEDthree, LOW);
    digitalWrite(LEDfour, LOW);
    digitalWrite(LEDfive, LOW);
    digitalWrite(BZR01, LOW);
    digitalWrite(BZR02, LOW);
    digitalWrite(BZR03, LOW);
    digitalWrite(BZR04, LOW);
    digitalWrite(BZR05, LOW);
    delay(500);
    digitalWrite(LEDone, HIGH);
    digitalWrite(LEDtwo, HIGH);
    digitalWrite(LEDthree, HIGH);
    digitalWrite(LEDfour, HIGH);
    digitalWrite(LEDfive, HIGH);
    tone(BZR01, 300, 1000);
    tone(BZR02, 500, 1000);
    tone(BZR03, 700, 1000);
    tone(BZR04, 900, 1000);
    tone(BZR05, 1100, 1000);
    delay(500);
    digitalWrite(LEDone, LOW);
    digitalWrite(LEDtwo, LOW);
    digitalWrite(LEDthree, LOW);
    digitalWrite(LEDfour, LOW);
    digitalWrite(LEDfive, LOW);
    digitalWrite(BZR01, LOW);
    digitalWrite(BZR02, LOW);
    digitalWrite(BZR03, LOW);
    digitalWrite(BZR04, LOW);
    digitalWrite(BZR05, LOW);
  }

  delay(PAUSE); // Pause for 3 seconds
}

Your error messages are a hint. There’s no long option for setValue().

Try changing this:

long randomNumber = 0L;

To:

uint32_t randomNumber = 0;

Then change this:

pCharacteristic->setValue("randomNumber");

To:

pCharacteristic->setValue(randomNumber);

That should work.

Cheers,
Norm.

1 Like

It was in quotes because the quotes made the error go away.

That’s because there’s a version of setValue() which takes a char * parameter.

Cheers,
Norm.

I can’t connect fast enough. It quits connection to LightBlue.

The BLE code actually causes the program to stop running.

My program only takes a few seconds. I saw a long list of reads on LightBlue, indicating to me that I sent several random number out over the blue teeth.

But it only worked as far as the one where I had Hello World in HEX.

What do you mean? The one in hex, “Hello World, says Neil” sent out thevdata in Hex, but you had to configure lightblue to show it in ASCII.

Are you seeing hex again, even after configuring lightblue?

As I’ve said before, make it simple. If something doesn’t work, reduce the complexity until you have something to work with.

Send out one number, the same one, in your loop(), not lots of random numbers. uint32_t randomNumber = 0xDEADBEEF; is a good one. That’s 4 bytes or 32 bits, and fits in a uint32_t.

You will either see “DEADBEEF” in lightblue, indicating that you are in Hex mode and need to configure, or, you will see “3,735,928,559” (my commas), which is the value in decimal.

Try it. What do you see?

Cheers,
Norman.

The issue is mechanical. When I hit the button and the script starts running, it starts the bluetooth, gets a number, and I bet sends the number.

While that’s happening, I’m selecting my server from the list and tapping the read thing. and then quickly hit the ‘hex’ button to translate. And the connection quits. In those moments, I’m not going to see the number on the OLED and then see it in LightBlue. And the number changed.

This BLE method is unsuitable to this application. This code wants to beam a signal, no more. And the whole thing screeches to a halt. Without the BLE code, this will run forever. With the BLE code, it runs maybe two minutes and stops generating random numbers. I don’t know if the program quits, but it stops working.

I’ll look for a peripheral code to ping changes, but I haven’t gotten a central to work yet- maybe. I get serial gibberish, and it’s continuous. This is in two programs so far, so something consistently giving me serial gibberish in these central scripts. I haven’t checked to see whether something will display on OLED.

BUT, I just got Peltier units, and they work.

Your previous serial gibberish was caused by the code you posted setting 9600 baud at one point, then setting 115200 later in the code. You had the monitor running at 9600.

Do a search for “.begin(” and see how many hits you get.

Cheers,
Norm.

Thanks- I was only aware of one Serial.begin.
If I use the Clean function, would that get caught?

No. The clean option simply removes all compiler generated files. It takes you back to just the sources.

It doesn’t check the code for anything. You would need a lint type application for that, and even then, it would only pick up incorrectness in usage, syntax errors etc, not, unfortunately, programmer induced errors.

Cheers,
Norm.

1 Like