if you make a low pass filter to convert a PWM to a voltage, the output depends on the R and C values of your filter. It will take a lot of guesswork to find the appropriate values for R and C. You can use one of the analog inputs to measure the throttle voltage (by connecting your PWM output to it) and adjust your PWM accordingly.
edit
d8veh is correct, connect the output of your low pass filter to an analog input, not the PWM pin.
I suggest you test with 5 kOhm variable resistor and 0.1microF capacitor.
shouldn't they add a sink resistor to the circuit diagram? if there is no load on the output of the low pass filter, the capacitor will charge up until it reaches Vcc.
It's all in that link that I gave you above. You only have to read it!i desperately need a low pass filter formula then...
int panel = A0; //810 panel voltage input pin
int thr = 5; //throttle signal addition wire with a diode on throttle
int THR1 = 90; // preset output preset voltage. 1.9 volts 8 bit
int THR2 = 130; // 2.5 volts
int THR3 = 185; // 4.8 volts
int PNLLO = 430; // constant signal from control panel 2 volt. 10 bit
int PNLMD = 580; // 3 volt. 10 bit
int PNLHI = 729; // 4 volt. 10 bit
int magnet = 8; //PEDELEC SENSOR Magnet Count to show correct rpm on serial monitor
int val = 0;
//Varibles used for calculations
int ticks = 0, Speed = 0;
int hallsensor = 12; //The Hall effect sensor pin
//Defines the structure for multiple fans and their dividers
typedef struct{
char fantype;
unsigned int fandiv;
} fanspec;
//set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 1; // from original code
//fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 0; // from original code
void pickrpm ()
//This is the interrupt subroutine that increments ticks counts for each HES response.
{ ticks++; }
//This is the setup function where the serial port is initialised,
//and the interrupt is attached and other pins initialized.
void setup()
{
pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(0, pickrpm, RISING);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
void loop ()
{
ticks = 0; // Make ticks zero before starting interrupts.
interrupts(); // or use sei(); to Enables interrupts
delay (1000); //Wait 1 second
noInterrupts(); // or use cli(); to Disable interrupts
Speed = ((ticks * magnet *1.875)/fanspace[fan].fandiv); // rpm calculation
Serial.print (Speed, DEC);
Serial.print (" RPM\r\n");
val = analogRead(panel); //
// Following code is for RGB LED.
if ((Speed > 0) &&(val <= PNLLO)){
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
analogWrite(thr, THR1);
}
else if ((Speed > 0)&&((val > PNLMD)&&(val < PNLHI))){
digitalWrite(11, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
analogWrite(thr, THR2);
}
if ((Speed > 0)&&(val > PNLHI)){
digitalWrite(9, LOW);
digitalWrite(10, HIGH );
digitalWrite(11, HIGH);
analogWrite(thr, THR3);
}
if (Speed == 0){
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
analogWrite(thr, 0);
int valu = 0;
int panel = 11; //panel voltage input pin
int thr = 6; //throttle signal addition wire with a diode on throttle
int THR1 = 90; // preset output preset voltage. 1.9 volts 8 bit
int THR2 = 130; // 2.5 volts
int THR3 = 185; // 4.8 volts
int PNLLO = 420; // constant signal from control panel 2 volt. 10 bit
int PNLMD = 630; // 3 volt. 10 bit
int PNLHI = 800; // 4 volt. 10 bit
float rotationTime;
float distanceTravelledPerSecond;
float wheelCircumference = 2; // Circumference of the wheel
float RPM;
float speeds;
float maximumSpeed;
unsigned int distanceTravelled;
//unsigned int averageSpeed; // not needed for now
unsigned long revoloutions = 0;
unsigned long startRotation = 0;
unsigned long endRotation = 0;
unsigned long timeInSeconds;
unsigned long timeInMilliseconds;
unsigned long lastMillis;
boolean reedSwitchState;
boolean lastReedSwitchState;
const int reedSwitchPin = 2;
int buttonState;
int val;
int val2;
void DoTimeAndDistanceCalcs()
{
revoloutions ++;
endRotation = millis();
rotationTime = endRotation - startRotation;
startRotation = endRotation;
RPM = (600 / rotationTime);
distanceTravelled = revoloutions * wheelCircumference / 100;
distanceTravelledPerSecond = RPM * wheelCircumference;
distanceTravelledPerSecond = distanceTravelledPerSecond / 1000 * 60;
speeds = distanceTravelledPerSecond;
if (speeds > maximumSpeed)
maximumSpeed = speeds;
timeInSeconds = millis();
timeInSeconds = timeInMilliseconds / 1000;
//averageSpeed = (distanceTravelled / timeInSeconds); //not needed for now
}
void setup() {
pinMode(reedSwitchPin, INPUT);
Serial.begin(9600);
buttonState = digitalRead(reedSwitchPin);
// lcd.begin(16,4);
}
void loop() {
valu = analogRead(panel);
val = digitalRead(reedSwitchPin);
delay(10);
val2 = digitalRead(reedSwitchPin);
if (val == val2) {
if (val != buttonState) {
lastReedSwitchState = reedSwitchState;
reedSwitchState = val;
if ((lastReedSwitchState == HIGH) && (reedSwitchState == LOW) && (valu <= PNLLO)) {
DoTimeAndDistanceCalcs();
analogWrite(thr, THR1);
// lcd.setCursor(8,3);
}
else {
DoTimeAndDistanceCalcs();
delay(1500);
// there must be some conditions to stop sending voltage to output pin if hall sensor is HIGH for too long.
analogWrite(thr, 0);
}
// else if ((lastReedSwitchState == LOW) && (reedSwitchState == HIGH) && (valu >= PNLHI)){
// DoTimeAndDistanceCalcs();
// analogWrite(thr, THR3); this part wont work for a reason...
}
}
buttonState = val ;
Serial.println(digitalRead(reedSwitchPin)); // test
}
int val = 0;
int panel = 11; //panel voltage input pin 2 3 4 volts preset.
int thr = 6; //voltage output
int THR1 = 90; // output preset voltage. approx 1.7 volts 8 bit. RC filter for PWM to DC.
int THR2 = 130; // 2.7 volts
int THR3 = 185; // 4.3 volts
int PNLLO = 420; // constant signal from control panel 2 volt. 10 bit
int PNLMD = 630; // 3 volt. 10 bit
int PNLHI = 800; // 4 volt. 10 bit
int ledPin = 13; //integrated led
int hallsensor = 2; // hall sensor pin.
int firsttime = 1;
unsigned long startTime;
unsigned long hallTime;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(hallsensor, INPUT);
pinMode(hallsensor, INPUT_PULLUP);
digitalWrite(hallsensor, HIGH);
Serial.begin(9600);
}
void loop()
{
if ((digitalRead(hallsensor) == LOW) && (panel <= PNLLO)) { //if panel gives 2 volts.
if (firsttime == 1) {
startTime = millis();
firsttime = 0;
analogWrite(thr, THR1); // arduino has 1.7 volts output
delay (400);
} else
{ if ((digitalRead(hallsensor) == LOW) && (panel > PNLLO) && (panel < PNLHI)) { //if panel gives 3 volts.
if (firsttime == 1) {
startTime = millis();
firsttime = 0;
analogWrite(thr, THR2); // arduino has 1.7 volts output
delay (400);
} else
{ if ((digitalRead(hallsensor) == LOW) && (panel >= PNLHI)) { //if panel gives 4 volts.
if (firsttime == 1) {
startTime = millis();
firsttime = 0;
analogWrite(thr, THR3); // arduino has 1.7 volts output
delay (400);
}
}
}
}
}
hallTime = millis() - startTime;
if (hallTime >= 1) {
Serial.print("Time: ");
Serial.print(hallTime);
Serial.print(" milliseconds ");
Serial.print(int(hallTime / 1000));
Serial.println(" seconds");
}
if (hallTime > 500) { // if hall sensor matches to the magnet for more dan 0.5 sec
digitalWrite(ledPin, HIGH);
analogWrite(thr, 0); // cut power
}
} //***
else if (firsttime == 0) { // if no pulse on hall sensor
firsttime = 1;
Serial.println("Time: 0 milleseconds; 0 seconds");
digitalWrite(ledPin, LOW);
delay (400);
analogWrite(thr, 0); // more than delay, cut power.
}
}