hello everybody
I'm working on an Arduino Pro Mini clone (here)
among other things I have a potentiometer connected to A0 where I read the values for knob-like purposes. You can see the schematics in the image attached.
Everything works fine, but the analogRead(A0) gets weird values within weird range: from about 30 to about 200. Also if I don't touch the knob, the reading slowly drifts to lower values :o
Of course I checked the connections with a multimeter and the voltage readings... everything seems fine.
Any suggestions ?
Here is the code
#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 R1 = 23; double R2 = 1000000;
void setup() {
Serial.begin(9600);
pinMode(btnPin,INPUT);
pinMode(batPin,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);
motJuice = analogRead(potPin);//map(analogRead(potPin),65,160,0,100);
Serial.println(motJuice);
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,3200,3850); //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(analogRead(batPin),0,1023,0,100);
//Serial.println(Vbatt);
if(motJuice_perc == 0 && btnState == HIGH) // se throttle == 0 e bottone premuto, stampa misura batt
{
displayBATperc(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
/*
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
displayMSG[0] = SEG_F | SEG_E | SEG_C | SEG_D | SEG_G;
if(Vbatt_perc/100 == 0){displayMSG[1] = 0b00000000;}//tutto spento
else {displayMSG[1] = digitalDisplay.encodeDigit(Vbatt_perc/100);}
displayMSG[2] = digitalDisplay.encodeDigit(Vbatt_perc/10);
displayMSG[3] = digitalDisplay.encodeDigit(Vbatt_perc/1);
digitalDisplay.setSegments(displayMSG);
}