Low battery indicator

Discussion about other hardware (including other Pi-like boards)
User avatar
sotasystems
Posts: 160
Joined: Sun Oct 09, 2016 4:56 am
Location: Germany, 127.0.0.1
Has thanked: 95 times
Been thanked: 93 times
Contact:

Re: Low battery indicator

Post by sotasystems » Sun Nov 27, 2016 12:36 am

Does this work for you?:

Code: Select all

#include <Keyboard.h>
int BatFlag = 0;
int BatLow = 650;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long Interval = 1000;
int keycode_arr[] =
{
  216,  // LEFT    Pin 0
  218,  // RIGHT   Pin 3
  217,  // DOWN    Pin 2
  215,  // UP      Pin 1
  118,  // 'v'     Pin 10
  119,  // 'w'     Pin A0
  120,  // 'x'     Pin 7
  121,  // 'y'     Pin 6
  46,  // .        Pin 4
  32,  // +        Pin 5
  97,  // 'a'      Pin 9
  98,  // 'b'      Pin 8
};

void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  // pinMode(16, INPUT_PULLUP); (Not used)
  // pinMode(14, INPUT_PULLUP); (Not used)
  pinMode(15, OUTPUT);
  analogReference(INTERNAL);
  pinMode(A1, INPUT);
  Keyboard.begin();
}

void loop() {
  currentMillis = millis();
  if(currentMillis - previousMillis >= Interval) {
  previousMillis = currentMillis;
    if (analogRead(A1) <= BatLow) {
      BatFlag = 1;
    }
    else {
      BatFlag = 0;
    }
    if (BatFlag == 1) {
      digitalWrite(15, LOW);
    }
    else {
      digitalWrite(15, HIGH);
    }
  }
  
  if (digitalRead(0) == LOW) {
    Keyboard.press(keycode_arr[0]);
  }
  else {
    Keyboard.release(keycode_arr[0]);
  }
  
  if (digitalRead(3) == LOW) {
    Keyboard.press(keycode_arr[1]);
  }
  else {
    Keyboard.release(keycode_arr[1]);
  }
  
  if (digitalRead(2) == LOW) {
    Keyboard.press(keycode_arr[2]);
  }
  else {
    Keyboard.release(keycode_arr[2]);
  }
  
  if (digitalRead(1) == LOW) {
    Keyboard.press(keycode_arr[3]);
  }
  else {
    Keyboard.release(keycode_arr[3]);
  }
  
  if (digitalRead(10) == LOW) {
    Keyboard.press(keycode_arr[4]);
  }
  else {
    Keyboard.release(keycode_arr[4]);
  }
  
  if (digitalRead(18) == LOW) {
    Keyboard.press(keycode_arr[5]);
  }
  else {
    Keyboard.release(keycode_arr[5]);
  }
  
  if (digitalRead(7) == LOW) {
    Keyboard.press(keycode_arr[6]);
  }
  else {
    Keyboard.release(keycode_arr[6]);
  }
  
  if (digitalRead(6) == LOW) {
    Keyboard.press(keycode_arr[7]);
  }
  else {
    Keyboard.release(keycode_arr[7]);
  }
  
  if (digitalRead(4) == LOW) {
    Keyboard.press(keycode_arr[8]);
  }
  else {
    Keyboard.release(keycode_arr[8]);
  }
  
  if (digitalRead(5) == LOW) {
    Keyboard.press(keycode_arr[9]);
  }
  else {
    Keyboard.release(keycode_arr[9]);
  }
  
  if (digitalRead(9) == LOW) {
    Keyboard.press(keycode_arr[10]);
  }
  else {
    Keyboard.release(keycode_arr[10]);
  }
  
  if (digitalRead(8) == LOW) {
    Keyboard.press(keycode_arr[11]);
  }
  else {
    Keyboard.release(keycode_arr[11]);
  }
   
}
Ladies and gentlemen, I would like to inform you that I am currently moving very far away, and therefore I am very busy.

UPDATE: I am still alive! My ISP is setting up my internet on the 19th of January at my new place, explaining my offline-ness.

If you write me a PM, I will very likely take some time to respond.

Also, my 2nd build will soon go on, so if you've been following it, please stay tuned! :)

Many thanks for your understanding!

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Low battery indicator

Post by winnetouch » Sun Nov 27, 2016 11:13 pm

