Go Down

Topic: I need some help with a fan controller. (Read 502 times) previous topic - next topic

wildbill

analogRead returns an int in the 0 to 1023 range, so 512 and 350 are both fine.

bidouilleelec

#16
Feb 13, 2020, 03:11 pm Last Edit: Feb 13, 2020, 03:12 pm by bidouilleelec
Hello Phobos84
.  I thought the UNO was 10 bit.

JCA34F's message seems not justified.
ATmega328p is 8 bits.  Type int is 16 bits.

Regards,
bidouilleelec

bidouilleelec

Code: [Select]


void setup() {
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(4, OUTPUT);

  pinMode(A0, INPUT);

  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(4, LOW);
}

void loop() {
  if(analogRead(A0) > 500)  //fan 1 on
    {digitalWrite(8, HIGH);}
  
else if(analogRead(A0) < 400)  //fan 1 off
    {digitalWrite(8, LOW);}

 if(analogRead(A0) > 700)  //fan 2 on
    {digitalWrite(7, HIGH);}

else if(analogRead(A0) < 600)  //fan 2 off
    {digitalWrite(7, LOW);}

 if(analogRead(A0) > 900)  // warning light on
    {digitalWrite(4, HIGH);}

else if(analogRead(A0) < 800)  // warning light off
    {digitalWrite(4, LOW);}


    }


Sorry I put my brackets in odd places.  

You have to study more carefuly if else statements.

Please explain what you want :

Fan 1 ON between x1 and y1
Fan 2 ON between x2 and y2
Light ON between x3 and y3

Regards,
bidouilleelec

Phobos84

You have to study more carefuly if else statements.

Please explain what you want :

Fan 1 ON between x1 and y1
Fan 2 ON between x2 and y2
Light ON between x3 and y3

Regards,
bidouilleelec

Well the idea is to control dual cooling fans in a car.  The temp sensor that is screwed into the passenger side cylinder head is a variable resistor.  With a fixed 1K ohm resistor I will make a voltage divider and have that feed analog data into the Arduino.  I need the board to turn fan 1 on at 190 deg F and off at 180 Deg F.  Then turn fan 2 on at 210 deg F and off at 195 deg F.  Then I would like a warning light to come on at 230 deg F and off at 215 Deg F.   

This is all so I can "reclaim" two used outputs in the Holley Terminator X ECU.  If I can use the Arduino to control fans then I can use the PWM of the Holley computer to control nitrous oxide.  This is for use in a race car.

I know that building a voltage divider using a variable resistor "thermistor" and a fixed 1K ohm resistor will not give me a linear reading.  That's okay considering I truly only have to read accurately between 150 to 250 deg F.  So the plan is after I get some code that seems to work with a pot then I will do some testing with a GM coolant temp sensor.  I'm going to wire the voltage divider as explained and put the sensor in heated water with a temp probe.  So as the temp increases I will watch the serial monitor so I can see what value from the sensor is the equivalent to what real world temp and go from there.

Sorry this was so long winded.  Any way I removed all my wiring and blew off the board with compressed air and now everything is back up an running.  I have 3 relays working as they should. Other than the pot I have on it seems to jump a little.

You guys are awesome!  I would never have got this running without this forums help.  I'm sure after more work on this I will have more questions.  To be honest I'm just having fun learning how to write code in a language I have never used before. 

JCA34F

My reply was in response to @Blue Eyes' original reply which contained:
Quote
Code: [Select]
const byte ON_TEMP =  512;    // turn on fan
const byte OFF_TEMP = 350;    // turn off fan when cooled


Proietti

Your if else statement is not the way it should be done. You start with if then all the next statements conditions should be else if and end with an else

bidouilleelec

Hello Phobos84

Please excuse my message #17.
Your code in message #14 is quite correct.

And  :
ATmega328p is 8 bits.
Type int is 16 bits.
But the ADC in Uno is 10 bits.

Regards,
bidouilleelec

TomGeorge

#22
Feb 15, 2020, 01:52 pm Last Edit: Feb 15, 2020, 01:52 pm by TomGeorge
Hi,
Check that you may have burnt your pot out with the short.
Do you have a DMM to check its operation?

This is where you need to know how to debug using   Serial.print.
Please load this code.

When you have loaded the code to your controller, select TOOLS then select SERIAL MONITOR.
At the bottom of the window that appears, baud, change that to 115200.
This code will then make the value of the pot ADC print on the screen every 250ms, so you can see a live reading of your pot.
Code: [Select]

int val ;
int PotPin = A0; //assigns a name to the A0 pin.
void setup()
{ // good practice to put the { on a new line when using it
  Serial.begin(115200); // opens the USB to send print statements to the IDE monitor screen.
  pinMode(PotPin, INPUT); //declares A0 as an input.
}


void loop()
{
  val = analogRead(PotPin); //reads the pot
  Serial.println(val);      //prints the value with linereturn to monitor.
  delay(250);
}



Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Phobos84

#23
Feb 17, 2020, 12:20 am Last Edit: Feb 17, 2020, 12:21 am by Phobos84
Your if else statement is not the way it should be done. You start with if then all the next statements conditions should be else if and end with an else
This is what I keep reading but when I try it that way it gives me an error.  If I change any of the "else If" to an "else"  it won't take it.  But as soon as I changed it to an "else if" then it worked.  Not sure why.

I put a new pot on and now everything is fine.  Well except that the ebay relay board I was using was junk.  So just using LED's for now to simulate until I get a new relay board.

