• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Problem with Serial.parseint

Q&A Forum › Category: ESP32 › Problem with Serial.parseint
0 Vote Up Vote Down
Steen Heide asked 3 years ago

Dear forum
I am trying to make a remote controlled car, using Bluetoothserial, and the App “Bluetooth electronics” from keuwlsoft. A lot of the program is running ok but the part with Serial.parseInt, wont work it is returning 0.
This is the original part from keuwlsoft, LED brightness

 //Process any info coming from the bluetooth serial link
  if (Serial.available()){
    BluetoothData=Serial.read(); //Get next character from bluetooth
    if(BluetoothData=='R') Red_value=Serial.parseInt(); //Read Red value
    if(BluetoothData=='G') Green_value=Serial.parseInt(); //Read Green Value
    if(BluetoothData=='B') Blue_value=Serial.parseInt(); //Read Blue Value
  }
in the program above this string is send from the mobile  R number(0..255) A
R are used to select. the number is used to set the brightness, as i see A is ignored 
As the program should be used on a arduino uno the number is used in analogWrite(pin,number).

when i converted the program to ESP32 i used — ledcWrite(led1, x);
I cant see what i am doing wrong, any one can help?

// Program with bluetooth control of a car
// ********************************************************************************
// Modified from SerialToSerialBT in the ESP-32 example packet
// builded on the example in the  ESP-32 serial bluetooth and led brightness
// from Keuw.com
// Modified by Steen.
// Dato 25/1 2022
// ********************************************************************************
// here data is read from ESP32 via bluetooth
// The unit communicating is a mobile phone and the app Bluetooth electronics
// the program can read two analog values A-værdi-A og B-værdi-B
// There are tre different binary values Cc Dd og Ee
#include “BluetoothSerial.h”
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
const int led1=23;
const int led2=17;
const int led3=22;
int tegn=0;
BluetoothSerial SerialBT;
int x=0;
int y=0;
int z=0;
void setup() {
ledcSetup(0, 2000, 8);
ledcAttachPin(led3, 0);
Serial.begin(115200);
pinMode (led1,OUTPUT);
pinMode (led2,OUTPUT);
pinMode (led3,OUTPUT);
SerialBT.begin(“ESP32test”); //Bluetooth device name
Serial.println(“The device started, now you can pair it with bluetooth!”);
}
void loop() {
// if (Serial.available()) {
// SerialBT.write(Serial.read());} // læsning fra den serielle bt
if (SerialBT.available()) {
tegn=(SerialBT.read());
if (tegn==’R’) x=Serial.parseInt(); // Right motor R–Value–r
if (tegn==’L’) y=Serial.parseInt(); // Left motor L–Value–l
if (tegn==’C’) digitalWrite(led2,HIGH);
if (tegn==’c’) digitalWrite(led2,LOW);
if (tegn==’D’) digitalWrite(led1,HIGH);
if (tegn==’d’) digitalWrite(led1,LOW);
if (tegn==’E’) digitalWrite(led3,HIGH);
if (tegn==’e’) digitalWrite(led3,LOW);
ledcWrite(led1, x);
Serial.write (x);
// Serial.write(SerialBT.read());
}
delay(20);
}

Question Tags: Serial bluetooth parseInt
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
You’re not using the ledcWrite function in the right way.
You must assign a PWM channel.
Check the following tutorial first, it explains how to use the ledcWrite function properly.

ESP32 PWM with Arduino IDE (Analog Output)


Regards,
Sara
 

0 Vote Up Vote Down
Steen Heide answered 3 years ago

Dear Sara 
I have done as in the tutorial, in fact it was there i found the ledcWrite(led1, x);
and the pwm is both setup “ledcSetup(0, 2000, 8);” and Attached “ledcAttachPin(led3, 0);”
i have had different blink sequences with diffrent duty cycle 
The real problem is “if (tegn==’R’) x=Serial.parseInt(); “
who is responding on a message:    R125r
       where R i read first and used as a selection number
      125 is a number between 0 and 255
      I think “r” is ignored but is a part of the string from the Bluetooth Electronics — app
does anyone have a clue on, how i get the number out of the string as a integer ?
Best regards steen

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
 
You are using PWM wrong.
the lecWrite() function accepts as the first argument the PWM channel, not the PWM pin as you’re doing.
 
To get the number out of a string, you can use the substring method(): https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/
To convert a string to a number: https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/
 
I hope this helps.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.