Ir ao conteúdo
  • Cadastre-se

Arduino Menu chocadeira! Implementar posteriormente em um PID


Posts recomendados

Olá amigos, estou desenvolvendo uma chocadeira de TCC e preciso de um MENU legal pra ela, sou fraco de programação mas ja fiz algo..

Tem um código PID que vou implementar o menu depois, então provavelmente eu vou ter que mexer com os Delays.. Porque da forma que tá ele não vai funcionar com o PID, fiz um post anteriormente falando sobre isso, mas de toda forma o que eu preciso é:

 

Um iniciador de processo de incubação via buttons.. Pois pretendo diminuir o set point gradativamente depois do decimo quinto dia o valor de temperatura, pois depois, o embrião ja produz seu próprio calor.


Eu ja tenho um mostrador da umidade, temperatura e data e hora!

Utilizando display OLED I2C, ds18b20 para temperatura e htud21 para umidade

// --- Bibliotecas Auxiliares ---
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Time.h>
#include <DS3232RTC.h>
#include "cactus_io_HTU21D.h"



// --- Definições e Objetos ---
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);   //objeto para o OLED
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

HTU21D htu;                         //objeto para o htu21d
// ========================================================================================================


// --- Variáveis Globais ---
int temp;
int hum;
// ========================================================================================================

// --- Função setup ---
void setup ()
{
  Serial.begin(9600);   //serial em 9600 baud rate

  setSyncProvider(RTC.get); // the function to get the time from the RTC

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //Inicializa OLED com endereço I2C 0x3C (para 128x64)
  display.clearDisplay();
  display.display();

  }
// ========================================================================================================


// --- Função Loop ---
void loop ()
{
  temp = sensors.getTempCByIndex(0);
  hum =  htu.getHumidity();
    sensors.requestTemperatures();
    htu.readSensor();

    disp_temp(); 
  delay (4000);
  display.clearDisplay(); //limpa o display OLED

  disp_umid(); 
  delay (4000);
  display.clearDisplay(); //limpa o display OLED
  
  disp_data();
   delay (4000);
  display.clearDisplay(); //limpa o display OLED
}
// ========================================================================================================


// --- Desenvolvimento das Funções ---
  void disp_temp()
{
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("Temp.");
  display.setTextSize(2);
  display.setCursor(3, 12);
  display.setTextColor(WHITE);
  display.print(temp);
  display.print(" C");
  display.display();
  delay (2000);

} //end disp_temp


void disp_umid()
{
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(40, 0);
  display.print("Umid.");
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(40, 12);
  display.print(hum);
  display.print(" %");
  display.display();

} 

void disp_data()
{
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("Hora");
  display.setTextSize(1.75);
  display.setTextColor(WHITE);
  display.setCursor(3, 15);
    display.print(hour());
  display.print(":");
 display.print(minute());
 display.print(":");
 display.print(second());
   display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.setCursor(70, 0);
  display.print("Data");
   display.setTextSize(1.75);
  display.setTextColor(WHITE);
  display.setCursor(70, 15);
  display.print(day());
   display.print("-");
  display.print(month());
   display.print("-");
  display.print(year());
  display.display();

} 
// ========================================================================================================

 

IMG_0939.JPG

IMG_0940.JPG

IMG_0941.JPG

adicionado 3 minutos depois
#include <PID_v1.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Temp. sensor on PIN 2 on the Arduino
#define ONE_WIRE_BUS 2

//Define Variables we'll be connecting to
double Setpoint, Input, Output;
int inputPin=0, outputPin=3;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,0, DIRECT);


unsigned long serialTime; //this will help us know when to talk with processing

void setup()
{
  //initialize the serial link with processing
  Serial.begin(9600);
  
  //initialize the variables we're linked to
 // Input = analogRead(inputPin);
  Setpoint = 144;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  //pid-related code
  
sensors.requestTemperatures();
  float ftemp= sensors.getTempCByIndex(0);
  
  Input = (double)ftemp;
    delay (500);
  myPID.Compute();
  analogWrite(outputPin,Output);
  

  
  //send-receive with processing if it's time
  if(millis()>serialTime)
  {
    SerialReceive();
    SerialSend();
    serialTime+=500;
  }
  
  
}