sotasystems wrote:I see. I will work on it ;)
Thank you very much :) You are a lifesaver (well not a life saver but I still greatly appreciate you'r help :)).

Also. I tried depleting the battery again just to see if the circuit works. The red LED lights up but it's very faint. Is it suppose to light up gradually or just light up as soon as the battery is at a certain point?

User avatar
sotasystems
Posts: 160
Joined: Sun Oct 09, 2016 4:56 am
Location: Germany, 127.0.0.1
Has thanked: 95 times
Been thanked: 93 times
Contact:

Re: Low battery indicator

Post by sotasystems » Mon Nov 28, 2016 1:38 am

winnetouch wrote:
sotasystems wrote:I see. I will work on it ;)
Thank you very much :) You are a lifesaver (well not a life saver but I still greatly appreciate you'r help :)).

Also. I tried depleting the battery again just to see if the circuit works. The red LED lights up but it's very faint. Is it suppose to light up gradually or just light up as soon as the battery is at a certain point?
I just noticed that the code is an old version I modified all along, which will derp around with the LED. :oops:.

This is the one for you.

Code: Select all

#include <Keyboard.h>
int BatFlag = 0;
int BatLow = 650;
int LED = 670;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long Interval = 1000;
int keycode_arr[] =
{
  216,  // LEFT    Pin 0
  218,  // RIGHT   Pin 3
  217,  // DOWN    Pin 2
  215,  // UP      Pin 1
  118,  // 'v'     Pin 10
  119,  // 'w'     Pin A0
  120,  // 'x'     Pin 7
  121,  // 'y'     Pin 6
  46,  // .        Pin 4
  32,  // +        Pin 5
  97,  // 'a'      Pin 9
  98,  // 'b'      Pin 8
};

void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  // pinMode(16, INPUT_PULLUP); (Not used)
  // pinMode(14, INPUT_PULLUP); (Not used)
  pinMode(15, OUTPUT);
  analogReference(INTERNAL);
  pinMode(A1, INPUT);
  Keyboard.begin();
}

void loop() {
  currentMillis = millis();
  if(currentMillis - previousMillis >= Interval) {
  previousMillis = currentMillis;
    if (analogRead(A1) <= BatLow) {
      BatFlag = 1;
    }
    if (analogRead(A0) >= LED) {
      BatFlag[0] = 0;
    }
    if (BatFlag == 1) {
      digitalWrite(15, LOW);
    }
    else {
      digitalWrite(15, HIGH);
    }
  }
  
  if (digitalRead(0) == LOW) {
    Keyboard.press(keycode_arr[0]);
  }
  else {
    Keyboard.release(keycode_arr[0]);
  }
  
  if (digitalRead(3) == LOW) {
    Keyboard.press(keycode_arr[1]);
  }
  else {
    Keyboard.release(keycode_arr[1]);
  }
  
  if (digitalRead(2) == LOW) {
    Keyboard.press(keycode_arr[2]);
  }
  else {
    Keyboard.release(keycode_arr[2]);
  }
  
  if (digitalRead(1) == LOW) {
    Keyboard.press(keycode_arr[3]);
  }
  else {
    Keyboard.release(keycode_arr[3]);
  }
  
  if (digitalRead(10) == LOW) {
    Keyboard.press(keycode_arr[4]);
  }
  else {
    Keyboard.release(keycode_arr[4]);
  }
  
  if (digitalRead(18) == LOW) {
    Keyboard.press(keycode_arr[5]);
  }
  else {
    Keyboard.release(keycode_arr[5]);
  }
  
  if (digitalRead(7) == LOW) {
    Keyboard.press(keycode_arr[6]);
  }
  else {
    Keyboard.release(keycode_arr[6]);
  }
  
  if (digitalRead(6) == LOW) {
    Keyboard.press(keycode_arr[7]);
  }
  else {
    Keyboard.release(keycode_arr[7]);
  }
  
  if (digitalRead(4) == LOW) {
    Keyboard.press(keycode_arr[8]);
  }
  else {
    Keyboard.release(keycode_arr[8]);
  }
  
  if (digitalRead(5) == LOW) {
    Keyboard.press(keycode_arr[9]);
  }
  else {
    Keyboard.release(keycode_arr[9]);
  }
  
  if (digitalRead(9) == LOW) {
    Keyboard.press(keycode_arr[10]);
  }
  else {
    Keyboard.release(keycode_arr[10]);
  }
  
  if (digitalRead(8) == LOW) {
    Keyboard.press(keycode_arr[11]);
  }
  else {
    Keyboard.release(keycode_arr[11]);
  }
   
}
Also, what resistor did you use for the LED? It sounds to me that it is very high.
Ladies and gentlemen, I would like to inform you that I am currently moving very far away, and therefore I am very busy.

UPDATE: I am still alive! My ISP is setting up my internet on the 19th of January at my new place, explaining my offline-ness.

If you write me a PM, I will very likely take some time to respond.

Also, my 2nd build will soon go on, so if you've been following it, please stay tuned! :)

Many thanks for your understanding!

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Low battery indicator

Post by winnetouch » Mon Nov 28, 2016 3:18 am

I am using 100kΩ as you suggested. Or is that meant to be only for the divider and I should use a smaller resistor for the LED. I just assumed they all need to be the same because of the diagram :P.

User avatar
sotasystems
Posts: 160
Joined: Sun Oct 09, 2016 4:56 am
Location: Germany, 127.0.0.1
Has thanked: 95 times
Been thanked: 93 times
Contact:

Re: Low battery indicator

Post by sotasystems » Mon Nov 28, 2016 4:38 am

winnetouch wrote:I am using 100kΩ as you suggested. Or is that meant to be only for the divider and I should use a smaller resistor for the LED. I just assumed they all need to be the same because of the diagram :P.
:lol: No, 100 kΩ is way too high for the LED. The 100kΩ was only meant for the divider. The resistor for the LED is usualy calculated with it's working voltage and current consumption. Most of the 5mm red LED's like I have in mind should be OK with something around 300Ω, though.
Ladies and gentlemen, I would like to inform you that I am currently moving very far away, and therefore I am very busy.

UPDATE: I am still alive! My ISP is setting up my internet on the 19th of January at my new place, explaining my offline-ness.

If you write me a PM, I will very likely take some time to respond.

Also, my 2nd build will soon go on, so if you've been following it, please stay tuned! :)

Many thanks for your understanding!

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Low battery indicator

Post by winnetouch » Mon Nov 28, 2016 12:29 pm

Well... :oops:

I changed out the resistor and now the LED shines bright.

But when I tried to compile the second code you pasted I get this error:

Code: Select all

Arduino: 1.6.12 (Mac OS X), Board: "SparkFun Pro Micro, ATmega32U4 (5V, 16 MHz)"

/Users/viktorhochtl/Desktop/test/test.ino: In function 'void loop()':
test:53: error: invalid types 'int[int]' for array subscript
       BatFlag[0] = 0;
                ^
exit status 1
invalid types 'int[int]' for array subscript

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I'm guessing this line has something to do with the LED. But A0 is the L1 button. So how does that work?

Code: Select all

    if (analogRead(A0) >= LED) {
      BatFlag[0] = 0;
    }
Also is this OK? I don't have a pin 18 :)

Code: Select all

pinMode(18, INPUT_PULLUP);

User avatar
sotasystems
Posts: 160
Joined: Sun Oct 09, 2016 4:56 am
Location: Germany, 127.0.0.1
Has thanked: 95 times
Been thanked: 93 times
Contact:

Re: Low battery indicator

Post by sotasystems » Mon Nov 28, 2016 12:41 pm

winnetouch wrote:Well... :oops:

I changed out the resistor and now the LED shines bright.
Meaning it works as it should?

winnetouch wrote:I'm guessing this line has something to do with the LED. But A0 is the L1 button. So how does that work?
Oh no! I messed up again
Image

I'm so sorry man! :oops: Here, this does work:

Code: Select all

#include <Keyboard.h>
int BatFlag = 0;
int BatLow = 650;
int LED = 670;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long Interval = 1000;
int keycode_arr[] =
{
  216,  // LEFT    Pin 0
  218,  // RIGHT   Pin 3
  217,  // DOWN    Pin 2
  215,  // UP      Pin 1
  118,  // 'v'     Pin 10
  119,  // 'w'     Pin A0
  120,  // 'x'     Pin 7
  121,  // 'y'     Pin 6
  46,  // .        Pin 4
  32,  // +        Pin 5
  97,  // 'a'      Pin 9
  98,  // 'b'      Pin 8
};

void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  // pinMode(16, INPUT_PULLUP); (Not used)
  // pinMode(14, INPUT_PULLUP); (Not used)
  pinMode(15, OUTPUT);
  analogReference(INTERNAL);
  pinMode(A1, INPUT);
  Keyboard.begin();
}

