Low battery indicator

Discussion about other hardware (including other Pi-like boards)
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 » Thu Nov 24, 2016 12:12 pm

sotasystems wrote:When do you want your battery low LED to come on (Voltage value)? I would recommend 10-15 minutes before reaching 3.20 Volts, but it's up to you.
That would probably the best option yes. So I have enough time to save any given game and to shut down the pie in time (I'm not using a safe shutdown switch).

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 » Thu Nov 24, 2016 12:24 pm

winnetouch wrote:
sotasystems wrote:When do you want your battery low LED to come on (Voltage value)? I would recommend 10-15 minutes before reaching 3.20 Volts, but it's up to you.
That would probably the best option yes. So I have enough time to save any given game and to shut down the pie in time (I'm not using a safe shutdown switch).

Code: Select all

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

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(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(1) == LOW) {
    Keyboard.press(keycode_arr[0]);
  }
  else {
    Keyboard.release(keycode_arr[0]);
  }
  
  if (digitalRead(0) == 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(3) == LOW) {
    Keyboard.press(keycode_arr[3]);
  }
  else {
    Keyboard.release(keycode_arr[3]);
  }
  
  if (digitalRead(4) == LOW) {
    Keyboard.press(keycode_arr[4]);
  }
  else {
    Keyboard.release(keycode_arr[4]);
  }
  
  if (digitalRead(5) == LOW) {
    Keyboard.press(keycode_arr[5]);
  }
  else {
    Keyboard.release(keycode_arr[5]);
  }
  
  if (digitalRead(6) == LOW) {
    Keyboard.press(keycode_arr[6]);
  }
  else {
    Keyboard.release(keycode_arr[6]);
  }
  
  if (digitalRead(7) == LOW) {
    Keyboard.press(keycode_arr[7]);
  }
  else {
    Keyboard.release(keycode_arr[7]);
  }
  
  if (digitalRead(8) == LOW) {
    Keyboard.press(keycode_arr[8]);
  }
  else {
    Keyboard.release(keycode_arr[8]);
  }
  
  if (digitalRead(9) == LOW) {
    Keyboard.press(keycode_arr[9]);
  }
  else {
    Keyboard.release(keycode_arr[9]);
  }
  
  if (digitalRead(10) == LOW) {
    Keyboard.press(keycode_arr[10]);
  }
  else {
    Keyboard.release(keycode_arr[10]);
  }
  
  if (digitalRead(11) == 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 » Thu Nov 24, 2016 12:57 pm

sotasystems wrote:

Code: Select all

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

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(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(1) == LOW) {
    Keyboard.press(keycode_arr[0]);
  }
  else {
    Keyboard.release(keycode_arr[0]);
  }
  
  if (digitalRead(0) == 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(3) == LOW) {
    Keyboard.press(keycode_arr[3]);
  }
  else {
    Keyboard.release(keycode_arr[3]);
  }
  
  if (digitalRead(4) == LOW) {
    Keyboard.press(keycode_arr[4]);
  }
  else {
    Keyboard.release(keycode_arr[4]);
  }
  
  if (digitalRead(5) == LOW) {
    Keyboard.press(keycode_arr[5]);
  }
  else {
    Keyboard.release(keycode_arr[5]);
  }
  
  if (digitalRead(6) == LOW) {
    Keyboard.press(keycode_arr[6]);
  }
  else {
    Keyboard.release(keycode_arr[6]);
  }
  
  if (digitalRead(7) == LOW) {
    Keyboard.press(keycode_arr[7]);
  }
  else {
    Keyboard.release(keycode_arr[7]);
  }
  
  if (digitalRead(8) == LOW) {
    Keyboard.press(keycode_arr[8]);
  }
  else {
    Keyboard.release(keycode_arr[8]);
  }
  
  if (digitalRead(9) == LOW) {
    Keyboard.press(keycode_arr[9]);
  }
  else {
    Keyboard.release(keycode_arr[9]);
  }
  
  if (digitalRead(10) == LOW) {
    Keyboard.press(keycode_arr[10]);
  }
  else {
    Keyboard.release(keycode_arr[10]);
  }
  
  if (digitalRead(11) == LOW) {
    Keyboard.press(keycode_arr[11]);
  }
  else {
    Keyboard.release(keycode_arr[11]);
  }
}

Thank you. I'll try it out right away :)

I did notice that pin 3 is commented twice though. Also... I don't see pin A0 anywhere in the script. As far as I can follow the script all pins are defined as outputs. 15 is output and A1 is input for the voltage (correct me if I'm wrong). But I have one of the buttons wired to A0 analog pin. Will it still work?

EDIT: Tried it and the buttons don't work :cry: Then I reuploaded the old sketch (the one that did work) and it also doesn't work... I don't know what's wrong. I'll have to check the wiring angain but everything is hotglued together so I doubt it's the wiring's fault. I may have to reconfigure the keyboard but I don't know why that would jutst stop working :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 » Fri Nov 25, 2016 2:18 am

Hmm this sketch works fine for me, but I see some modifications need to be made for your buttons to be matched up.

Can you tell me how your buttons are wired up to the leonardo? E.g. which Pin of the arduino goes to which button? And also, are you using a common ground on your buttons or common anode?
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 » Fri Nov 25, 2016 9:36 am

I'm using a commom ground with helders controller + audio board.

Sorry for the weird diagram but I flipped the sparkfun board upside down when I soldered the pins. But like I said. Need to check the thing insde out because now the old script doesn't work either. I don't have the GB0 with me now. When I'll be at my folks place I'll check it once over and report back.
wiring-01.jpg
wiring-01.jpg (2.17 MiB) Viewed 11484 times

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 » Fri Nov 25, 2016 2:11 pm

winnetouch wrote:I'm using a commom ground with helders controller + audio board.
Sorry for the weird diagram but I flipped the sparkfun board upside down when I soldered the pins. But like I said. Need to check the thing insde out because now the old script doesn't work either. I don't have the GB0 with me now. When I'll be at my folks place I'll check it once over and report back.
It's all good. I have modified the code for your button mappings and it is working for me (couldn't test A0 though, but in theory should work too):

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(A0, 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(A0) == 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 » Sat Nov 26, 2016 12:21 pm

sotasystems wrote:
It's all good. I have modified the code for your button mappings and it is working for me (couldn't test A0 though, but in theory should work too):
I connected the arduino to my computer and tried the keyboard out that way. It works... Except for the L1 button.

Also, i found out that one of the USB wires got torn off so that's why it didn't work with the pi.

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 » Sat Nov 26, 2016 3:58 pm

winnetouch wrote:
sotasystems wrote:
It's all good. I have modified the code for your button mappings and it is working for me (couldn't test A0 though, but in theory should work too):
I connected the arduino to my computer and tried the keyboard out that way. It works... Except for the L1 button.

Also, i found out that one of the USB wires got torn off so that's why it didn't work with the pi.
Does R1 (connected to A0) work, though? That's what I couldn't test myself yet, L1 (connected to pin 10) works for me, 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 » Sun Nov 27, 2016 12:26 am

sotasystems wrote:Does R1 (connected to A0) work, though? That's what I couldn't test myself yet, L1 (connected to pin 10) works for me, though.
Sorry. The diagram is wrong. I switched the L1 and R1 buttons. L1 is connected to A0 and it does not work.

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:34 am

winnetouch wrote:
sotasystems wrote:Does R1 (connected to A0) work, though? That's what I couldn't test myself yet, L1 (connected to pin 10) works for me, though.
Sorry. The diagram is wrong. I switched the L1 and R1 buttons. L1 is connected to A0 and it does not work.
I see. I will work on it ;)
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!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest