// the code!
//digital read Hall Sensor on "val=digitalRead(0);".
//This part is working.
//Include Servo Library
#include <Servo.h>
//Define Pins
int servoPin = 9; //ESC control an 9
int calSpeed = 0; //Variable für Servo
//Create Servo Object
Servo juergenServo; // Servo
// Ab hier die Erfassung von des Radsensors (RPM, "rounds p. minute")
int val;
long last=0;
int stat=LOW;
int stat2;
int contar=0;
int sens=1; // Sensor/Magnet -> auf 0V,
int nPalas=5; // the number of blades of the propeller
int milisegundos=500; // the time it takes each reading
void setup()
{
//Attaches the Servo to our object
juergenServo.attach(servoPin);
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
val=digitalRead(0); //
if(val<sens)
stat=LOW;
else
stat=HIGH;
digitalWrite(13,stat); //as iR light is invisible for us, the led on pin 13
//indicate the state of the circuit.
if(stat2!=stat){ //counts when the state change, thats from (dark to light) or
//from (light to dark), remmember that IR light is invisible for us.
contar++;
stat2=stat;
{
//Read the Distance Sensor and adjust values
int calSpeed = ((double)contar/nPalas)/2.0*60000.0/(milisegundos);
int pos = map(calSpeed, 0, 1023, 0, 180);
juergenServo.write(pos);
}
}
if(millis()-last>=milisegundos)
{
double rps=((double)contar/nPalas)/2.01000.0/milisegundos;
double rpm=((double)contar/nPalas)/2.060000.0/(milisegundos);
int calSpeed=((int)contar/nPalas)/2.0*60000.0/(milisegundos);
//Serial.print((contar/2.0));
Serial.print("RPS ");Serial.print(rps);
Serial.print(" RPM");Serial.print(rpm);
Serial.print(" CAL SPEED ");Serial.print(calSpeed);
Serial.print(" VAL ");Serial.println(val);
contar=0;
last=millis();
}
}