wvmarle:
ADC = Analog Digital Converter.
Kindergarten kids don't know what electricity is, what voltage are, etc. So sorry, that's way too much of explaining to do. At the very least you need high school physics level.
Actually, I don't even understand your schematic and your code is too much cut to make much sense of it. There's a voltage divider of 1M / 23.8 Ohm, giving you a near-Vcc voltage on A1, and there's another that's done with a pot.
For your voltage measurement get a voltage divider of say 100k + 22k, 100k to Vcc, 22k to GND, mid point to an analog pin. Rather high values but you don't want to drain your battery through it. This gives you 0.77V on any of your analog pins when your battery is full (4.3V) and about 0.49V when empty (2.7V).
Then enable the internal reference (do look up the documentation of how the ADC works!) and you can measure this.
I was kidding of course: my studies on the matter is a little bit rusty. Plus english is not my Language, so I get some difficulties with achronyms.
Here is my full code: there are oter things going on, like the pot: it has nothing to do with the voltage divider.
#include <RH_ASK.h>
#include <TM1637Display.h>
// iniszializzazioni pin
#define potPin A0
#define batPin A1
#define btnPin 2
#define txPin 13
#define pinCLK 8
#define pinDIO 9
// inizializzazione librerie RadioHead per trasmissione
RH_ASK tx(2000,1,txPin,1,true);
//inizializzazione libreria TM1637Display del display
TM1637Display digitalDisplay(pinCLK, pinDIO);
#define SEG_A 0b00000001 // A
#define SEG_B 0b00000010 // ---
#define SEG_C 0b00000100 // F | | B
#define SEG_D 0b00001000 // -G-
#define SEG_E 0b00010000 // E | | C
#define SEG_F 0b00100000 // ---
#define SEG_G 0b01000000 // D
//-- per scrittura “OFF”
// ogni dgit dell’array displayOFF (quindi quale segmento è acceso di ogni digit) è definito con OR-bitwise “|” dei segmenti interessati
const uint8_t displayOFF[] = {
0b00000000,
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_A | SEG_E | SEG_F | SEG_G, // F
SEG_A | SEG_E | SEG_F | SEG_G, // F
};
// inizializzazione comandi throttle
int motJuice = 0; int motJuice_perc;
int btnState; String msgStr;
// inizializzazioni partitore di tensione
double VPinRead;
double Vbatt; double Vbatt_perc; double VbattMin = 3200, VbattMax = 4200; // [mV]
double R1 = 23.6; double R2 = 1462000;
void setup() {
Serial.begin(9600);
pinMode(btnPin,INPUT);
pinMode(batPin,INPUT);
pinMode(potPin,INPUT);
digitalDisplay.setBrightness(0x0f); //! Sets the brightness – 0x0f =7, massima lumx;
// SETTAGGI DI TRASMISSIONE
if (!tx.init()){ Serial.println("init failed");}
}
void loop() {
// ----------------------- COMANDI THROTTLE ---------------------------//
btnState = digitalRead(btnPin);
if(btnState == LOW) // se tasto non premuto comando da throttle
{
motJuice = analogRead(potPin);
motJuice = map(motJuice,0,1023,2000,1100);
motJuice_perc = map(motJuice,1250,2000,0,100);
if(motJuice_perc < 5){motJuice_perc = 0;} // filtro
digitalDisplay.showNumberDec( motJuice_perc , false); // stampa percentuale throttle
} else if(motJuice_perc > 0 && btnState == HIGH) // se tasto premuto, comando -> motore spento
{
motJuice = 0;
digitalDisplay.setSegments(displayOFF);
}
// --------------------------------------------------------------------//
//
// ------------------- MISURA BATTERIA con PARTITORE di TENSIONE ---------------------//
VPinRead = map(analogRead(batPin),0,1023,VbattMin,VbattMax); //tra 0 e 5000 mV massima tensione del pin
// in mV per avere + cifre significative
Vbatt = (VPinRead/1000)*((R2/(R1+R2))) ; // diviso 1000 per riportarlo in V da mV.
// * 3.85/3.91 - fattore di calibrazione a misura multimetro
Vbatt_perc = map(Vbatt*1000,VbattMin,VbattMax,0,100);
//Serial.print(Vbatt);Serial.print("\t"); Serial.println(analogRead(batPin));
if(motJuice_perc == 0 && btnState == HIGH) // se throttle == 0 e bottone premuto, stampa misura batt
{
displayBATperc((int) (Vbatt_perc));
}
// ----------------------------------------------------------------------------------//
//TRASMISSIONE
// costituzione messaggio di invio (1 digit)
msgStr=String(motJuice); // conversione in stringa
char msg[msgStr.length()+1]; //dichiarazione char
msgStr.toCharArray(msg,msgStr.length()+1); //conversione in char
tx.send((uint8_t *)msg, msgStr.length()+1); // invio del messaggio
tx.waitPacketSent(); // attesa conclusione invio
// -------- STAMPA MESSAGGIO DI INVIO
/*
Serial.print("Sent: ");Serial.print(msgStr);Serial.print(" - ");
for (int i=0; i<= msgStr.length(); i++)
{
Serial.print(char(msg[i]));
}
Serial.println();
*/
// --------------------------------- //
/*if(Serial.available()>0)
{
msg = Serial.readString();
motJuice = msg.toInt();
}*/
}
void displayBATperc(int Vbatt_perc)
{
uint8_t displayMSG[] = { 0xff, 0xff, 0xff, 0xff }; // 0xff = 255 – tutto acceso?
// digit 0 : stampa "b"
displayMSG[0] = SEG_F | SEG_E | SEG_C | SEG_D | SEG_G;
// primo digit - cenitnaia
if(Vbatt_perc >= 100)
{
int centinaia = (int) (Vbatt_perc/100);
displayMSG[1] = digitalDisplay.encodeDigit((int) (Vbatt_perc/100));
}
else
{
displayMSG[1] = 0b00000000; //digit delle centinaia tutto spento
}
// secondo digit - decine
if(Vbatt_perc >= 10)
{
int decine = (int) (Vbatt_perc/10) - ((int) (Vbatt_perc/100)) * 10;
displayMSG[2] = digitalDisplay.encodeDigit(decine);
}
else
{
displayMSG[1] = 0b00000000; //digit delle centinaia tutto spento
}
// terzo digit - unità
int unita = (int) Vbatt_perc - ((int) (Vbatt_perc/10)) * 10;
displayMSG[3] = digitalDisplay.encodeDigit(unita);
digitalDisplay.setSegments(displayMSG);
}
Why R2>>>R1 is wrong? don't I want to measure Vcc ? Also in this way there wouldn't be power consumption over the R2, i guess.
Also please help me get this straight and correct me if I'm wrong: You want to lower the voltage (0.77V - 0.49V) over R2, lowering R2 value. Then you want to compare it with a lower reference value (internal reference of 2.56V for ATMEGA328P).