//LCD config
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Thermistor needed libraries
#include <thermistor.h> //Download it here: https://electronoobs.com/eng_arduino_thermistor.php
#include <AccelStepper.h>
// Define a stepper and the pins it will use
//new
#define potentiometer A6 //10k Variable Resistor
#define bt_1 7 // Clockwise Button
#define bt_2 10 // Start-Stop Button
#define bt_3 11 // Anticlockwise Button
#define dirPin 4 //4Pin of Arduino--Direction of stepper motor driver
#define stepPin 3 //3Pin of Arduino--Step of stepper motor driver
#define enPin 2 //2Pin of Arduino--Enabled of stepper motor driver
// I/O
int PWM_pin = 5;
/* //Pin for PWM signal to the MOSFET driver (the BJT npn with pullup)
int speed_pot = A1;
int but1 = 7;
int EN = 2;
int STEP = 3;
int DIR = 4;
*/
int LED = 13;
//new
int read_ADC;
int Speed_LCD, Speed_Delay;
int Mode=1, flag=0;
//Variables
float set_temperature = 200; //Default temperature setpoint. Leave it 0 and control it with rotary encoder
float temperature_read = 0.0;
float PID_error = 0;
float previous_error = 0;
float elapsedTime, Time, timePrev;
float PID_value = 0;
int button_pressed = 0;
int menu_activated=0;
float last_set_temperature = 0;
int max_PWM = 255;
/*
//Stepper Variables
int max_speed = 1000;
int main_speed = 0;
bool but1_state = true;
bool activate_stepper = false;
int rotating_speed = 0;
*/
//PID constants
//////////////////////////////////////////////////////////
int kp = 90; int ki = 30; int kd = 80;
//////////////////////////////////////////////////////////
int PID_p = 0; int PID_i = 0; int PID_d = 0;
float last_kp = 0;
float last_ki = 0;
float last_kd = 0;
int PID_values_fixed =0;
LiquidCrystal_I2C lcd(0x27,16,2); //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.
thermistor therm1(A0,0); //Connect thermistor on A0, 0 represents TEMP_SENSOR_0 ( configuration.h for more)
//AccelStepper stepper1(1, STEP, DIR); // (Type of driver: with 2 pins, STEP, DIR)
void setup() {
//new
pinMode(potentiometer, INPUT); // declare potentiometer as input
pinMode(bt_1, INPUT_PULLUP); // declare bt_1 as input
pinMode(bt_2, INPUT_PULLUP); // declare bt_2 as input
pinMode(bt_3, INPUT_PULLUP); // declare bt_3 as input
pinMode(dirPin, OUTPUT); // declare as output for Direction of stepper motor driver
pinMode(stepPin, OUTPUT); // declare as output for Step of stepper motor driver
pinMode(enPin, OUTPUT); // declare as output for Enabled of stepper motor driver
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(" WELCOME To My ");
lcd.setCursor(0,1);
lcd.print("Filament Project");
delay(2000); // Waiting for a while
lcd.clear();
//new_eof
/*
pinMode(EN, OUTPUT);
digitalWrite(EN, HIGH); //Stepper driver is disbled
stepper1.setMaxSpeed(max_speed);
pinMode(but1, INPUT_PULLUP);
pinMode(speed_pot, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
*/
pinMode(PWM_pin,OUTPUT);
TCCR0B = TCCR0B & B11111000 | B00000010; // D6 adn D6 PWM frequency of 7812.50 Hz
Time = millis();
TCCR1A = 0; //Reset entire TCCR1A register
TCCR1B = 0; //Reset entire TCCR1B register
TCCR1A |= B00000010; // /8
TCNT1 = 0; //Reset Timer 1 value to 0
lcd.init();
lcd.backlight();
}
void loop() {
//new
read_ADC = analogRead(potentiometer); // read analogue to digital value 0 to 1023
Speed_Delay = map(read_ADC, 0, 1023, 5000, 10); //value map for Microstep resolution Delay
Speed_LCD = map(read_ADC, 0, 1023, 0, 100); //value map to Display on the LCD
lcd.setCursor(0,1);
lcd.print("Spd: ");
lcd.print(Speed_LCD);
lcd.print("% ");
if(digitalRead (bt_1) == 0){Mode = 2; digitalWrite(enPin, LOW);} //For Clockwise
if(digitalRead (bt_2) == 0){ //For Stop
if(flag==0){flag=1;
if(Mode>1)Mode=1;
else{Mode=!Mode;
if(Mode==0)digitalWrite(enPin, HIGH);
else digitalWrite(enPin, LOW);
}
delay(100);
}
}else{flag=0;}
if(digitalRead (bt_3) == 0){Mode = 3; digitalWrite(enPin, LOW);} //For Anticlockwise
lcd.setCursor(9,1);
if(Mode==0)lcd.print("Free ");
else if(Mode==1)lcd.print("Lock ");
else if(Mode==2)lcd.print("P.Kiri ");
else if(Mode==3)lcd.print("P.Kanan");
if(Speed_LCD>0 && Mode>1){
if(Mode==2)digitalWrite(dirPin, LOW);// Stepper motor rotates CW (Clockwise)
else digitalWrite(dirPin, HIGH);// Stepper motor rotates CCW (Anticlockwise)
digitalWrite(stepPin, HIGH);
delayMicroseconds(Speed_Delay);
digitalWrite(stepPin, LOW);
delayMicroseconds(Speed_Delay);
}
//new_eof
/*
if(!digitalRead(but1) && but1_state){
but1_state = false;
activate_stepper = !activate_stepper;
delay(10);
}
else if(digitalRead(but1) && !but1_state){
but1_state = true;
}
if(activate_stepper){
digitalWrite(LED, HIGH);
digitalWrite(EN, LOW); //We activate stepper driver
rotating_speed = map(analogRead(speed_pot),0,1024,main_speed,max_speed);
stepper1.setSpeed(rotating_speed);
//stepper1.runSpeed();
}
else
{
digitalWrite(EN, HIGH); //We deactivate stepper driver
digitalWrite(LED, LOW);
stepper1.setSpeed(0);
stepper1.runSpeed();
}
*/
// First we read the real value of temperature
temperature_read = therm1.analog2temp(); // read temperature
//Next we calculate the error between the setpoint and the real value
PID_error=set_temperature-temperature_read+6;
//Calculate the P value
PID_p=0.01*kp*PID_error;
//Calculate the I value in a range on +-6
PID_i=0.01*PID_i+(ki*PID_error);
//For derivative we need real time to calculate speed change rate
timePrev = Time; // the previous time is stored before the actual time read
Time = millis(); // actual time read
elapsedTime = (Time - timePrev) / 1000;
//Now we can calculate the D calue
PID_d = 0.01*kd*((PID_error - previous_error)/elapsedTime);
//Final total PID value is the sum of P + I + D
PID_value = PID_p + PID_i + PID_d;
//We define PWM range between 0 and 255
if(PID_value < 0){
PID_value = 0;
}
else if(PID_value > max_PWM){
PID_value = max_PWM;
}
//Now we can write the PWM signal to the mosfet on digital pin D5
analogWrite(PWM_pin, PID_value);
previous_error = PID_error; //Remember to store the previous error for next loop.
//delay(250); //Refresh rate + delay of LCD print
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("T: ");
lcd.print(temperature_read,0);
lcd.print("C");
lcd.print(" PID: ");
lcd.print(PID_value,0);
/*
lcd.setCursor(0,1);
lcd.print("S: ");
lcd.print(rotating_speed);
lcd.print(" M: ");
lcd.print(activate_stepper);
*/
}
//Void loop end
/*
ISR(TIMER1_COMPA_vect){
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
stepper1.runSpeed();
}
*/