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.