/*
pullstruder heater control
thermistor&resistor joined on A0
3.3V joined to resistor
GND joined to thermistor
relay signal joined to D13 (built in LED)

using excerpts or concepts from: 
thermistor stuff: https://www.circuitbasics.com/arduino-thermistor-temperature-sensor-tutorial/
a4988 driver stuff: https://www.youtube.com/watch?v=0qwrnUeSpYQ
potentiometer stuff: https://docs.arduino.cc/learn/electronics/potentiometer-basics
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>




// ============ termistor ===========================

//pin for thermistor read
#define ThermistorPin A0  // analog pin to which the thermistor is connected

float seriesResistor = 10000;// resistance value of the series resistor (in ohms)
const float thermNominal = 100000; // nominal resistance of the thermistor at the standard temperature (in ohms)
const float temperatureNominal = 25; // temperature at which the thermistor's nominal resistance is specified (in degrees Celsius)
const float betaCoefficient = 3950; // beta coefficient of the thermistor

int samples = 30; //num temp samples to take
// ===================================================================
float setT = 40.0; //the set temperature for nozzle

int i;
//int V[6];
float R1 = 10000; //otpor u kolu koji je prikacen na termistor
float logR2, R2, T, Vo;
float c1 = 0.6992049798e-03, c2 = 2.189833533e-04, c3 = 0.8759609869e-07;

int potPin = A1;  // where the potentiometer is hooked up
int potVal = 0;  // variable to store its value

const int dirPin = 2;  // stepper direction pin D2
const int stepPin = 3;  // step pin D3
const int STEPS_PER_REV = 200;  // motor steps per revolution
int x = 0;  //loop counter for motor



int tPom = -1;
int speedPom = -1;


LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(A0, INPUT);

  pinMode(dirPin,OUTPUT);
  pinMode(stepPin,OUTPUT);

  pinMode(4, OUTPUT);

  Serial.begin(9600);
  lcd.init();
  lcd.clear();         
  lcd.backlight();
  // connect AREF to 3.3V as it is less noisy than 5V:
  analogReference(EXTERNAL);
}


void loop() {
    digitalWrite(dirPin,HIGH);  // set direction clockwise
    digitalWrite(4,HIGH);  //set MS2 on a4988 to high, enabling quarter steps
  // begin thermistor code
  Vo = 0;
  for (i=0; i<samples; i++)  {
    Vo = Vo + analogRead(ThermistorPin);
    //delay(2);
    digitalWrite(stepPin,HIGH); //step
    delay((potVal/100)+1);      //step delay
    digitalWrite(stepPin,LOW);  //step
    delay((potVal/100)+1);      //step delay
  }
  Vo = Vo / samples; //average tempereature after "samples" time 
  //Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15; 
  
  //Serial.print("Thermistor: ");
  //Serial.print(R2);
  //Serial.println(" ohms");
  //Serial.print("Vo: ");
  //Serial.println(Vo);
 

  if(tPom != T){
    lcd.setCursor(0,0);
    lcd.print("Temperature: ");
    lcd.print(T);
    lcd.println(" C");
    tPom=T;
  }

  if ((T < setT) and (T < 300) and (T > 0))  {
    digitalWrite(LED_BUILTIN, HIGH);  //turn on builtin LED and close heater relay
  }
  if (T > setT)  {
    digitalWrite(LED_BUILTIN, LOW);   //turn off builtin LED and open heater relay
  }
  //delay(200);
  // end thermistor code

  // begin potentiometer read code
  potVal = analogRead(potPin);
  if(speedPom != potVal){
    lcd.setCursor(4,2);
    int speed = map(potVal,0,1023,1,100);
    lcd.print("Speed: ");
    lcd.print(speed);
    lcd.println(":D");
    speedPom=potVal;
  }
  potVal = map(potVal,0,1023,1023,0);
  Serial.print("potVal: ");
  Serial.println(potVal);
  // end potentiometer read code

  // begin a4988 stepper driver code
  //digitalWrite(dirPin,HIGH);  // set direction clockwise

  // /*  spin
  while(x < 30)  {
    digitalWrite(stepPin,HIGH);
    delay((potVal/100)+1);
    digitalWrite(stepPin,LOW);
    delay((potVal/100)+1);
    x++;
  }
  x=0;
  // end a4988 stepper driver code */
  
}




//  float rawADC = 0.0;
//   for (i=0; i<samples; i++)  {
//     rawADC = rawADC + analogRead(ThermistorPin); // read the raw analog value
//     //delay(2);
//     digitalWrite(stepPin,HIGH); //step
//     delay((speed/100)+1);      //step delay
//     digitalWrite(stepPin,LOW);  //step
//     delay((speed/100)+1);      //step delay
//   }
//   rawADC  = rawADC / samples; //average tempereature after "samples" time 
//   //Vo = analogRead(ThermistorPin);
//   float resistance = seriesResistor / (1023.0 / rawADC - 1); // calculate the thermistor resistance using a voltage divider circuit
//   float steinhart;

//   steinhart = resistance / thermNominal; // calculate the resistance ratio
//   steinhart = log(steinhart); // take the natural logarithm of the resistance ratio
//   steinhart /= betaCoefficient; // divide by the beta coefficient
//   steinhart += 1.0 / (temperatureNominal + 273.15); // add the reciprocal of the nominal temperature
//   steinhart = 1.0 / steinhart; // take the reciprocal to get the temperature in kelvin
//   T = steinhart - 273.15; // convert from kelvin to degrees Celsius
//   Serial.print("Temperature: ");
//   Serial.print(T);
//   Serial.println(" C");
//   delay(1000); //
A4988
NOCOMNCVCCGNDINLED1PWRRelay Module