Two analog sensors controlling PWM

I am working on a project that takes the input of two analog sensors and outputs one PWM signal as a function of the two. Both of these analog sensors are limited at a high and low value, as set by potentiometers. I am also currently using potentiometers for sensor inputs, as they are easier to control than the sensors I will be using. I have an led hooked up to the PWM output and can get it to increase in brightness with the increase in both “sensor” potentiometers, but then it cuts out and begins to increase on brightness again instead of increasing in brightness and then staying fully on. Tomorrow I am going to try to monitor the f_out value using Serial.print to see what is happening- I am doing this in an electronics class I am enrolled in so can only physically work on it when I am in class. If you see anything clearly wrong with my code PLEASE point it out to me, otherwise I will update on my troubleshooting efforts as they happen.

/*
  Two analog sensors, each with their own min and max values, 
  that control the dimming of an LED bulb
  
  by: OnDaTrail
 */
 
// Setting up the variables:

   // Naming the analog pins:
      int analog_pin_0 = 0;
      int analog_pin_1 = 1;
      int analog_pin_2 = 2;
      int analog_pin_3 = 3;
      int analog_pin_4 = 4;
      int analog_pin_5 = 5;
    // Led, or PWM output, attached to pin 11:
      int pin_f_out = 11;
    // Analog inputs for the temperature input:
      int T_min;
      int T_max;
      int T;
    // Analog temperature inputs converted to PWM:
      int t_min;
      int t_max;
      int t;
    // Temperature output value set between min and max values and in PWM form:
      int t_out;
    // Analog inputs for the pressure input:
      int P_min;
      int P_max;
      int P;
    // Analog Pressure inputs converted to PWM:
      int p_min;
      int p_max;
      int p;
    // Pressure output value set between min and max values and in PWM form:
      int p_out;

    // Final PWM output values, as a function of both temperature and pressure
      int final_out;

/*
  Setting up pins as outputs or inputs.
*/
      void setup()
  { 
    // Led, or PWM output pin 11, set as an output
      pinMode(pin_f_out, OUTPUT); 

    // Analog input pins are set as inputs by default  
  }

/*
  The program, split up into temperature, pressure,
  and the combination of the two into one PWM output.
*/
      void loop()
  {
    // Temperature output: 
      T_min = analogRead(analog_pin_0);
      T_max = analogRead(analog_pin_1);
      T = analogRead(analog_pin_2);
    // Converting from analog (0-1023) to pwm (0-255)
      t_min = T_min / 4;
      t_max = T_max / 4;
      t = T / 4;
    // Setting the analog input between the min and max values- 
    // with the t_out being equal to 0 at t_min and 255 at t_max
      t_out = (255 / (t_max - t_min)) * (t - t_min);
    
    // Pressure output:
      P_min = analogRead(analog_pin_3);
      P_max = analogRead(analog_pin_4);
      P = analogRead(analog_pin_5);
    // Converting from analog (0-1023) to pwm (0-255)
      p_min = P_min / 4;
      p_max = P_max /4;
      p = P / 4;
    // Setting the analog input between the min and max values- 
    // with the p_out being equal to 0 at p_min and 255 at p_max
      p_out = (255 / (p_max - p_min)) * (p - p_min);
    
    // Combination of the temperature and pressure outputs to one PWM output:
      final_out = (sqrt((sq(p_out)) + (sq(t_out))));
  
    // The program:
      if (final_out > 255){
        digitalWrite(pin_f_out, HIGH);}
        
      else if(final_out < 0){
        digitalWrite(pin_f_out, LOW);}
      
      else if(final_out >= 0 && final_out <= 255){
        analogWrite(pin_f_out, final_out);}
 
     
   }

I do not have any error messages with this code.

your correct , best thing is to monitor with a few printf's , start with the analog reads and work your way up, arithmatic can be tricky also. maybe add a check to see if Tmax and Pmax are larger than Tmin Pmin, Risc of devide by zero or negative

You should know that this part wont work, well it will but not the way you want it to be

// Naming the analog pins:
      int analog_pin_0 = 0;
      int analog_pin_1 = 1;
      int analog_pin_2 = 2;
      int analog_pin_3 = 3;
      int analog_pin_4 = 4;
      int analog_pin_5 = 5;

The right way to declare an analog pin is by using A0 to A5 in your case like this

// Naming the analog pins:
      int analog_pin_0 = A0;
      int analog_pin_1 = A1;
      int analog_pin_2 = A2;
      int analog_pin_3 = A3;
      int analog_pin_4 = A4;
      int analog_pin_5 = A5;

one part that i an wondering is that

I am working on a project that takes the input of two analog sensors and outputs one PWM signal as a function of the two.

but in your code

T_min = analogRead(analog_pin_0);
      T_max = analogRead(analog_pin_1);
      T = analogRead(analog_pin_2);
   ......
      P_min = analogRead(analog_pin_3);
      P_max = analogRead(analog_pin_4);
      P = analogRead(analog_pin_5);

do you use 2 or 6 Analog pin?

if you are using 2 analog input and you want min, max and current value something along this line should work

int min = 1023;
int max = 0;

int current = analogRead(A0) // Just and example
if ( current < min) min= current;
if ( current > max) max = current;
int Tout = (((1023/ (t_max - t_min)) * (t - t_min))/4);

you know for the output part you do not need any conditioning since the arduino know if 0 its equal to low and 255 equal to high so

analogWrite(pin_f_out, final_out); would have been enough

@ash901226

You should know that this part wont work,

Why ever not?

it works, one advantage of arduino background stuff:

wiring_analog.c gives us : if (pin >= 14) pin -= 14; // allow for channel or pin numbers

pins_arduino.h gives us: static const uint8_t A0 = 14; static const uint8_t A1 = 15; static const uint8_t A2 = 16; ...etc...

AWOL: @ash901226

You should know that this part wont work,

Why ever not?

Awol as i wrote in reply #2

You should know that this part wont work, well it will but not the way you want it to be

from my understanding, the op wanted to use Analog pin, However from my limited knowledge of arduino, when someone wish to use the Analog pin, he/she have to write an A before the digit so that the arduino compiler know that the programmer requested to the Analog pin. I do how ever know that analog pin can also be call bit its pin number like for instant A0-A5 can be called as pin 14-19

The compiler doesn't need to know anything - that part is taken care of when you call [u]analog[/u]Read. What other pin could you be referring to, other than an analogue pin?

well then if thats the case i stand corrected AWOL, i learn new thing each and every single day here at the forum. Awol Thank you