# How to implement my formulas in my code?

Hello!

I’m new to Arduino, but for school I have to make a 4 wire fan that is adjustable. I want to make it that way that I give in the frequentie of the pulse, and the Arduino transers it in to a value between 0 and 255. I already did some math and I have the formulas to get the value. But where do I have to implement these?

Formulas:
Calabration formula → f(Hz)=5,1143∙U-2,2012
Into this → U=(f+2,2012)/5,1143
Calculate when motor starts → 2,2012/5,1143=0,43 ← motor starts at 0,43V
Between 0,43V en 9,991V the Arduino goes in 255 steps → (9,991-0,43)/255=0,037V each step
The formula of your speed is → ((U-0,43))/0,037V=x(in steps between 0 and 255)

``````float fanSpeed   = 0;  // A0 op de Arduino / Loper van de Potmeter
float fanPWM     = 3;  // Pin D3 op de Arduino. / Instellen van toerental 0/255
int   fanSensor  = 8;  // Pin D8 op de Arduino / Uitlezen van toerental 0/1023
int   Puls       = 4;  // Hier het aantal pulsen per omwenteling van de fan. Te vinden in specs van de fan.

int Speed = x; // Var die nodig is om de snelheid in te stellen van 0 tot 255

unsigned long SensorPulsTijd;

void setup() {
Serial.begin(9600);
pinMode(fanSensor, INPUT);
pinMode(fanSpeed, INPUT);
digitalWrite(fanSensor, HIGH);
}

void loop() {
analogWrite(3, Speed); // RPM snelheid instellen
SensorPulsTijd = pulseIn(fanSensor, LOW);
double frequency = 1000000/SensorPulsTijd;  // 1000000 microsec / SensorPulsTijd = aantal microsec per puls.

Serial.print("Pulsduur in microsec.     = ");
Serial.println(SensorPulsTijd);
Serial.print("Rotatietijd in microsec.  = ");
Serial.println(SensorPulsTijd*Puls);
Serial.print("Frequentie in Herz        = ");
Serial.println(frequency/Puls, 0);
Serial.print("Toerental per Minuut      = ");
Serial.println(frequency/Puls*60);
Serial.println();
Serial.println();
delay(1000);
}
``````

Please look at Read this before posting a programming question and follow the instructions on how to post code

You should not be using float data type for pin numbers:

``````float fanSpeed   = 0;  // A0 op de Arduino / Loper van de Potmeter
float fanPWM     = 3;  // Pin D3 op de Arduino. / Instellen van toerental 0/255
``````

Then, in loop(), you never use the definition of 'fanPWM' that you made, instead you use the raw pin number:

`````` analogWrite(3, Speed); // RPM snelheid instellen
``````

In setup(), you never set a pin mode for pin D3:

``````void setup() {
Serial.begin(9600);
pinMode(fanSensor, INPUT);
pinMode(fanSpeed, INPUT);
digitalWrite(fanSensor, HIGH);
}
``````

I don’t see why you need to include all that maths .
Once you’ve done the maths you could use the “map” function to convert your frequency number to an output value , it even use a look up table .