Hello all, here is full code, crude, simplistic, crappy, and rough - but it compiles and loads to Arduino nicely. Temperature and setpoint work and display on LCD. The last section in my problem.
// variables for input pin and control LED
#include <LiquidCrystal.h>
#include <math.h>
int SetPnt_pin = 5;
int temp_pin = 1;
int heat_pin = 9;
int rawsetpnt = 0;
int SetPntF = 0;
int tempraw = 0;
float tempV = 0;
float tempC = 0;
int tempF = 0;
int ctrl_F;
int tempF1;
int tempF2;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (RS, Enable, data lines)
void setup()
{
// declaration of pin modes
pinMode(heat_pin, OUTPUT);
// setup to send to LCD with 16 columns and 2 lines.
lcd.begin(16,2);
Serial.begin(9600);
}
void loop(){
tempraw = 0;
ctrl_F = 0;
{
for (int i = 0; i < 20; i++) // a smoothing function
tempraw = tempraw+analogRead(temp_pin);
delay(50);
tempraw = tempraw / 20;
tempV = ((tempraw * 5.0) / 1023.0);
tempC = ((tempV-1.375) / .0225);
tempF = ((tempC*1.8)+32);
lcd.setCursor(0,0);
lcd.print("HeatTemp = ");
lcd.print(tempF);
lcd.print("F");
lcd.print(" ");
}
delay(50);
//*************************start setPoint section
{
rawsetpnt = analogRead(SetPnt_pin);
rawsetpnt = (rawsetpnt/3.09);
SetPntF = rawsetpnt;
lcd.setCursor(0,1);
lcd.print("SetPoint = " );
lcd.print(SetPntF);
lcd.print("F");
lcd.print(" ");
}
delay(50);
/*everything works just fine down to this section. The setpoint displays and
changes with pot. Temperature displays a reasonable temperature and goes
up with heat applied to sensor. Here is where my problem comes in - trying
to map with PWM to vary output to heater.*/
//tempF1 = (tempF - 20);
//tempF2 = tempF;
//ctrl_F = map (tempF, tempF1, tempF2, 255, 0);//emaking all these different.
//ctrl_F = map (tempF, 75, 95, 255, 0); //this works
ctrl_F = map (tempF, (tempF - 20), tempF, 255, 0);
//will this work? using (setpt - 20) & setpt rather than an actual number
if (tempF < (SetPntF - 20))
{
analogWrite (heat_pin, 255); //until temperature gets within 20 points
//of setpoint, heat on max heat
}
else // else if tempF is NOT < (SetPntF-20) then start tapering heat off
//from 255 to zero
{
analogWrite(heat_pin, ctrl_F); //start tapering off the heat, from 255 to zero.
}
Serial.print("ctrl_F =");
Serial.println(ctrl_F);
}
If I set the analogWrite(heat_pin, SetPntF): the PWM varys nicely as it should. The problem seems to be that ctrl_F always is “0” (zero) so it doesn’t work.
What I do get is the LED (for heater) is ON 100% until 20 before setpoint, then the PWM goes to zero, which is what it should do since ctrl_F is always zero.
Why does it not vary with temperature as it should? Is it because the map function only works with a real number, not… what do you call a word that is assigned a value? The example only uses numbers, but didn’t say if “words” work or not.
Thanks for any and all help.
Ken H>