/********************************************
 * Serial Communication functions / helpers
 ********************************************/


union {                // This Data structure lets
  byte asBytes[24];    // us take the byte array
  float asFloat[6];    // sent from processing and
}                      // easily convert it to a
foo;                   // float array



// getting float values from processing into the arduino
// was no small task.  the way this program does it is
// as follows:
//  * a float takes up 4 bytes.  in processing, convert
//    the array of floats we want to send, into an array
//    of bytes.
//  * send the bytes to the arduino
//  * use a data structure known as a union to convert
//    the array of bytes back into an array of floats

//  the bytes coming from the arduino follow the following
//  format:
//  0: 0=Manual, 1=Auto, else = ? error ?
//  1: 0=Direct, 1=Reverse, else = ? error ?
//  2-5: float setpoint
//  6-9: float input
//  10-13: float output  
//  14-17: float P_Param
//  18-21: float I_Param
//  22-245: float D_Param
void SerialReceive()
{

  // read the bytes sent from Processing
  int index=0;
  byte Auto_Man = -1;
  byte Direct_Reverse = -1;
  while(Serial.available()&&index<26)
  {
    if(index==0) Auto_Man = Serial.read();
    else if(index==1) Direct_Reverse = Serial.read();
    else foo.asBytes[index-2] = Serial.read();
    index++;
  } 
  
  // if the information we got was in the correct format, 
  // read it into the system
  if(index==26  && (Auto_Man==0 || Auto_Man==1)&& (Direct_Reverse==0 || Direct_Reverse==1))
  {
    Setpoint=double(foo.asFloat[0]);
    //Input=double(foo.asFloat[1]);       // * the user has the ability to send the 
                                          //   value of "Input"  in most cases (as 
                                          //   in this one) this is not needed.
    if(Auto_Man==0)                       // * only change the output if we are in 
    {                                     //   manual mode.  otherwise we'll get an
      Output=double(foo.asFloat[2]);      //   output blip, then the controller will 
    }                                     //   overwrite.
    
    double p, i, d;                       // * read in and set the controller tunings
    p = double(foo.asFloat[3]);           //
    i = double(foo.asFloat[4]);           //
    d = double(foo.asFloat[5]);           //
    myPID.SetTunings(p, i, d);            //
    
    if(Auto_Man==0) myPID.SetMode(MANUAL);// * set the controller mode
    else myPID.SetMode(AUTOMATIC);             //
    
    if(Direct_Reverse==0) myPID.SetControllerDirection(DIRECT);// * set the controller Direction
    else myPID.SetControllerDirection(REVERSE);          //
  }
  Serial.flush();                         // * clear any random data from the serial buffer
}

// unlike our tiny microprocessor, the processing ap
// has no problem converting strings into floats, so
// we can just send strings.  much easier than getting
// floats from processing to here no?
void SerialSend()
{
  Serial.print("PID ");
  Serial.print(Setpoint);   
  Serial.print(" ");
  Serial.print(Input);   
  Serial.print(" ");
  Serial.print(Output);   
  Serial.print(" ");
  Serial.print(myPID.GetKp());   
  Serial.print(" ");
  Serial.print(myPID.GetKi());   
  Serial.print(" ");
  Serial.print(myPID.GetKd());   
  Serial.print(" ");
  if(myPID.GetMode()==AUTOMATIC) Serial.print("Automatic");
  else Serial.print("Manual");  
  Serial.print(" ");
  if(myPID.GetDirection()==DIRECT) Serial.println("Direct");
  else Serial.println("Reverse");
}

Esse é o código do PID que eu fiz para o d218b20.. Obtive boas respostas, mas preciso implementar o MENU LCD nele, com sistema de rolamento de ovo feito por relé ponte H e motor de microondas, sistema de umidificação feito por cooler 12v em relé e sistema de exaustão..

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...