I need to round an integer to 2 decimal places because it is an analog input that is measuring voltage. The integer i am getting is many dps and the problem is when i output this integer over bluetooth to an app on my phone (to display the battery voltage) the digits are changing so fast in the app that it gets confused and tries to display them all at once. I can't add a a delay to slow it down because i need some other code in the loop to be running quickly.
here is my code:
#include <SoftwareSerial.h>// import the serial library
#include <Servo.h>
SoftwareSerial Bluetooth(10, 11); // RX, TX
int BluetoothData; // the data given from Computer
Servo ESC;
void setup() {
// put your setup code here, to run once:
Bluetooth.begin(9600);
Serial.begin(9600);
Serial.println("Bluetooth On");
ESC.attach(9);
}
void loop() {
// put your main code here, to run repeatedly:
if (Bluetooth.available()){
BluetoothData=Bluetooth.read();
ESC.write(BluetoothData);
Serial.println(BluetoothData);
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Bluetooth.println(voltage, DEC);
}
}
basically the purpose is to control an electric longboard with my phone and have a readout on my phone with the status of one of the cells of the lipo so i know when the battery is running out
An int doesn't have any decimal places. Integers are whole numbers.
Multiply your float number by 100. Turn it into an int. to truncate it. Turn it back into a float and divide by 100.
have tried the code below but am getting 3.000000000 repeatedly now (im using a lipo so voltage is going to be from 3.50 to 4.20)
#include <SoftwareSerial.h>// import the serial library
#include <Servo.h>
SoftwareSerial Bluetooth(10, 11); // RX, TX
int BluetoothData; // the data given from Computer
Servo ESC;
void setup() {
// put your setup code here, to run once:
Bluetooth.begin(9600);
Serial.begin(9600);
Serial.println("Bluetooth On");
ESC.attach(9);
}
void loop() {
// put your main code here, to run repeatedly:
if (Bluetooth.available()){
BluetoothData=Bluetooth.read();
ESC.write(BluetoothData);
Serial.println(BluetoothData);
}
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
int rounded = voltage * 100;
float voltageRounded = rounded / 100;
Bluetooth.println(voltageRounded, DEC);
Also i need some way of only sending a voltage reading via bluetooth every 20 seconds or so but i cant use a delay because the other part of the code needs to run quickly
Okay thanks, had a look and came up with this:
#include <SoftwareSerial.h>// import the serial library
#include <Servo.h>
SoftwareSerial Bluetooth(10, 11); // RX, TX
int BluetoothData; // the data given from Computer
Servo ESC;
long previousMillis = 0;
long interval = 20000;
void setup() {
// put your setup code here, to run once:
Bluetooth.begin(9600);
Serial.begin(9600);
Serial.println("Bluetooth On");
ESC.attach(9);
}
void loop() {
// put your main code here, to run repeatedly:
if (Bluetooth.available()){
BluetoothData=Bluetooth.read();
ESC.write(BluetoothData);
Serial.println(BluetoothData);
}
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > millis()) {
previousMillis = currentMillis;
if (voltage <= 3.5)
Serial.println("Low Battery");
else
Serial.println("Battery Fine");
}
}
but i am not getting any text coming up in the serial monitor except the " bluetooth on" bit...?
Ah thanks, silly mistake!! got it all working!!!
thanks for everyones help!!