using level indicator to read battery charge

View previous topic View next topic Go down

using level indicator to read battery charge

Post by belart on Thu May 05, 2016 11:47 pm

Hi,
I am using Arduino Uno, HC-06 Bluetooth module & RoboRemo App to control speed of 5 VDC motor and to read the battery level.
I used the source code and wiring from your site to control the motor, but being a noob, I'm at a loss as to how to do the battery level indicator code and wiring.
I am using a voltage divider with an analog input to pin A0.  

I have two sample programs below, one to identify the BT module and the other for the voltage info. I don't know how to combine them together, and along with the motor speed code : I appreciate your help

#define bluetooth Serial
char cmd[100];
int cmdIndex;

void exeCmd() {
//if(strcmp(cmd, "some cmd")==0) doSomething();
}

void setup() {
delay(500); // wait for HC-06 to start
Serial.begin(9600);    
bluetooth.begin(9600); // HC-06 default baud is 115200
cmdIndex = 0;
}

void loop() {

if(bluetooth.available()) {    
  char c = (char)bluetooth.read();
 
  if(c=='\n') {
    cmd[cmdIndex] = 0;
    exeCmd();  // execute the command
    cmdIndex = 0; // reset the cmdIndex
  } else {      
    cmd[cmdIndex] = c;
    if(cmdIndex<99) cmdIndex++;
  }  
}
 // String st= ("BatteryPcnt" + "\n");
  bluetooth.print("BatteryPcnt  0  \n");
  delay(1000);

}

Code for voltage divider below:

#include <SPI.h>
#include <MySensor.h>

int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point

MySensor gw;
unsigned long SLEEP_TIME = 900000; // sleep time between reads (seconds * 1000 milliseconds)
int BatteryPcnt = 0;

void setup()
{
// use the 1.1 V internal reference
#if defined(__AVR_ATmega2560__)
analogReference(INTERNAL1V1);
#else
analogReference(INTERNAL);
#endif
gw.begin();

// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Battery Meter", "1.0");
}

void loop()
{
// get the battery Voltage
int sensorValue = analogRead(BATTERY_SENSE_PIN);
#ifdef DEBUG
Serial.println(sensorValue);
#endif

// 1M, 470K divider across battery and using internal ADC ref of 1.1V
// Sense point is bypassed with 0.1 uF cap to reduce noise at that point
// ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
// 3.44/1023 = Volts per bit = 0.003363075
float batteryV = sensorValue * 0.003363075;
int batteryPcnt = sensorValue / 10;

#ifdef DEBUG
Serial.print("Battery Voltage: ");
Serial.print(batteryV);
Serial.println(" V");

Serial.print("Battery percent: ");
Serial.print(batteryPcnt);
Serial.println(" %");
#endif

if (oldBatteryPcnt != batteryPcnt) {
// Power up radio after sleep
gw.sendBatteryLevel(batteryPcnt);
oldBatteryPcnt = batteryPcnt;
}
gw.sleep(SLEEP_TIME);
}

belart

Posts : 2
Join date : 2016-05-05

View user profile

Back to top Go down

Re: using level indicator to read battery charge

Post by Admin on Fri May 06, 2016 3:00 pm

Hi,
Here is the code:
Code:

char cmd[100];
int cmdIndex;
long t1=0, t2=0;

void exeCmd() {
  //if(strcmp(cmd, "some cmd")==0) doSomething();
}

void setup() {
  delay(500); // wait for HC-06 to start
  Serial.begin(9600); // HC-06 default baud is 115200
  cmdIndex = 0;

  // use the 1.1 V internal reference
  #if defined(__AVR_ATmega2560__)
  analogReference(INTERNAL1V1);
  #else
  analogReference(INTERNAL);
  #endif
 
}

void loop() {

  if(Serial.available()) {   
    char c = (char)Serial.read();
 
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {     
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    } 
  }

  t2 = millis();

  if(t2 > t1+100) { // every 100 ms
    t1 = t2;
    int adcValue = analogRead(0);
    float voltage = adcValue * 0.003363075;
    int batPcnt = adcValue/10;
    Serial.print((String)"BatteryPcnt " + batPcnt + "\n");
    // level indicator id is "BatteryPcnt", case sensitive, without ""
  }
 
  // DO NOT ADD DELAY HERE
  // (RECEIVING COMMANDS WILL STOP WORKING)

}

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: using level indicator to read battery charge

Post by belart on Fri May 06, 2016 11:48 pm

Thank you kindly... I will upload it and let you know if other questions come up.

belart

Posts : 2
Join date : 2016-05-05

View user profile

Back to top Go down

Re: using level indicator to read battery charge

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum