Alternatring between two fans - How?

Objective:
Have two fans but only run one at a time, when the temp sensor calls for the fan to come on one will start and stay on until a shutdown temp is met. Then on the next on call the other fan will come on and stay on until a shutdown temp is met.

I have the alternating part, but I dont know how to latch the on state so the fans don't bounce back and forth on each cycle through the loop.

/*

  • FAN logic...
    */
    if (inside_temp < shutdownTemp) // if temp is below shutoff point turn off
    outputValue = 0; // fan is off
    else if (inside_temp > shutdownTemp && inside_temp < startTemp && outputValue == 0) // we are above our shurdown point but under the startup point
    outputValue = 0; // fan is off
    else if (inside_temp < outside_temp) // if the temp inside is lower than te outside temp stay off
    outputValue = 0; // fan is off
    else if (inside_temp > startTemp && inside_temp > outside_temp) { //temp above start up
    dif = inside_temp - outside_temp;
    if (dif > dif_above){ // full output, outside temp went way down FAST and inside is still hot.
    outputValue = 255;
    }
    else{
    outputValue = map(dif, 0, dif_above, min_speed_adj, max_speed_adj); // start fan and adjust accordingly
    }
    }
    // pick what fan to use
    // this works but will not do what I want, as this is it swaps fans on EVERY cycle in the loop
    if (lastFanOn == 2){
    lastFanOn = 1;
    // use fan #1
    analogWrite(fan_1, outputValue); // writes to PWM output
    analogWrite(fan_2, 0); // writes to PWM output -- this fan is OFF --
    }else {
    lastFanOn = 2;
    // use fan #2
    analogWrite(fan_1, 0); // writes to PWM output -- this fan is OFF --
    analogWrite(fan_2, outputValue); // writes to PWM output
    }

Please read the two "sticky" threads at the top of the forum.

You should post all your code, not just a snippet. For the reason why, please go to http://snippets-r-us.com/.

Nothing else in the code except for the declarations has anything to do with what was posted or asked.

When did I say I know the answer?

And why do you post smart ass questions? Who does that help?

02660:
When did I say I know the answer?

And why do you post smart ass questions? Who does that help?

We infer you know the answer since you know what the problem isn't.

Help us help you by sticking to the rules.

Everyone here tries to help where we can, but it's a hobby, so if we're browsing the threads and can help we do that; if a thread looks like there's info missing, it gets closed and on to the next one.

02660:
Nothing else in the code except for the declarations has anything to do with what was posted or asked.

Well, we don't really know that, do we? Why should we take the word of someone who doesn't know why his code doesn't work?

But, ultimately, it's up to you. Personally, I will not bother trying to take code that is in QUOTE tags insted of CODE tags, fart around reformatting it to readability, and guessing what else might be in the code, just to answer a question from someone who can't be bothered to ask it properly.

Good luck.

You could use a statemachine to clean up the logic. This example uses my statemachine library found here;

http://playground.arduino.cc/Code/SMlib

#include <SM.h>

SM FanControl(StoppedB);

int inside_temp;
int outside_temp;
int dif;
const int shutdownTemp = 20;
const int startTemp = 24;
const int dif_above = 10;
const int min_speed_adj = 40;
const int max_speed_adj = 120;
const int fanPin[2] = {6, 9};//just guessing here, not enough info
int fanNo;//for selecting fans



void setup(){
  for(fanNo = 0; fanNo < 2; fanNo++) pinMode(fanPin[fanNo], OUTPUT);
  fanNo = 0;
}//setup()

void loop(){
  //code to get inside_temp
  //code to get outside_temp
  EXEC(FanControl);
}//loop()

State StoppedH(){
  analogWrite(fanPin[fanNo], 0);//turn current fan off
  fanNo = 1-fanNo;//change fans
}//StoppedH()

State StoppedB(){
  if(inside_temp >max(outside_temp, startTemp)) FanControl.Set(Running);
}//StoppedB()

State Running(){
  dif =  inside_temp - outside_temp;
  if(dif > dif_above) analogWrite(fanPin[fanNo], 255);
  else analogWrite(fanPin[fanNo], map(dif, 0, dif_above, min_speed_adj, max_speed_adj));
  if(inside_temp < shutdownTemp) FanControl.Set(StoppedH, StoppedB);
}//Running()

And please, listen to the other users and provive a full program, that makes it so much easier to help you

state diagram alternating fans.png

Use edge detection to determine when the fan demand changes.
Assign a number to each fan (i.e. 0 and 1).
Have a variable recording the number of the fan currently being used.
Each time the fan demand changes from 'off' to 'on', turn the current fan on.
Each time the fan demand changes from 'on' to 'off', turn the current fan off and increment the current fan number. Use the modulus operator to ensure that the current fan number stays within the number of fans.

JimboZA:
Everyone here tries to help where we can, but it's a hobby, so if we're browsing the threads and can help we do that; if a thread looks like there's info missing, it gets closed and on to the next one.

Fair enough.
I have gotten a lot of good help from lots of people and am thankful for all of it.
The code I posted was not really part of my question, more of a example of when the fans turn on and off.

lar3ry:
Personally, I will not bother trying to take code that is in QUOTE tags insted of CODE tags, fart around reformatting it to readability, and guessing what else might be in the code, just to answer a question from someone who can't be bothered to ask it properly.

The code was copied from the IDE using the "copy for forum" option... Perhaps you should ask them to change the tag since in puts them in.

PeterH:
You could use a statemachine to clean up the logic. This example uses my statemachine library found here;

Interesting, I think that will work. I was not thinking of breaking it out into separate functions, but that should do it.
Thanks

02660:
The code was copied from the IDE using the "copy for forum" option... Perhaps you should ask them to change the tag since in puts them in.

I will, if I can figure out who to mention it to.

lar3ry:

02660:
The code was copied from the IDE using the "copy for forum" option... Perhaps you should ask them to change the tag since in puts them in.

I will, if I can figure out who to mention it to.

It is the same format it just adds the color tags, why don't you like it?

02660:
It is the same format it just adds the color tags, why don't you like it?

Because it doesn't preserve the formatting properly.
No matter. I just mentioned it to the developers.

The 'copy for forum' catches people out regularly and is worse than useless. It's an abortion that should never have made it into the IDE in the first place. It's been in there for many versions and no sign of the developers taking it out so far, so I wouldn't be optimistic that they'll treat this new complaint any differently.

PeterH:
The 'copy for forum' catches people out regularly and is worse than useless. It's an abortion that should never have made it into the IDE in the first place. It's been in there for many versions and no sign of the developers taking it out so far, so I wouldn't be optimistic that they'll treat this new complaint any differently.

Well, you never know. The discussion is at IDE: Copy for Forum · Issue #1675 · arduino/Arduino · GitHub if you're interested.