Motor driver need help to convert from potentiometer to hall effect sensor

Hi i know about nothing how to program in here, i am bad at grammar bare with me 8D
using an arduino mega 2560

problem start here instead of using a potentiometer i am using a hall effect sensor throttle
like on ebike on pin A0 (with is throttle)
but a hall effect throttle is 1v to 4v instead of the 0v to 5v like a pot do
so there alway some pwoer given to the motor making pwm noise and waste power
probably need a dead zone of some sort ?

what the code do is having a soft acceleration controlled with a pot independent of the throttle asked
like having a super slow acceleration even when you give 100% throttle
it decelerate instantly if the throttle is releashed

included a picture with basic wiring instead of D3 pinout i use D5 pinout i use since i burned the D3 pinout.
the code is taken from a french dude who shared his code to all
i aslo speak french if you need the comment in the code translated i can'T figure out most of what it mean but can try :stuck_out_tongue:

Thanks for any help

/*potentiometer x2  for motor controller.
  Version 1.4, Incroyables Expériences */

void setup() // Boucle d'initialisation.
{
    pinMode(5, OUTPUT);  // Broche 3 réglée comme sortie numérique.
}

float r=0; // Initialisation du rapport cyclique réel à 0.
float s=30; // Initialisation de la souplesse de conduite.
int a=0; // Initialisation du rapport cyclique demandé à 0.
int b=0; // Initialisation de la mémoire du rapport cyclique demandé.
int c=700; // default 70 safety if the throttle  is floored too fast

void loop() { // Boucle principale
  
  analogWrite(5,r/1023*254); // Création du PWM.
  a=analogRead(0); // Lecture du rapport cyclique demandé.
  s=analogRead(1); // Lecture de la souplesse de conduite demandée.

  if(a>b+c){ // Vérification d'une accélération trop brutale (d'origine volontaire ou non).
    digitalWrite(5,LOW); // Arrêt immédiat du moteur.
    while(a>c){ // Attente d'un rapport cyclique très faible pour redémarrer.
      a=analogRead(0); // Lecture continue du rapport cyclique demandé.
    }
  }
  b=a; // Mémorisation du rapport cyclique demandé.

  if(a>r){ // Vérification de la croissance du rapport cyclique demandé.
    delay(20); // Délai responsable de la souplesse de conduite.
    r=r+s/20+2; // Calibrage empirique de la souplesse de conduite.
  }
  if(a<r){ // Vérification de la décroissance du rapport cyclique demandé.
    r=a; // Diminution immédiate du rapport cyclique réel le cas échéant.
  }
  
}

buder5:
but a hall effect throttle is 1v to 4v instead of the 0v to 5v like a pot do

This code:

  analogWrite(5,r/1023*254); // Création du PWM.

is mapping the analogRead value from 0-1023 to the 0-254 passed to analogWrite(). So you can simply adjust that code to the effective range of your analogRead values.

pert:
This code:

  analogWrite(5,r/1023*254); // Création du PWM.

is mapping the analogRead value from 0-1023 to the 0-254 passed to analogWrite(). So you can simply adjust that code to the effective range of your analogRead values.

befor i try and risking breaking something
should i edit that

  analogWrite(5,r/1023*254); // Création du PWM.

to that

  analogWrite(5,r/0-1023*0-254); // Création du PWM.

or simply changing the number 1024254 to something else ? example 900154
because i don't know what each number do
Thanks

Edit1: if i put the *254 to *300 the output voltage increase to 4v instead of 3v but the voltage crash if i edit to *350

Edit2: the idle voltage is still 0.80v enought to make noise if i change the number to lower or hight it change the idle voltage but aslo the maximum voltage when the throttle is at 100%
if the idle is lower the max is lower

Edit3: i need the idle voltage to be absolute 0v

i think i need a dead zone on the input

The map() function is your friend. Also look up constrain() because map() can run off the end of the desired output values.

  int analogOutputValue = constrain(map(r, 100, 900, 0, 255), 0,255);
  analogWrite(5, analogOutputValue);

Pick a better name for r.

Pick a name for analog pin A5 and use that name everywhere in your code instead of "5".

Adjust the 100 and 900 until you get the dead-zone and response that you want.

i added that to the void loop seem to work and fine tuned as much as i can

   int analogOutputValue = constrain(map(r, 110, 1155, 0, 355), 0,255);
  analogWrite(5, analogOutputValue);

but the idle voltage still not absolute 0v its 0.57v (before was 0.85v).
it did lower with the dead zone
if i increase (110) to a ridiculus amount the minimum is 0.25v and make a dead zone controll
with the thumb throttle

got any idea to make it 0 ? when idle
the throttle halleffect output 0.8v-0.9v when idle

Edit1: when i mess with the number -22, 360 i got the idle voltage lower to 0.28v the 360 increase the max voltage when at 100% slighty better but still not 0v at idle
if i edit the -22 to an even more negative value it dosent get lower than 0.25v

Edit2:name the stuff the way you want pinout5 is GasOut if i try to name the stuff i will just make a mess Thanks

  int analogOutputValue = constrain(map(r, 110, 1155, -22, 360), 0,255);

the full code look like this now

/* PWM pour moteur à courant continu avec réglage de la souplesse de conduite (2 potentiomètres).
  Version 1.4, Incroyables Expériences */

void setup() // Boucle d'initialisation.
{
    pinMode(5, OUTPUT);  // Broche 3 réglée comme sortie numérique.
}
float r=0; // Initialisation du rapport cyclique réel à 0.
float s=30; // Initialisation de la souplesse de conduite.
int a=0; // Initialisation du rapport cyclique demandé à 0.
int b=0; // Initialisation de la mémoire du rapport cyclique demandé.
int c=700; // default 70 safety if the throttle  is floored too fast


void loop() { // Boucle principale
  int analogOutputValue = constrain(map(r, 110, 1155, -22, 360), 0,255);
  analogWrite(5, analogOutputValue);
  /*analogWrite(5,r/1023*254); // Création du PWM.*/
  a=analogRead(0); // Lecture du rapport cyclique demandé.
  s=analogRead(1); // Lecture de la souplesse de conduite demandée.

  if(a>b+c){ // Vérification d'une accélération trop brutale (d'origine volontaire ou non).
    digitalWrite(5,LOW); // Arrêt immédiat du moteur.
    while(a>c){ // Attente d'un rapport cyclique très faible pour redémarrer.
      a=analogRead(0); // Lecture continue du rapport cyclique demandé.
    }
  }
  b=a; // Mémorisation du rapport cyclique demandé.

  if(a>r){ // Vérification de la croissance du rapport cyclique demandé.
    delay(20); // Délai responsable de la souplesse de conduite.
    r=r+s/20+2; // Calibrage empirique de la souplesse de conduite.
  }
   if(a<r){ // Vérification de la décroissance du rapport cyclique demandé.
    r=a; // Diminution immédiate du rapport cyclique réel le cas échéant.
  }
  
}

i am more than a newbie at programing but i will mess with the number
Thanks

What is your minimum and maximum ADC readings? let's assume 204 and 818:

void loop()
{
  int adcMin = 204, adcMax = 818;
  int val = constrain(analogRead(0),adcMin,adcMax);
  analogWrite(5,(float)(val - adcMin) / (adcMax - adcMin) * 255);
}

Or:

analogWrite(5,255 * (val - adcMin) / (adcMax - adcMin));

Ghost Edit2:ignore this (this code do nothing since i don't know what i am suppose todo)

but any analog reading on the a0 to a5 leave 5 checkmark in the serial monitor each time the code run with basic analog reading example
any tutorial i searched leave me with no luck
tried with the analog pin jumped to a ground still only checkmark in the serial monitor

unless my smraza UNO R3 MEGA2560 is a bad one or dosent comunicate properly with my pc ?

Edit1:nvm had a brain fart forgot to remove the other part i added fine tuning the new code test of #Outsider wait 10 mintue after the edit

Edit2: it seem that its 180 min 875 max i get the best result
the PinOut.5 a voltage range of 0.25v idle and 4.02v when at 100% throttle
wondering if the usb powered arduino is not recieving enought power through usb to get the
0v-5v on the PinOut.5

Edit3: using @Outsider code part instead of the other prevent the soft acceleration control to work
making it instantly going to the asked throttle instead of the slow acceleration
just saying i don't know if that was wanted or not

  int adcMin = 180, adcMax = 873;
  int val = constrain(analogRead(0),adcMin,adcMax);
  analogWrite(5,(float)(val - adcMin) / (adcMax - adcMin) * 255);

What is your minimum and maximum ADC readings?
What is your ADC reading when the pot is turned all the way to the LEFT? All the way to the RIGHT? In the CENTER?

i get checkmark only in the serial moniter screen when i try

but 180 min 873 max give best result

int adcMin = 180, adcMax = 873;

Edit1: dont care anymore my controller got burned >_> but thanks to MorganS he the best and everyone else who tried to help me

buder5:
i get checkmark only in the serial moniter screen when i try

but 180 min 873 max give best result

int adcMin = 180, adcMax = 873;

Edit1: dont care anymore my controller got burned >_> but thanks to MorganS he the best and everyone else who tried to help me

Edit1: 1 year later everything work