LCD is working however analog inputs are not being read or calculated correctly ... responds like digital input
#include <LiquidCrystal_I2C.h>
#include <Wire.h> // I2C Master lib for ATTinys which use USI - comment this out to use with standard arduinos
#include <avr/interrupt.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define tempSensor A3//pin2
#define knob A2 //pin3
#define iron 6 //Pin6
//#define LED 5;
int
minTemp = 27, //Minimum aquired iron tip temp during testing (°C)
maxTemp = 525, //Maximum aquired iron tip temp during testing (°C)
minADC = 234, //Minimum aquired ADC value during minTemp testing
maxADC = 733, //Maximum aquired ADC value during minTemp testing
maxPWM = 255, //Maximum PWM Power
avgCounts = 5, //Number of avg samples
lcdInterval = 80, //LCD refresh rate (miliseconds)
pwm = 0, //System Variable
tempRAW = 0, //System Variable
knobRAW = 0, //System Variable
counter = 0, //System Variable
setTemp = 0, //System Variable
setTempAVG = 0, //System Variable
currentTempAVG = 0, //System Variable
previousMillis = 0; //System Variable
float
currentTemp = 0.0, //System Variable
store = 0.0, //System Variable
knobStore = 0.0; //System Variable
void setup(){
pinMode(tempSensor,INPUT); //Set Temp Sensor pin as INPUT
pinMode(knob,INPUT); //Set Potentiometer Knob as INPUT
pinMode(iron,OUTPUT); //Set MOSFET PWM pin as OUTPUT
// pinMode(LED,OUTPUT); //Set LED Status pin as OUTPUT
Wire.begin() ;
lcd.backlight();
lcd.init();
lcd.clear();
lcd.setCursor(0,1);lcd.print("PRESET T: ");
lcd.setCursor(0,0);lcd.print("ACTUAL T:");
}
void loop(){
//--------Gather Sensor Data--------//
knobRAW = analogRead(knob); //Get analog value of Potentiometer
setTemp = map(knobRAW,0,1023,minTemp,maxTemp); //Scale pot analog value into temp unit
tempRAW = analogRead(tempSensor); //Get analog value of temp sensor adjust for opamp-2.5V + temp mv
currentTemp = map(analogRead(tempSensor),minADC,maxADC,minTemp,maxTemp); //Scale raw analog temp values as actual temp units
//--------Get Average of Temp Sensor and Knob--------//
if(counter<avgCounts){ //Sum up temp and knob data samples
store = store+currentTemp;
knobStore = knobStore+setTemp;
counter++;
}
else{
currentTempAVG = (store/avgCounts)-1; //Get temp mean (average)
setTempAVG = (knobStore/avgCounts); //Get knob - set temp mean (average)
knobStore=0; //Reset storage variable
store=0; //Reset storage variable
counter=0; //Reset storage variable
}
//--------PWM Soldering Iron Power Control--------//
if(analogRead(knob)==0){ //Turn off iron when knob as at its lowest (iron shutdown)
//digitalWrite(LED,LOW);
pwm=0;
}
else if(currentTemp<=setTemp){ //Turn on iron when iron temp is lower than preset temp
// digitalWrite(LED,HIGH);
pwm=maxPWM;
}
else{ //Turn off iron when iron temp is higher than preset temp
//digitalWrite(LED,LOW);
pwm=0;
}
analogWrite(iron,pwm); //Apply the aquired PWM value from the three cases above
//--------Display Data--------//
unsigned long currentMillis = millis(); //Use and aquire millis function instead of using delay
if (currentMillis - previousMillis >= lcdInterval){ //LCD will only display new data ever n milisec intervals
previousMillis = currentMillis;
if(analogRead(knob)==0){
lcd.setCursor(10,1);lcd.print("OFF ");
}
else{
lcd.setCursor(10,1);lcd.print(setTempAVG,1);lcd.print((char)223);lcd.print("C ");
}
if(currentTemp<minTemp){
lcd.setCursor(10,0);lcd.print("COOL ");
}
else{
lcd.setCursor(10,0);lcd.print(currentTempAVG,1);lcd.print((char)223);lcd.print("C ");
}
}
}