void loop() {
  currentMillis = millis();
  if(currentMillis - previousMillis >= Interval) {
  previousMillis = currentMillis;
    if (analogRead(A1) <= BatLow) {
      BatFlag = 1;
    }
    if (analogRead(A1) >= LED) {
      BatFlag = 0;
    }
    if (BatFlag == 1) {
      digitalWrite(15, LOW);
    }
    else {
      digitalWrite(15, HIGH);
    }
  }
  
  if (digitalRead(0) == LOW) {
    Keyboard.press(keycode_arr[0]);
  }
  else {
    Keyboard.release(keycode_arr[0]);
  }
  
  if (digitalRead(3) == LOW) {
    Keyboard.press(keycode_arr[1]);
  }
  else {
    Keyboard.release(keycode_arr[1]);
  }
  
  if (digitalRead(2) == LOW) {
    Keyboard.press(keycode_arr[2]);
  }
  else {
    Keyboard.release(keycode_arr[2]);
  }
  
  if (digitalRead(1) == LOW) {
    Keyboard.press(keycode_arr[3]);
  }
  else {
    Keyboard.release(keycode_arr[3]);
  }
  
  if (digitalRead(10) == LOW) {
    Keyboard.press(keycode_arr[4]);
  }
  else {
    Keyboard.release(keycode_arr[4]);
  }
  
  if (digitalRead(18) == LOW) {
    Keyboard.press(keycode_arr[5]);
  }
  else {
    Keyboard.release(keycode_arr[5]);
  }
  
  if (digitalRead(7) == LOW) {
    Keyboard.press(keycode_arr[6]);
  }
  else {
    Keyboard.release(keycode_arr[6]);
  }
  
  if (digitalRead(6) == LOW) {
    Keyboard.press(keycode_arr[7]);
  }
  else {
    Keyboard.release(keycode_arr[7]);
  }
  
  if (digitalRead(4) == LOW) {
    Keyboard.press(keycode_arr[8]);
  }
  else {
    Keyboard.release(keycode_arr[8]);
  }
  
  if (digitalRead(5) == LOW) {
    Keyboard.press(keycode_arr[9]);
  }
  else {
    Keyboard.release(keycode_arr[9]);
  }
  
  if (digitalRead(9) == LOW) {
    Keyboard.press(keycode_arr[10]);
  }
  else {
    Keyboard.release(keycode_arr[10]);
  }
  
  if (digitalRead(8) == LOW) {
    Keyboard.press(keycode_arr[11]);
  }
  else {
    Keyboard.release(keycode_arr[11]);
  }
   
}
winnetouch wrote:Also is this OK? I don't have a pin 18 :)

Code: Select all

pinMode(18, INPUT_PULLUP);
Yes, that is A0. ;)
Ladies and gentlemen, I would like to inform you that I am currently moving very far away, and therefore I am very busy.

UPDATE: I am still alive! My ISP is setting up my internet on the 19th of January at my new place, explaining my offline-ness.

If you write me a PM, I will very likely take some time to respond.

Also, my 2nd build will soon go on, so if you've been following it, please stay tuned! :)

Many thanks for your understanding!

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Low battery indicator

Post by winnetouch » Mon Nov 28, 2016 1:30 pm

The L1 button still doesn't work. I'm starting to suspect that the button is busted. When I'll have a bit more time I'll solder another button to the wires to check that out and see if it'll work.

To see if the LED works I'll have to deplet the battery again. Looks like the old code really did mess with the LED because it lit up no matter what the battery level :P.

So I'll report back on the LED. But I'm sure the code is fine and it's just something with my wiring.

Thank you for all your help man it's really greatly appreciated :).

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Low battery indicator

Post by winnetouch » Mon Nov 28, 2016 2:22 pm

Well... The LED is a bit dodgy :P

It lights up randomly and stays on for a couple of seconds then goes out. Also it lights up when the GB0 starts up and then turns of also.

User avatar
Camble
Posts: 885
Joined: Thu May 05, 2016 2:31 am
Location: Scotland
Has thanked: 269 times
Been thanked: 488 times

Re: Low battery indicator

Post by Camble » Mon Nov 28, 2016 3:16 pm

If your battery is almost at the low battery threshold, your LED will light when there is extra load, like during bootup.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest