#1using analogWrite give proper reading but disabling give same result as using analogWrite (give proper reading)
//Beta FREQ
// SetPinFrequencySafe(Motor, frequency); //set frequency and speed
// SetPinFrequencySafe(SpeedoPin, frequency2); //set frequency and speed
#2 no idea how to setup highter freq without the library since that is its reason to be loaded
image here is the hall sensor wiring
if i try to use the hall sensor without capacitor its a no-no never work nomater the wiring i tried like 10 way to wire this up and some sketch
unless its the pullup that never worked for me
this is the calculation from hertz to rpm
int RPMmulti = Freq*30; Hertz to RPM 60 60/(magnet number)= your rpm. i use 2 magnet so 30
Serial.print(" RPM: ");
Serial.print(RPMmulti);
int MapRPMmulti = map(RPMmulti, 0, 3000, 0, 255);
int Speedo = constrain(MapRPMmulti,0,255);
pwmWrite(SpeedoPin,Speedo);
pwmWrite(Motor, insideTHR);
latest full code
/* PWM pour moteur à courant continu avec réglage de la souplesse de conduite (2 potentiomètres).
Version 1.4, Incroyables Expériences */
/*its now in v1.7 from buder :D*/
#include <PWM.h>
#define MainPeriod 100
unsigned long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration=0; // accumulates pulse width
volatile unsigned int pulsecount=0;
volatile unsigned long previousMicros=0;
float THRint =0; //THRint requested throttle when thumb removed from throttle // Initialisation du rapport cyclique réel à 0.
float ACCpot=30; //before was (S) base Accel Initialisation // Initialisation de la souplesse de conduite.
int THRpot=0; //Throttle poteniometer // Initialisation du rapport cyclique demandé à 0.
int THRmem=0; //THRmem // Initialisation de la mémoire du rapport cyclique demandé.
int Safety=7000; // default 70 safety if the throttle is floored too fast increase number to disable/lower the safety sensibility (at 700 is clearly disabled)
int32_t frequency= 20000;
int32_t frequency2= 20000;
int Motor = 9; //THRout is throttle to connect to transistor/driver/led/ebike controller
int SpeedoPin = 3; //Speedo Pin
int delayVar=20;//delay for acceleration speed to be used with rpm later
void setup() //setup loop // Boucle d'initialisation.
{
Serial.begin(9600);
attachInterrupt(0, myinthandler, RISING);
InitTimersSafe();
pinMode(Motor, OUTPUT); // pin5 set as THRout (throttle out)
pinMode(SpeedoPin, OUTPUT); // pin10 set as to speedo mosfet
//Beta FREQ
// SetPinFrequencySafe(Motor, frequency); //set frequency and speed
// SetPinFrequencySafe(SpeedoPin, frequency2); //set frequency and speed
}
void loop() { // main loop // Boucle principale
int MapTHRint = map(THRint, 110, 1200, -22, 340);
int insideTHR = constrain(MapTHRint,0,255);
THRpot=analogRead(A0); //throttle pot reading // Lecture du rapport cyclique demandé.
ACCpot=analogRead(A1); //acceleration threshold pot reading // Lecture de la souplesse de conduite demandée.
if(THRpot>THRmem+Safety){ //check for brutale THRpot request (aka flooring the throttle too fast) // Vérification d'une accélération trop brutale (d'origine volontaire ou non).
digitalWrite(Motor,LOW); //imediatly stop if THRout above Safety // Arrêt immédiat du moteur.
while(THRpot>Safety){ //waiting for THRpot to be lower than safety to restart // Attente d'un rapport cyclique très faible pour redémarrer.
THRpot=analogRead(0); // Lecture continue du rapport cyclique demandé.
}
}
THRmem=THRpot; // Mémorisation du rapport cyclique demandé.
if(THRpot>THRint){ //checking for throttle increase
// Vérification de la croissance du rapport cyclique demandé.
delay(delayVar);//to be used later its 20
// delay(20); // Délai responsable de la souplesse de conduite.
THRint=THRint+ACCpot/20+2; // Calibrage empirique de la souplesse de conduite.
}
if(THRpot<THRint){ //checking if you removed your thumb from the throttle // Vérification de la décroissance du rapport cyclique demandé.
THRint=THRpot; //stop power being sent if above true(aka thumb removed from throttle) // Diminution immédiate du rapport cyclique réel le cas échéant.
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= MainPeriod)
{
previousMillis = currentMillis;
// need to bufferize to avoid glitches
noInterrupts();
unsigned long _duration = duration;
unsigned long _pulsecount = pulsecount;
duration = 0; // clear counters
pulsecount = 0;
interrupts();
float Freq = 1e6 / float(_duration); //Duration is in uSecond so it is 1e6 / T
Freq *= _pulsecount; // calculate F
// output time and frequency data to RS232
Serial.print(" Frequency: ");
Serial.print(Freq);
Serial.print("Hz ");
int RPMmulti = Freq*30; Hertz to RPM 60 60/(magnet number)= your rpm. i use 2 magnet so 30
Serial.print(" RPM: ");
Serial.print(RPMmulti);
int MapRPMmulti = map(RPMmulti, 0, 3000, 0, 255);
int Speedo = constrain(MapRPMmulti,0,255);
Serial.print(" Speedo: ");
Serial.println(Speedo);
// analogWrite(10,Speedo);
pwmWrite(SpeedoPin,Speedo);
pwmWrite(Motor, insideTHR);
}
}
void myinthandler() // interrupt handler
{
unsigned long currentMicros = micros();
duration += currentMicros - previousMicros;
previousMicros = currentMicros;
pulsecount++;
}