Temp Controlled fans, new build

hi guys, been in the hardware thread getting some help now i need some coding help

essentially i'm running a UNO, reading 1-wire thermometers, displaying the temp on a screen and turning a fan on and off based on that.

my code is a follows, i'm only working on the AMP temperature section at the moment....

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
#include <TimerOne.h>

#define ONE_WIRE_BUS 2 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define cFanControlPin0 9
#define cFanControlPin1 10

// Used as a baseline OFF point for fan control
const uint8_t FanOff0 = 35; //**************amp cutoff***********************
const uint8_t FanOff1 = 33; //**************xbox cutoff**********************

// OLED display TWI address
#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(-1);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


void setup() {

  Timer1.initialize(40);  // 40 us = 25 kHz
  
//Turn on the Temp Sensors
  sensors.begin();

  // Initialize and clear Display Buffer
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();

  delay(500);

  //set pins for fan control
  pinMode(cFanControlPin0, OUTPUT);
  analogWrite(cFanControlPin0, 0);
  pinMode(cFanControlPin1, OUTPUT);
  analogWrite(cFanControlPin1, 0);

}

void loop()
{
  //GET THE TEMPERATURES FROM ALL SENSORS
  sensors.requestTemperatures();
  //GIVE THEM TIME TO READ
  delay(1000);
  

//*******************************************
//                FAN AMP
//          Controlled by pin #9
//            
//*******************************************

// IF the TEMP on AMP is LESS THAN #FanOff0
// then clear the display and shut the fan off

  
if (sensors.getTempCByIndex(0) < FanOff0)
  {
    display.clearDisplay();
    display.display();
    analogWrite(cFanControlPin0, 0);
  }

  //ELSE is the TEMP on AMP is GREATER OR EQUAL to #FanOff0
  // start the fan and display the temp
  
  
  else if (sensors.getTempCByIndex(0) >= FanOff0)
    {
      analogWrite(cFanControlPin0, 255);

      //Prints the DEG symbol
      printfinish();

      //Sets up the display font and prints the TEMP from Probe#1
      display.setTextSize(4);
      display.setTextColor(WHITE);
      display.setCursor(41,1);
      //    
      display.print(round(sensors.getTempCByIndex(0)),DEC);
      display.display();
      delay(1000);
      //clears display buffer
      display.clearDisplay();
    }



//*******************************************
//                FAN XBOX
//         Controlled by pin #10
//            
//*******************************************

// IF the TEMP on XBOX is LESS THAN #FanOff1
// then clear the display and shut the fan off
  

if (sensors.getTempCByIndex(1) < FanOff1)
  {
//    display.clearDisplay();
//    display.display();
    analogWrite(cFanControlPin1, 0);
  }

  //ELSE is the TEMP on XBOX is GREATER OR EQUAL to #FanOff1
  // start the fan and display the temp
  
  
  else if (sensors.getTempCByIndex(1) >= FanOff1)
    {
      analogWrite(cFanControlPin1, 255);

      //Prints the DEG symbol
   //   printfinish();

      //Sets up the display font and prints the TEMP from Probe#2
  //    display.setTextSize(4);
   //   display.setTextColor(WHITE);
   //   display.setCursor(1,1);
      //
  //    display.print(round(sensors.getTempCByIndex(1)),DEC);
   //   display.display();
  //    delay(1000);
      //clears display buffer
  //    display.clearDisplay();
    }
        
  
}



void printfinish()
{
  
  //Function to build the DEG and FAN marking on the displays
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(90,-3);
  display.print("o");
//  display.setTextSize(3);
//  display.setTextColor(WHITE);
//  display.setCursor(70,8);
//  display.print("FAN");

}

now...

it works fine as is,
the temp comes up then fan comes on,
the temp drops then fan stops.... but what I'd like to do is add a bit of range control to it.

something like
0-30degrees -off
31-36 - 30%
37-41 - 60%
and over 42 - Max out

i came across this little guy which works great for its pupose....

#include <TimerOne.h>


#define motorPin 10

void setup() {
  Timer1.initialize(40);  // 40 us = 25 kHz
  pinMode(motorPin, OUTPUT);
  analogWrite(motorPin, 0);
  Serial.begin(9600);
  Serial.println("Enter number between 0 and 9:");
}

void loop() {
if(Serial.available()){
  char ch = Serial.read();
  if (ch >= '0' && ch <= '9'){
    int speed = ch - '0';
    analogWrite(motorPin, map(speed, 0, 9, 0, 255));
  }
}
}

you enter numbers from 0 to 9 to control the fan.... again that works great,

so i tried to modify it to suit my needs and adjust the fan based on the temp...

else if (sensors.getTempCByIndex(0) >= FanOff0)
{
  int tp = (round(sensors.getTempCByIndex(0)),DEC);
  if (tp >= 0 && tp <= 100)
     {
      int speed = tp;
      analogWrite(cFanControlPin0, map(speed, 0, 100, 0, 255));
     }
}

but i cant get it right...

what it does do is turns the fan on at about 50deg up and shuts it off on the way back down...

but it doesn't throttle, just full speed and off
and i cant seem to get the range adjusted to get it to turn off at like 30deg

any help would be greatly appreciated, i'm coming back to coding after a very long time and am having a bit of trouble re-wrapping my head around it all.

thanks in advance

What you should maybe look into is the concept of a state machine. There are tutorials in the Forum. Essentially you define your state variable, in this case simply call it temp and classify your states with a switch statement,

switch (temp)
{
  case 0 ... 30:
    // do this
    break;
  case 31 ... 36:
    // do that
    break;
  case 37 ... 41:
    // do something else
    break;
  default:
    // do whatever if the temp is > 41
    break;
}

DKWatson:
What you should maybe look into is the concept of a state machine.

thanks for that suggestion
i looked into that a little bit last night and you're right that might be the way to go long term...

but for now I've finally managed to get it to work using a few "if, else if" statements and that'll keep me happy for now, so as long as i don't break it while i clean and finish adding in a few functions then i might just leave it alone for a while , or at least till i read all the important stuff again and have a good long nap.

thanks again