Buongiorno a tutti ho fatto un progettino di un termostato con pid, relay tradizionale (non ssr) e una termocoppia k per regolare e mantenere la temperatura di un fornetto per pizza. Ho tre pulsanti: cambio modalita/schermata (off, mantieni temperatura e on con resistenza sempre accesa ) e due pulsanti per aumentare o diminuire la temperatura desiderata. Il problema è che riesco ad impostare solo da sketch la temperatura desiderata e se uso i pulsanti me la imposta sul display, ma il pid sembra fregarsene...Premetto che sono arrivato a questo schetch con vari copia/incolla perche le mie conoscenze di programmazione rimangono molto basilari quindi abbiate pietà
#include <PID_v1.h>
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 100, 0.25, 1.7, DIRECT);
int WindowSize = 10000;
unsigned long windowStartTime;
#include <MAX6675_Thermocouple.h>
#include <Thermocouple.h>
Thermocouple* thermocouple;
#define SCK_PIN 9
#define CS_PIN 10
#define SO_PIN 8
MAX6675_Thermocouple tc = MAX6675_Thermocouple(SCK_PIN, CS_PIN, SO_PIN);
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
boolean isPressed = false;
int Ttarget = 200; // temperatura(MODIFICABILE A PIACIMENTO TRAMITE PULSANTI)
#define temppiu 4 //pulsante incremento temperatura
#define tempmeno 5 //pulsante decremento temperatura
#define buttonmodePin 6 //pulsante cambio modalità
int relayPin = 7;
long int startTime=0;
long int stopTime=0;
long int realTime=0;
long int Time=0;
long int temposec=0;
long int tempomin=0;
int selector = 0;
int flag = 0; //variabile per incremento temperatura
int flag1 = 0; //variabile per decremento temperatura
void setup()
{
// Debug console
Serial.begin(9600);
int temp = tc.readCelsius();
Wire.begin();
pinMode(buttonmodePin, INPUT_PULLUP);
pinMode(temppiu, INPUT_PULLUP);
pinMode(tempmeno, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(13,1);
display.println("FOR-NINO");
display.setCursor(20,25);
display.println("3.1 PID");
display.setCursor(42,55);
display.setTextSize(1);
display.println("by Dany");
display.display();
delay(3000);
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = Ttarget;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
realTime=millis();
if (digitalRead(buttonmodePin) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
{
isPressed = true; //set to true, so this code will not run again until button released
// a call to a separate function that performs the switch statement and subsequent evoked code
selector++; // this is done after the doSwitchStatement(), so case 0 will be executed on the first button press
if (selector > 2) {
selector = 0;
}
} else if (digitalRead(buttonmodePin) == HIGH)
{
isPressed = false; //button is released, variable reset
}
if (selector == 2)
{
int temp = tc.readCelsius();
digitalWrite(relayPin, HIGH);
display.setTextSize(2);
display.clearDisplay();
display.setCursor(0, 7);
display.print("Temp");
display.setCursor(58, 7);
display.print(temp);
display.setCursor(105, 7);
display.print("C");
display.setCursor (90, 40);
display.print("ON");
Serial.println("acceso");
delay(200);
startTime=realTime;
Time = startTime - stopTime;
temposec = Time / 1000;
display.setCursor (0, 40);
display.print("Sec");
display.setCursor (45, 40);
display.print(temposec);
display.display();
if (temp >= 500)
digitalWrite(relayPin, LOW);
else {
}
}
else if (selector == 1)
{
if ((digitalRead(temppiu) == LOW) && (flag == 0))
{
Ttarget = (Ttarget + 10);
flag = 1;
Serial.println( Ttarget);
}
if ((digitalRead(temppiu) == HIGH) && (flag == 1))
{
flag = 0;
}
if ((digitalRead(tempmeno) == LOW) && (flag1 == 0))
{
Ttarget = (Ttarget - 10);
flag1 = 1;
Serial.println(Ttarget);
}
if ((digitalRead(tempmeno) == HIGH) && (flag1 == 1))
{
flag1 = 0;
}
int temp = tc.readCelsius();
stopTime=realTime;
display.setTextSize(2);
display.clearDisplay();
display.setCursor(0,7);
display.print("Temp");
display.setCursor(58,7);
display.print(temp);
display.setCursor(105,7);
display.print("C");
display.setCursor(0,40);
display.print("Tgt");
display.setCursor(45,40);
display.print(Ttarget);
display.setCursor(90,45);
display.setTextSize(1);
display.print("PID");
Serial.println("automatico");
display.display();
Input = temp;
myPID.Compute();
/************************************************
turn the output pin on/off based on pid output
************************************************/
unsigned long now = millis();
if (now - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if (Output > now - windowStartTime) digitalWrite(relayPin, HIGH);
else digitalWrite(relayPin, LOW);
delay(200);
}
else if (selector == 0)
{
int temp = tc.readCelsius();
digitalWrite(relayPin, LOW);
display.setTextSize(2);
display.clearDisplay();
display.setCursor(0,7);
display.print("Temp");
display.setCursor(58,7);
display.print(temp);
display.setCursor(105,7);
display.print("C");
display.setCursor(45,40);
display.print("OFF");
Serial.println("spento");
display.display();
delay(200);
}
}