which pin

I have an arduino mega 2560 plus a backpack from coolcomponents.

I have attached a jumper wire from the status pin of battery backpack to analog in a0

i am trying to read this value but am getting random results (i think)

what pin number is a0? I currently have the following code

batteryStatus = analogRead(0);
Serial.write(batteryStatus);
delay(1000);

is this correct? I am guessing not as to me pin 0 is an rx pin.

cheers in adavnce

Are you sure that the status pin is really an analog output (battery voltage)? It may be a digital output (good/bad). If that is the case, reading it using an analog pin won’t get you any useful information.

Post a link to the backpack, if you are not sure.

As far as which pin is analog pin 0, the analog pins are clearly marked on the Arduinos.

no I am not sure
the battery pack/shield is this one http://www.coolcomponents.co.uk/catalog/product_info.php?cPath=50_76&products_id=467

as for the pins being clearly marked, yes they are as “a0, a1, a2, aN, a15” but analogRead takes an int parameter, so which one am i reading?

analogRead(0) reads from the pin marked A0. analogRead(3) reads from the pin labeled A3.

Excellent thanks for that, now all i need to know is if it is a analog or digital pin (i think from research it might me analog) and how to send that back to the user via Serial

ideas??

This page suggests the status pin is tied directly to the battery terminal: http://antipastohw.blogspot.com/2008/05/lithium-backpack-charging-it.html

When you say random, how random are your values?

So coming back into my java app (simply does inputStream.read()) I get

batt:68
batt:66
batt:63
batt:109
batt:63
batt:69
batt:67
batt:66
batt:61
batt:60
batt:255
batt:65
batt:191

as you can see most are around the 60’s then you get one way out, but i would expect the battery to be a little more consistent than that would I??

code for mega is thus

#include <Servo.h>

Servo gearbox;
Servo steering;
Servo lights;

int gears[] = {125, 55, 100, 150};
int RIGHT = 30;
int CENTER = 80;
int LEFT = 130;

int UP = 0;
int DOWN = 70;

int batteryStatus = 0;

char ch;

void setup() {
  
  Serial.begin(9600);
  
  steering.attach(15);
  gearbox.attach(14);
  lights.attach(16);
  
  lights.write(DOWN);
  steering.write(CENTER);
  
  pinMode(13, OUTPUT);
  
  pinMode(A0, INPUT);
}

void loop() {
  if(Serial.available()) {
    ch = Serial.read();
    digitalWrite(13, HIGH);
    
    switch(ch) {
      case '1':
        // 1st Gear
        gearbox.write(gears[1]);
        break;
      case '2':
        // 2nd Gear
        gearbox.write(gears[2]);
        break;
      case '3':
        // 3rd Gear
        gearbox.write(gears[3]);
        break;
      case '0':
        // Neutral Gear
        gearbox.write(gears[0]);
        break;
      case 'a':
        steering.write(LEFT);
        break;
      case 'd':
        steering.write(RIGHT);
        break;
      case 'w':
        steering.write(CENTER);
        break;
      case 'q':
        lights.write(UP);
        break;
      case 'z':
        lights.write(DOWN);
        break;
    }
  }
  else {
    delay(1000);
    digitalWrite(13, LOW);
    batteryStatus = analogRead(A0);
    Serial.write(batteryStatus);
  }
}

output from serial monitor is

=<<<=1<<^]_^___``a`abba^[UKCD<====><==>=>>==>>==>>=>>=>=>=>=>=======>====<==<<

The serial monitor will not be of use in your sketch.

Since you are using Serial.write and not Serial.print. Serial.write sends the value pass to it as 1’s and 0’s. Serial.print sends the ASCII code for the characters of the value passed to it.

If you put a multimeter on your battery, does the voltage fluctuate?

don't know, dont own a multimeter :$

Serial.write() doesn't deal well with integer input. It expects a byte. Either divide the output from analogRead() by 4 (1023/4 = 255), store in a byte, and Serial.write() that byte, or use Serial.print() to print the value as a string of characters.

don't know, dont own a multimeter

Strongly recommend you invest:

http://www.maplin.co.uk/search?criteria=multimeter

Compared to ten or twenty years ago, very very cheap.