int E1 = 5;//Define fan pin
int F1 = 4;//Define fan speed output
void fandriver(int a,int i) //Define fan function is used to start the fan and controls the speed of rotation;
{
analogWrite (E1,a);
digitalWrite(F1,i);
}
uint16_t get_gp2d12 (uint16_t value) //Range operation functions for handling range sensor data sent back;
{
if (value < 30)
value = 30;
return ((67870.0 / (value - 3.0)) - 40.0);}
void setup() {
for(int i=4;i<=5;i++) pinMode(i, OUTPUT);
pinMode(A1,INPUT);
pinMode(A0,INPUT);
}
void loop() {
uint16_t value1 = analogRead (A0); //Receive information from the data;
uint16_t range = get_gp2d12 (value1); //Processing received data;
if(range<1000)
{
Motor_speed=map(analogRead(A1),0,1024,0,256); //Match speed to motor speed;
if(Motor_speed<30)Motor_speed=0; //Threshold settings for a filter fan not turn speed;
fandriver(Motor_speed,1); //Starting Fan;
}
else
fandriver(0,0); //Stopping Fan;
}
Enjoy!