add volume log fix for linear potentiometer

This commit is contained in:
interfisch 2022-09-24 16:41:47 +02:00
parent fe83cff969
commit 1696e1f04d
1 changed files with 36 additions and 6 deletions

View File

@ -36,6 +36,9 @@ void changeRelaisByNumberTopic(uint8_t pn, String pTopicPrefix, uint8_t pindex,
void setRelaisByNumber(uint8_t pn, String pTopicPrefix, uint8_t pnumber, bool pstate, void (*psetXChannel) (uint8_t, boolean)); void setRelaisByNumber(uint8_t pn, String pTopicPrefix, uint8_t pnumber, bool pstate, void (*psetXChannel) (uint8_t, boolean));
float getSetVolume(); float getSetVolume();
uint8_t getIndex(uint8_t pn, String pTopic); uint8_t getIndex(uint8_t pn, String pTopic);
float mapPotiToVolume(int p);
int mapVolumeToPoti(float v);
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
#define LEDPIN 9 //PB1 = D9 = Pin15 #define LEDPIN 9 //PB1 = D9 = Pin15
#define NUMLED 9 #define NUMLED 9
@ -97,7 +100,7 @@ uint16_t srbits=0;
#include <Encoder.h> #include <Encoder.h>
Encoder volEnc(PIN_ENCA,PIN_ENCB); Encoder volEnc(PIN_ENCA,PIN_ENCB);
float encoderMultiplier=4.0; float encoderMultiplier=0.4;
int volEncVel=0; int volEncVel=0;
//Servo stuff //Servo stuff
@ -115,6 +118,7 @@ int _motormove;
#define POTIFILTER 0.5 //0 to 1. 1 means old value stays forever #define POTIFILTER 0.5 //0 to 1. 1 means old value stays forever
#define MAX_MOTOR_PWM 255 //0 to 255. Maximum pwm to output #define MAX_MOTOR_PWM 255 //0 to 255. Maximum pwm to output
float poti_volume_set=0; //set value as volume (poti is linear, volume is log). [0.0, 100.0]
int poti_set; //set value, initial value will be read from poti int poti_set; //set value, initial value will be read from poti
int poti_read=0; //read value from poti int poti_read=0; //read value from poti
boolean poti_reachedposition=true; //set to true if position reached. after that stop turning boolean poti_reachedposition=true; //set to true if position reached. after that stop turning
@ -253,6 +257,7 @@ void setup() {
poti_set=analogRead(PIN_POT); poti_set=analogRead(PIN_POT);
poti_volume_set=round(mapPotiToVolume(poti_set));
#ifdef DEBUG #ifdef DEBUG
Serial.println("Ready"); Serial.println("Ready");
@ -402,7 +407,9 @@ void loop() {
if (volEncVel!=0){ //knob moved if (volEncVel!=0){ //knob moved
switch(menu_mode) { switch(menu_mode) {
case 0: //volume case 0: //volume
poti_set+=volEncVel*encoderMultiplier; //change poti set value poti_volume_set+=volEncVel*encoderMultiplier; //change poti set value
poti_volume_set=constrain(poti_volume_set, 0.0,100.0);
poti_set=mapVolumeToPoti(poti_volume_set);
poti_set=constrain(poti_set, POT_MIN,POT_MAX); poti_set=constrain(poti_set, POT_MIN,POT_MAX);
poti_reachedposition=false; poti_reachedposition=false;
flag_publishCurrentSetVolume=true; flag_publishCurrentSetVolume=true;
@ -640,9 +647,9 @@ void callback(char* topic, byte* payload, unsigned int length) {
if (String(topic).equals("audiomixer/volume/set")){ if (String(topic).equals("audiomixer/volume/set")){
float _floatvalue = spayload.toFloat(); float _floatvalue = spayload.toFloat();
_floatvalue=constrain(_floatvalue,0.0,100.0); poti_volume_set=constrain(_floatvalue,0.0,100.0); //set new poti position
poti_set=constrain(map(_floatvalue,0.0,100.0,POT_MIN,POT_MAX),POT_MIN,POT_MAX); //set new poti position poti_set=mapVolumeToPoti(poti_volume_set);
poti_reachedposition=false; //aim for new position poti_reachedposition=false; //aim for new position
publishCurrentSetVolume(); publishCurrentSetVolume();
@ -818,5 +825,28 @@ void setRelaisByNumber(uint8_t pn, String pTopicPrefix, uint8_t pnumber, bool ps
float getSetVolume() { float getSetVolume() {
return map(poti_set,POT_MIN,POT_MAX, 0.0,100.0); //get percentage from set poti value //return map(poti_volume_set,POT_MIN,POT_MAX, 0.0,100.0); //get percentage from set poti value
return poti_volume_set;
} }
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#define STEEPNESS 10.0 //has to be a float!
int mapVolumeToPoti(float v) {
v=v/100.0;
//volume is [0.,100.]
//poti is [POTI_MIN,POTI_MAX]
float pout=(pow(STEEPNESS,v)-1)/(STEEPNESS-1);
return mapfloat(pout,0.0,1.0, POT_MIN, POT_MAX);
}
float mapPotiToVolume(int p) {
//volume is [0.,100.]
//poti is [POTI_MIN,POTI_MAX]
float pmapped=mapfloat((float)p,POT_MIN,POT_MAX,0.0,1.0);
return (log((STEEPNESS-1)*( pmapped+(1/(STEEPNESS-1)) ))/log(STEEPNESS))*100.0;
}