Proietti


Proietti

Here you go like this

Code: [Select]

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(A0, INPUT);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(4, LOW);
}

void loop()
{
  if(analogRead(A0) > 500)  //fan 1 on
  {
    digitalWrite(8, HIGH);
  }
  else if(analogRead(A0) < 400)  //fan 1 off
  {
    digitalWrite(8, LOW);
  }
  else if(analogRead(A0) > 700)  //fan 2 on
  {
    digitalWrite(7, HIGH);
  }
  else if(analogRead(A0) < 600)  //fan 2 off
  {
    digitalWrite(7, LOW);
  }
  else if(analogRead(A0) > 900)  // warning light on
  {
    digitalWrite(4, HIGH);
  }
  else if(analogRead(A0) < 800)  // warning light off
  {
    digitalWrite(4, LOW);
  }
  else
  {
    //WHAT TO DO IF NONE OF THE ABOVE CONDITIONS ARE TRUE
  }
}

JCA34F

#26
Feb 17, 2020, 11:49 am Last Edit: Feb 17, 2020, 11:50 am by JCA34F
See if this simplified code works.
Code: [Select]

void setup() {
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(4, OUTPUT);

  //pinMode(A0, INPUT); // no need to pinMode analog inputs

/*digitalWrite(8, LOW);  not needed, pins are LOW by default
  digitalWrite(7, LOW);    at startup 
  digitalWrite(4, LOW);*/
}

void loop() {
  if(analogRead(A0) > 500)  //fan 1 on, curlies not needed if
    digitalWrite(8, HIGH);  // only one statement
   
  if(analogRead(A0) < 400)  //fan 1 off, else not necessary 
    digitalWrite(8, LOW);
  if(analogRead(A0) > 700)  //fan 2 on
    digitalWrite(7, HIGH);
  if(analogRead(A0) < 600)  //fan 2 off
    digitalWrite(7, LOW);
  if(analogRead(A0) > 900)  // warning light on
    digitalWrite(4, HIGH);
  if(analogRead(A0) < 800)  // warning light off
    digitalWrite(4, LOW);
}

JCA34F

Simpler:
Code: [Select]

void setup() {
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(4, OUTPUT);

  //pinMode(A0, INPUT); // no need to pinMode analog inputs

/*digitalWrite(8, LOW);  not needed, pins are LOW by default
  digitalWrite(7, LOW);    at startup 
  digitalWrite(4, LOW);*/
}

void loop() {
  int temp = analogRead(A0);
  if(temp > 500)  //fan 1 on, curlies not needed if
    digitalWrite(8, HIGH);  // only one statement
   
  if(temp < 400)  //fan 1 off, else not necessary 
    digitalWrite(8, LOW);
  if(temp > 700)  //fan 2 on
    digitalWrite(7, HIGH);
  if(temp < 600)  //fan 2 off
    digitalWrite(7, LOW);
  if(temp > 900)  // warning light on
    digitalWrite(4, HIGH);
  if(temp < 800)  // warning light off
    digitalWrite(4, LOW);
}

bidouilleelec

Hello Proietti,

Here you go like this

Code: [Select]

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(A0, INPUT);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(4, LOW);
}

void loop()
{
  if(analogRead(A0) > 500)  //fan 1 on
  {
    digitalWrite(8, HIGH);
  }
  else if(analogRead(A0) < 400)  //fan 1 off
  {
    digitalWrite(8, LOW);
  }
  else if(analogRead(A0) > 700)  //fan 2 on
  {
    digitalWrite(7, HIGH);
  }
  else if(analogRead(A0) < 600)  //fan 2 off
  {
    digitalWrite(7, LOW);
  }
  else if(analogRead(A0) > 900)  // warning light on
  {
    digitalWrite(4, HIGH);
  }
  else if(analogRead(A0) < 800)  // warning light off
  {
    digitalWrite(4, LOW);
  }
  else
  {
    //WHAT TO DO IF NONE OF THE ABOVE CONDITIONS ARE TRUE
  }
}

You could try this code :

Code: [Select]
int tempstock [] = {350 , 450 , 550 , 650 , 750 , 850 , 950} ;
int tempread = 0;
void setup()
{
  Serial.begin(1000000 );
}

void loop()
{
  //  int tempread = analogRead(A0);

  for (int i = 0; i < 7 ; i++ ) {
    tempread = tempstock[i];
    Serial.print("***** tempread = ");
    Serial.print(tempread );
   
    if (tempread > 500) //fan 1 on
    {
      //digitalWrite(8, HIGH);
      Serial.println("   test > 500 " );
    }
    else if (tempread < 400) //fan 1 off
    {
      Serial.println("   test < 400 " );
    }
    else if (tempread > 700) //fan 2 on
    {
      Serial.println("   test > 700 " );
    }
    else if (tempread < 600) //fan 2 off
    {
      Serial.println("   test < 600 " );
    }
    else if (tempread > 900) // warning light on
    {
      Serial.println("   test > 900 " );
    }
    else if (tempread < 800) // warning light off
    {
      Serial.println("   test < 800 " );
    }
    else
    {
      Serial.println(" WHAT TO DO IF NONE OF THE ABOVE CONDITIONS ARE TRUE ");
    }
  }
  for (;;);
}


and draw your own conclusion.

Regards,
bidouilleelec

Proietti

Hi bidouilleelec,

I am not the OP of this topic. I was just trying to show the OP the standard way of arranging a IF & IF AND ELSE statement.

Go Up