Atmega328p-pu i czujnik D1820 nie działa

Masz problem z Arduino? Tutaj możesz szukać pomocy.
ODPOWIEDZ
Dabalwwy
Młodszy majsterkowicz
Posty: 41
Rejestracja: 4 wrz 2013, 11:10

Atmega328p-pu i czujnik D1820 nie działa

Post autor: Dabalwwy » 20 lut 2015, 19:05

Witam mam pewien problem . Mam program który śmiga super na płytce Leonardo ale gdy go wrzucam na atmege przestaje wyświetlać temperaturę. Wrzucam kod


Kod: Zaznacz cały

//libraries
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <PID_v1.h>
OneWire ds(9);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


// push buttons
const int Button_up = A3;
const int Button_dn = A2;
const int Button_prev = A1;
const int Button_nxt = A0;


// outputs
const int Heat = 10;
const int Pump = 8;
const int Buzz = 7;


// global variables
unsigned int WindowSize;

unsigned long windowStartTime;
unsigned long start;

double Setpoint, Input, Output,eepromKp, eepromKi, eepromKd;
boolean autoLoop = false; 
boolean manuLoop = false;
boolean waterAdd = false;
boolean Conv_start = false;
boolean mpump = false;
boolean mheat = false;
boolean wtBtn = false;
boolean autoEnter = false;
boolean tempReached = false;
boolean pumpRest = false;
boolean boilLoop = false;
boolean resume = false;
float mset_temp = 35;
float Temp_c, stageTemp,pumptempError;

int temp;
int x;
int  stageTime,hopTime;

byte mainMenu = 0;
byte pumpTime;
byte data[2];
byte second;
byte minute;
byte i;
byte Busy = 0;
byte nmbrStgs;
byte nmbrHops;
byte tempHAddr;
byte tempLAddr;
byte timeAddr;
byte blhpAddr;
byte hopAdd;

char* stgName[] ={
  "Woda.","Etap1.","Etap2.","Etap3.","Etap4.","Etap5.","Etap6.","Etap7.","Etap8."};


// degree c sybmol 
byte degc[8] =
{
  B01000,
  B10100,
  B01000,
  B00011,
  B00100,
  B00100,
  B00011,
  B00000,
};


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


void setup()
{
  // Start up the library
  lcd.begin(16,4);
  pinMode (Button_up,INPUT_PULLUP);
  pinMode (Button_dn,INPUT_PULLUP);
  pinMode (Button_prev,INPUT_PULLUP);
  pinMode (Button_nxt,INPUT_PULLUP);
  pinMode (Heat,OUTPUT);
  pinMode (Pump,OUTPUT);
  pinMode (Buzz,OUTPUT);
  windowStartTime = millis();

  //tell the PID to range between 0 and the full window size
  myPID.SetMode(AUTOMATIC);

  // write custom symbol to LCD
  lcd.createChar(0,degc);
}

void loop()
{
  switch (mainMenu){
    case (0):
    digitalWrite(Heat,LOW);
    digitalWrite(Pump,LOW);  
    Temperature();
    lcd.setCursor(4,0);
    lcd.print("Sterownik");
    lcd.setCursor(3,1);
    lcd.print("Warzelniczy");
    lcd.setCursor(0,2);
    lcd.print("by Dabal");
    lcd.setCursor(-1,3);
    lcd.print("Temp=");
    lcd.print(Temp_c);
    lcd.write((byte)0);

    delay(3000);
    lcd.clear();

    //read the buttons for menu selection

    if (digitalRead(Button_dn) == 0){
      delay(1000);
      if (digitalRead(Button_dn) == 0){
        lcd.setCursor(5,1);
        lcd.print("Manualny");
        lcd.setCursor(0,2);
        lcd.print("                ");
        mainMenu = 1;
        while(digitalRead(Button_dn) == 0){
        } 
      }
    }  
    if (digitalRead(Button_prev) == 0){
      delay(1000);
      if (digitalRead(Button_prev) == 0){
        lcd.setCursor(2,1);
        lcd.print("Automatyczny");
        lcd.setCursor(0,2);
        lcd.print("                "); 
        mainMenu = 2;
        while(digitalRead(Button_prev) == 0){
        } 
      }
    }
    if (digitalRead(Button_nxt) == 0){
      delay(1000);
      if (digitalRead(Button_nxt) == 0){         
        lcd.setCursor(3,1);
        lcd.print("Ustawienia");
        lcd.setCursor(0,2);
        lcd.print("                ");
        delay(500);   
        lcd.clear();     
        mainMenu = 3;
        while(digitalRead(Button_nxt)==0){
        } 
      }
    }
    break;

    case(1): //manual mode
    delay(500);
    eepromKp = word(EEPROM.read(0),EEPROM.read(1));// read the PID settings from the EEPROM
    eepromKi = word(EEPROM.read(2),EEPROM.read(3));
    eepromKd = word(EEPROM.read(4),EEPROM.read(5));

    myPID.SetTunings(eepromKp,eepromKi,eepromKd); // send the PID settings to the PID
    WindowSize = word(EEPROM.read(33),EEPROM.read(34));
    myPID.SetOutputLimits(0, WindowSize);

    lcd.setCursor(0,1);           // prompt to add water
    lcd.print("  Kadz zalana?  ");
    Buzzer2();
    lcd.setCursor(-4,3);
    lcd.print("  Tak  Wyjscie");
    wtBtn = true;

    while (wtBtn){               // wait for comfirmation 
      if (digitalRead(Button_prev)==0){
        manuLoop = true;
        wtBtn = false;
        lcd.clear();
        while(digitalRead(Button_prev)==0){
        }
      }
      if (digitalRead(Button_nxt)==0){
        mainMenu = 0;
        wtBtn = false;
        lcd.clear();
        while(digitalRead(Button_nxt)==0){
        }
      }
    }

    while (manuLoop){            // manual loop
      Temperature();
      Setpoint = mset_temp;
      Input = Temp_c;

      lcd.setCursor(0,0);
      lcd.print("  Tryb Manualny   ");
      lcd.setCursor(0,1);
      lcd.print("S/A=");
      lcd.print(mset_temp);
      lcd.print("/");
      lcd.print(Temp_c);
      lcd.write((byte)0);

      // Increase set temp or quit
      if (digitalRead(Button_up)==0){
        delay(200);
        if (digitalRead(Button_dn)==0){
          digitalWrite(Heat,LOW);
          digitalWrite(Pump,LOW);
          mainMenu=0;
          manuLoop = false;
          lcd.clear();
        }
        else{
          mset_temp= mset_temp+0.25;
        }
        while (digitalRead(Button_up)==0){
        }
      }

      // decrease temp or quit
      if (digitalRead(Button_dn)==0) 
      {
        delay(200);
        if (digitalRead(Button_up)==0){
          digitalWrite(Heat,LOW);
          digitalWrite(Pump,LOW);
          mainMenu=0;
          manuLoop = false;
          lcd.clear();
        }
        else{
          mset_temp= mset_temp-0.25;
        }
        while (digitalRead(Button_dn)==0){
        }
      }

      //turns heat on or off      
      if (digitalRead(Button_prev)==0){
        while(digitalRead(Button_prev)==0){
        }
        if (mheat==false){
          mheat = true;
          windowStartTime = millis();
        }
        else{
          mheat = false;
          digitalWrite(Heat,LOW);
        }
      }

      //turns the pump on or off
      if (digitalRead(Button_nxt)==0){
        if (mpump == false){
          mpump = true;
          digitalWrite(Pump,HIGH);
        }
        else{
          mpump = false;
          digitalWrite(Pump,LOW);
        }
        while(digitalRead(Button_nxt)==0){
        }
      }
      if (mheat){
        PID_HEAT(); 
      }        
    }
    break;

    /* This is the Auto process, it reads the number of stages and it the starts a loop for the number of stages and executes the stages*
     * at the start of each stage it reads the time and temp of that stage and once the temp is reached it starts counting down         */


    case(2): 
    delay(500);
    tempHAddr = 6; // setup intitial stage addresses 
    tempLAddr = 7;
    timeAddr = 8;
    nmbrStgs = EEPROM.read(38);// read the number of steps
    nmbrHops = EEPROM.read(39);//read the number of hop additions
    eepromKp = word(EEPROM.read(0),EEPROM.read(1));// get the PID settings
    eepromKi = word(EEPROM.read(2),EEPROM.read(3));
    eepromKd = word(EEPROM.read(4),EEPROM.read(5));
    myPID.SetTunings(eepromKp,eepromKi,eepromKd);// set the PID
    WindowSize =word(EEPROM.read(33),EEPROM.read(34));
    myPID.SetOutputLimits(0, WindowSize);


    if(EEPROM.read(35)){ // read the auto started byte to see if it has been set and if so ask to resume
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Wznowic Proces");
      lcd.setCursor(0,2);
      lcd.print("Tak   Nie");
      wtBtn = true;

      while (wtBtn){  // wait for comfirmation
        if (digitalRead(Button_prev)==0){
          resume = true;
          tempHAddr = (EEPROM.read(36)*3)+6;
          tempLAddr = tempHAddr+1;
          timeAddr = tempHAddr+2;
          wtBtn = false;
          autoEnter = true;
          lcd.clear();
        }
        if (digitalRead(Button_nxt)==0){
          wtBtn = false;
          resume = false;

        }
      }
    }

    if(!(resume)){  // if starting a new process prompt for water
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("  Kadz zalana?  ");
      Buzzer2();
      lcd.setCursor(-4,2);
      lcd.print("  Tak  Wyjscie");
      wtBtn = true;

      while (wtBtn){
        if (digitalRead(Button_prev)==0){
          autoEnter = true;
          wtBtn = false;
          EEPROM.write(35,1);
          lcd.clear();
          lcd.setCursor(6,0);
          lcd.print("Test");
          lcd.setCursor(4,1);
          lcd.print("Mieszadla");  // priming the pump
          digitalWrite(Pump,HIGH);
          delay (5000);
          digitalWrite(Pump,LOW);
          delay(2000);
          digitalWrite(Pump,HIGH);
          delay (5000);
          digitalWrite(Pump,LOW);
          delay(2000);
          digitalWrite(Pump,HIGH);
          delay (5000);
          digitalWrite(Pump,LOW);  
          lcd.clear();      
          while(digitalRead(Button_prev)==0){
          }
        }
        if (digitalRead(Button_nxt)==0){
          mainMenu = 0;
          wtBtn = false;
          autoEnter = false;
          lcd.clear();
          while(digitalRead(Button_nxt)==0){
          }
        }
      }
    }
    if (autoEnter){     // mash steps
      if(resume){
        x = EEPROM.read(36); // reads the last step of the interrupted process
      }
      else{
        x = 0;
      }

      for (int i = x;i < nmbrStgs;i++){
        EEPROM.write(36,lowByte(x)); // stores the stage number for the resume
        x++;                         // used to count the stages for the resume 
        tempReached = false;
        stageTemp = word(EEPROM.read(tempHAddr),EEPROM.read(tempLAddr));
        stageTemp = stageTemp/16.0;
        if (resume){                 // on the start of resume gets saved time
          stageTime=EEPROM.read(37);
          resume = false;            // clears resume for next loop
        }
        else{
          stageTime = EEPROM.read(timeAddr); // gets stage time
          EEPROM.write(37,lowByte(stageTime));// saves the intial stage time
        }
        start = millis();
        // windowStartTime = millis();
        second = 0;
        minute = 0;
        delay(50);

        while ((stageTime>0)&&(autoEnter)){
          if ((millis()-start)>1000){  // timing routine
            start = millis();
            second++;
            if(!(tempReached))second=0;// starts counting down when temp reached
            if (second>59){
              lcd.setCursor(10,0);
              lcd.print("      ");
              second = 0;
              pumpTime++;
              stageTime--;
              EEPROM.write(37,lowByte(stageTime));// saves stage time incase of interuption
            }
          }
          Temperature();// get temp
          Setpoint = stageTemp;//
          Input = Temp_c;
          if (pumpRest){
            lcd.setCursor(0,0);
            lcd.print("   Przerwa   ");  // display Pump rest during rest period
            lcd.setCursor(0,1);
            lcd.print("                ");
          }
          else{
            lcd.setCursor(0,0);       // display stage name, time left , set temp and actual temp
            lcd.print(stgName[i]);
            lcd.print("    ");
            lcd.setCursor(10,0);
            lcd.print("T=");
            lcd.print(stageTime);
            lcd.setCursor(0,1);
            lcd.print("S/A=");
            lcd.print(stageTemp);
            lcd.print("/");
            lcd.print(Temp_c);
            lcd.write((byte)0);
          }
          if(i == 0)pumpTime = 0;
          pumptempError = stageTemp-Temp_c;
          if (pumptempError <= 0)tempReached = true;
          if ((pumpTime < 10) || (pumptempError > 1.0)){ // starts pumps and heat
            digitalWrite(Pump,HIGH);
            PID_HEAT();
            pumpRest =false;
          }
          if ((pumpTime >= 10) && (pumptempError < 1.0)){ // pump rest
            digitalWrite(Pump,LOW);
            digitalWrite(Heat,LOW);
            pumpRest = true;
            if(pumpTime>=12)pumpTime = 0;
          } 
          if (digitalRead(Button_dn) == 0) // quit pressing up and down button
          {
            delay(200);
            if (digitalRead(Button_up) == 0){
              digitalWrite(Heat,LOW);
              digitalWrite(Pump,LOW);
              lcd.clear();
              delay(50);
              mainMenu=0;
              autoEnter = false;
              i = 10;
            }
          }
        }

        if( i==0 ){    // at the end of the mashIn step pauses to add grain
          lcd.clear();
          digitalWrite(Pump,LOW);
          digitalWrite(Heat,LOW);
          lcd.print("   Dodaj Slod   ");
          Buzzer2();
          lcd.setCursor(0,2);
          lcd.print("Potwierdz");
          wtBtn = true;
          while (wtBtn){
            if (digitalRead(Button_nxt)==0){
              wtBtn = false;
              lcd.clear();
              while(digitalRead(Button_nxt)==0){
              }
            }
            if (digitalRead(Button_dn)==0) 
            {
              delay(200);
              if (digitalRead(Button_up)==0){
                digitalWrite(Heat,LOW);
                digitalWrite(Pump,LOW);
                lcd.clear();
                delay(50);
                mainMenu=0;
                autoEnter = false;
                i = 10;
              }
            }
          }
        }
        if(i==(nmbrStgs-1)){   // at the end of the last step pauses to remove the malt pipe before the boil
          lcd.clear();
          x = nmbrStgs;               // used add to stage count on the final stage for the resume 
          EEPROM.write(36,lowByte(x)); // stores the stage number for the resume
          digitalWrite(Pump,LOW);
          digitalWrite(Heat,LOW);
          lcd.print("  Usun Slod  ");
          lcd.setCursor(0,2);
          Buzzer2();
          lcd.print("Potwierdz");
          wtBtn = true;
          while (wtBtn){
            if (digitalRead(Button_nxt) == 0){
              wtBtn = false;
              lcd.clear();
              while(digitalRead(Button_nxt)==0){
              }
            }
            if (digitalRead(Button_dn) == 0) 
            {
              delay(200);
              if (digitalRead(Button_up) == 0){
                digitalWrite(Heat,LOW);
                digitalWrite(Pump,LOW);
                lcd.clear();
                delay(50);
                mainMenu=0;
                autoEnter = false;
                i=10;
              }
            }
          }

        }          

        Buzzer1(); 
        tempHAddr +=3; // increase stage addresses 
        tempLAddr +=3;
        timeAddr +=3;
        lcd.clear(); 
      }
    }
    // start of the boil
    if(autoEnter){
      start = millis();
      stageTemp= 98.0; // set the intital boil temp to 98 deg c
      boilLoop = true;
      tempReached = false;  
      nmbrHops = EEPROM.read(39);
      if (resume){
        stageTime= EEPROM.read(37);
      }
      else{
        stageTime = EEPROM.read(40);
        EEPROM.write(37,lowByte(stageTime));
      }
      hopAdd = EEPROM.read(49);
      blhpAddr = hopAdd+41;
      lcd.clear();
      hopTime = EEPROM.read(blhpAddr);


      while(boilLoop){
        if(hopAdd <= nmbrHops){
          if (stageTime == hopTime){
            Buzzer2();
            lcd.clear();
            lcd.print("    Dodaj Chmiel");
            delay(2000);
            Buzzer2();
            hopAdd++;
            EEPROM.write(49,hopAdd);
            blhpAddr++;
            hopTime = EEPROM.read(blhpAddr);
            lcd.clear();
          }
        }


        if(stageTime == 0 )boilLoop = false;  
        Temperature();
        Setpoint = stageTemp;
        Input = Temp_c;
        PID_HEAT();
        if(Temp_c >= stageTemp)tempReached =true;

        lcd.setCursor(0,0);
        lcd.print("Gotowanie");
        lcd.setCursor(10,0);
        lcd.print("T=");
        lcd.print(stageTime);        
        lcd.setCursor(0,1);
        lcd.print("S/A=");
        lcd.print(int(stageTemp));
        lcd.print("/");
        lcd.print(Temp_c);
        lcd.write((byte)0);


        if (digitalRead(Button_up)==0){ // quit or increase temp
          delay(200);
          if (digitalRead(Button_dn)==0){
            digitalWrite(Heat,LOW);
            digitalWrite(Pump,LOW);
            mainMenu=0;
            autoEnter = false;
            boilLoop = false;
            lcd.clear();
          }
          else{
            stageTemp++;
            if (stageTemp>120)stageTemp=120;
          }
          while (digitalRead(Button_up)==0){
          }
        }


        if (digitalRead(Button_dn)==0) // quit or decrease temp
        {
          delay(200);
          if (digitalRead(Button_up)==0){
            digitalWrite(Heat,LOW);
            digitalWrite(Pump,LOW);
            mainMenu=0;
            autoEnter = false;
            boilLoop = false;
            lcd.clear();
          }
          else{
            stageTemp--;
            if (stageTemp < 95.0)stageTemp = 95.0;
          }
          while (digitalRead(Button_dn) == 0){
          }
        }
        if ((millis() - start) > 1000){  // timing routine
          start = millis();
          second++;
          if(!(tempReached)) second = 0;// starts counting down when temp reached
          if (second > 59){
            lcd.setCursor(10,0);
            lcd.print("      ");
            second = 0;
            stageTime--;
            EEPROM.write(37,lowByte(stageTime));
          }
        }

      }
    }
    if(autoEnter){    // finishes the brewing process
      lcd.clear();
      lcd.print("    Warzenie     ");
      lcd.setCursor(0,1);
      lcd.print("   Zakonczone     ");
      Buzzer2();
      delay(2000);
      EEPROM.write(35,0); // sets auto start byte to 0 for resume
      EEPROM.write(49,0); // sets hop count to 0
      mainMenu=0;
      autoEnter =false;
      resume =false;
    }


    break;
    case(3):// Setup 
    SetupFun();
    lcd.clear();
    mainMenu=0;
    break; 

  }
}



// reads the DS18B20 temerature probe 

void Temperature(void){
  ds.reset();
  ds.skip();
  // start conversion and return
  if (!(Conv_start)){
    ds.write(0x44,0);
    Conv_start = true;
    return;
  }
  // check for conversion if it isn't complete return if it is then convert to decimal
  if (Conv_start){
    Busy = ds.read_bit();
    if (Busy == 0){
      return;
    }
    ds.reset();
    ds.skip();
    ds.write(0xBE);  
    for ( i = 0; i < 2; i++) {           // we need 2 bytes
      data[i] = ds.read();
    } 
    unsigned int raw = (data[1] << 8) + data[0];
    Temp_c = (raw & 0xFFFC) * 0.0625; 
    Conv_start = false;
    return;
  } 
  return;
}

/************************************************
 * turn the output pin on/off based on pid output
 ************************************************/

void PID_HEAT (void){
  myPID.Compute();

  unsigned long now = millis();
  if(now - windowStartTime>WindowSize)
  {                                     //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output > now - windowStartTime) digitalWrite(Heat,HIGH);
  else digitalWrite(Heat,LOW);

  return;
}

// setup function to set the Brauduino and Auto parameters

void SetupFun(void){
  byte pidMenu = 0;
  byte autoMenu = 0;
  byte setupMenu = 0;
  tempHAddr = 6;
  tempLAddr = 7;
  timeAddr = 8;
  blhpAddr = 40;
  int kpSet,kiSet,kdSet;
  double stgtmpSet;
  word stgtmpSetword;
  word windowSizeSet;
  int stgtimSet;
  byte hopSet;
  boolean setupLoop = true;
  boolean pidLoop = false;
  boolean hopLoop = false;
  boolean autotempLoop = false;
  boolean autotimeLoop = false;
  boolean eepromread = false;


  while (setupLoop){
    switch (setupMenu){ // to select between PID and Auto menu
      case(0):
      lcd.setCursor(4,1); 
      lcd.print("Parametry");
      lcd.setCursor(0,2); 
      lcd.print("Jednostek");

      delay(1500);
      lcd.clear();

      if (digitalRead(Button_up)==0){
        delay(100);
        if (digitalRead(Button_dn)==0){
          setupLoop = false;
          return;
        }
        else{
          setupMenu = 1;
        }
      }
      if (digitalRead(Button_nxt)==0){   // set PID parameters
        kpSet=word(EEPROM.read(0),EEPROM.read(1));
        kiSet=word(EEPROM.read(2),EEPROM.read(3));
        kdSet=word(EEPROM.read(4),EEPROM.read(5));
        windowSizeSet=word(EEPROM.read(33),EEPROM.read(34));
        nmbrStgs = EEPROM.read(38);
        nmbrHops = EEPROM.read(39);
        while(digitalRead(Button_nxt)==0){
        }
        pidLoop = true;
        while(pidLoop){
          switch(pidMenu){
            case(0): //kP setting
            lcd.setCursor(0,1);
            lcd.print("Kp = ");
            lcd.print(kpSet);
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                kpSet++;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                kpSet--;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");                
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              EEPROM.write(0,highByte(kpSet));
              EEPROM.write(1,lowByte(kpSet));
              lcd.setCursor(0,1);
              lcd.print("                ");
              pidMenu = 1;
            }
            break;
            case(1)://ki setting
            lcd.setCursor(0,1);
            lcd.print("Ki = ");
            lcd.print(kiSet);
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                kiSet++;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");              
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                kiSet--;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");              
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              EEPROM.write(2,highByte(kiSet));
              EEPROM.write(3,lowByte(kiSet));
              lcd.setCursor(0,1);
              lcd.print("                ");             
              pidMenu = 2;
            }
            break;
            case(2)://kd setting
            lcd.setCursor(0,1);
            lcd.print("Kd = ");
            lcd.print(kdSet);
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                kdSet++;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                kdSet--;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              EEPROM.write(4,highByte(kdSet));
              EEPROM.write(5,lowByte(kdSet));
              lcd.setCursor(0,1);
              lcd.print("                ");    
              pidMenu = 3;

            }
            break;
            case(3)://window size setting
            lcd.setCursor(0,1);
            lcd.print("Okno = ");
            lcd.print(windowSizeSet);
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                windowSizeSet+=500;
                if (windowSizeSet>5000) windowSizeSet = 5000;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                windowSizeSet-=500;
                if (windowSizeSet<500) windowSizeSet =500;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              EEPROM.write(33,highByte(windowSizeSet));
              EEPROM.write(34,lowByte(windowSizeSet));
              lcd.setCursor(0,1);
              lcd.print("                ");
              pidMenu = 4;

            }
            break;
            case(4)://number of stages setting
            lcd.setCursor(0,1);
            lcd.print("Etapy Zacierania = ");
            lcd.print(int(nmbrStgs));
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                nmbrStgs++;
                if(nmbrStgs>9)nmbrStgs=9;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                nmbrStgs--;
                if (nmbrStgs<1)nmbrStgs =1;
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              EEPROM.write(38,nmbrStgs);
              lcd.setCursor(0,1);
              lcd.print("                ");    
              pidMenu = 5;

            }
            break;
            case(5)://number of hop additons setting
            lcd.setCursor(0,1);
            lcd.print("Etapy Chmielenia = ");
            lcd.print(int(nmbrHops));
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                nmbrHops++;
                if (nmbrHops>8)nmbrHops =8;               
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                setupLoop = false;
                pidLoop = false;
                return;
              }
              else{
                nmbrHops--;
                if (nmbrHops>9)nmbrHops =0;                
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              EEPROM.write(39,nmbrHops);
              lcd.setCursor(0,1);
              lcd.print("                ");
              pidLoop = false;    
              pidMenu = 0;
              lcd.clear();

            }
            break;            


          }
        } 
      }
      break;

      case(1):
      lcd.setCursor(0,0);
      lcd.setCursor(4,1); 
      lcd.print("Parametry");
      lcd.setCursor(-2,2); 
      lcd.print("Automatyczne");
      delay(1500);
      lcd.clear();
      if (digitalRead(Button_dn)==0){
        delay(100);
        if(digitalRead(Button_up)==0){
          setupLoop = false;
          return;
        }
        else{
          setupMenu = 0;
        }
      }
      if (digitalRead(Button_nxt)==0){ // set auto parameters
        while(digitalRead(Button_nxt)==0){
        }
        nmbrStgs = EEPROM.read(38);
        tempHAddr = 6;
        tempLAddr = 7;
        timeAddr = 8;
        blhpAddr = 40;

        for (int i=0; i<nmbrStgs;i++){ // loops for the number of stages 
          stgtmpSet = word(EEPROM.read(tempHAddr),EEPROM.read(tempLAddr));
          stgtmpSet = stgtmpSet/16.1;
          autotempLoop = true;
          while (autotempLoop){  // loops for temp adjust
            lcd.setCursor(0,1);
            lcd.print(stgName[i]);
            lcd.print("Temp=");
            lcd.print(stgtmpSet);
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                autotempLoop = false;
                lcd.setCursor(0,1);
                lcd.print("               ");
                return;
              }
              else{
                stgtmpSet = stgtmpSet+0.25;
                if(stgtmpSet>100.0){
                  stgtmpSet=100.0;
                }
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                lcd.setCursor(0,1);
                lcd.print("               ");                
                setupLoop = false;
                autotempLoop = false;
                return;
              }
              else{
                stgtmpSet = stgtmpSet-0.25;
                if(stgtmpSet<20.0){
                  stgtmpSet=20.0;
                }
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }
              stgtmpSet = stgtmpSet*16;
              stgtmpSetword =word(stgtmpSet);
              EEPROM.write(tempHAddr,highByte(stgtmpSetword));
              EEPROM.write(tempLAddr,lowByte(stgtmpSetword));
              lcd.setCursor(0,1);
              lcd.print("                ");   
              autotempLoop = false; 
            }
          }
          autotimeLoop = true;
          stgtimSet = EEPROM.read(timeAddr);
          while (autotimeLoop){ // loops to adjust time setting

            lcd.setCursor(0,1);
            lcd.print(stgName[i]);
            lcd.print("Czas=");
            lcd.print(stgtimSet);
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                autotimeLoop = false;
                lcd.setCursor(0,1);
                lcd.print("               ");
                return;
              }
              else{
                stgtimSet = stgtimSet+1;
                if(stgtimSet>=120){
                  stgtimSet=120;
                }
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                lcd.setCursor(0,1);
                lcd.print("                ");                
                setupLoop = false;
                autotimeLoop = false;
                return;
              }
              else{
                stgtimSet = stgtimSet-1;
                if(stgtimSet<=0){
                  stgtimSet=0;
                }

                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }

              EEPROM.write(timeAddr,lowByte(stgtimSet));
              lcd.setCursor(0,1);
              lcd.print("                ");    
              autotimeLoop = false;
            }

          }
          tempHAddr+= 3;
          tempLAddr+= 3;
          timeAddr+= 3;
        }
        nmbrHops = EEPROM.read(39);
        nmbrHops+=1;
        for(int i =0;i<nmbrHops;i++){
          hopLoop = true;
          hopSet = EEPROM.read(blhpAddr);
          while (hopLoop){
            if (i==0){
              lcd.setCursor(0,1);
              lcd.print("Czas Gotow.=");
              lcd.print(int (hopSet));
            }
            else{
              lcd.setCursor(0,1);
              lcd.print("Chmiel");
              lcd.print(i);
              lcd.print(" Czas=");
              lcd.print(int(hopSet));

            }
            if (digitalRead(Button_up)==0){
              delay(100);
              if (digitalRead(Button_dn)==0){
                setupLoop = false;
                hopLoop = false;
                lcd.setCursor(0,1);
                lcd.print("               ");
                return;
              }
              else{
                hopSet = hopSet+1;
                if(hopSet>=180){
                  hopSet=180;
                }
                delay(100);
                lcd.setCursor(0,1);
                lcd.print("               ");
              }
            }
            if (digitalRead(Button_dn)==0){
              delay(100);
              if(digitalRead(Button_up)==0){
                lcd.setCursor(0,1);
                lcd.print("                ");                
                setupLoop = false;
                hopLoop = false;
                return;
              }
              else{
                hopSet = hopSet-1;
                if(hopSet<=0){
                  hopSet=0;
                }

                delay(100);
                lcd.setCursor(0,1);
                lcd.print("                ");
              }
            }      
            if (digitalRead(Button_nxt)==0){
              while (digitalRead(Button_nxt)==0){
              }

              EEPROM.write(blhpAddr,hopSet);
              lcd.setCursor(0,1);
              lcd.print("                ");    
              hopLoop = false;
            }
          }
          blhpAddr+= 1;
        }


      }
    }
  }

}

void Buzzer1(void){
  digitalWrite (Buzz,HIGH);
  delay (1000);
  digitalWrite(Buzz,LOW);
  return;
}
void Buzzer2(void){
  digitalWrite(Buzz,HIGH);
  delay(600);
  digitalWrite(Buzz,LOW);
  delay(200);
  digitalWrite(Buzz,HIGH);
  delay(600);
  digitalWrite(Buzz,LOW);
  delay(200);
  digitalWrite(Buzz,HIGH);
  delay(600);
  digitalWrite(Buzz,LOW);
}


Co miesiąc do wygrania nagrody o wartości ponad 1600 zł!


Dabalwwy
Młodszy majsterkowicz
Posty: 41
Rejestracja: 4 wrz 2013, 11:10

Re: Atmega328p-pu i czujnik D1820 nie działa

Post autor: Dabalwwy » 20 lut 2015, 22:00

Na początku zrobiłem wszystko na Leonardo i tu jest ok. Później wszytko przeniosłem na Atmege i podłączyłem w ten samo sposób wszystko(wyswietlacz, przyciski, menu) śmiga prócz tego ze brak wskazań temperatury wiec chyba jest coś nie tak z kodem

Kod: Zaznacz cały

void Temperature(void){
  ds.reset();
  ds.skip();
  // start conversion and return
  if (!(Conv_start)){
    ds.write(0x44,0);
    Conv_start = true;
    return;
  }
  // check for conversion if it isn't complete return if it is then convert to decimal
  if (Conv_start){
    Busy = ds.read_bit();
    if (Busy == 0){
      return;
    }
    ds.reset();
    ds.skip();
    ds.write(0xBE);  
    for ( i = 0; i < 2; i++) {           // we need 2 bytes
      data[i] = ds.read();
    } 
    unsigned int raw = (data[1] << 8) + data[0];
    Temp_c = (raw & 0xFFFC) * 0.0625; 
    Conv_start = false;
    return;
  } 
Dabalwwy
Młodszy majsterkowicz
Posty: 41
Rejestracja: 4 wrz 2013, 11:10

Re: Atmega328p-pu i czujnik D1820 nie działa

Post autor: Dabalwwy » 21 lut 2015, 12:32

Podłączenie czujnika i schematu to była pierwsza żecz jaka zrobiłem i to kilkukrotnie i wszysto jest ok
stiven
Złota rączka
Posty: 1641
Rejestracja: 13 maja 2014, 08:47
Lokalizacja: Zielona Góra

Re: Atmega328p-pu i czujnik D1820 nie działa

Post autor: stiven » 21 lut 2015, 13:45

Trochę dziwna logika, skoro działało na arduino, a po przeniesieniu na atmegę nie działa, więc to coś nie tak z kodem? No ale nigdy nic nie wiadomo. Wyświetlacz masz z czterema linijkami?

Proponuje użyć do tego termometru biblioteki DallasTemperature.h, to jest biblioteka do tego układu ds1820, można ściągnąć stąd: https://github.com/milesburton/Arduino- ... ol-Library.

Wtedy po dodaniu tej biblioteki:

Kod: Zaznacz cały

  #include <DallasTemperature.h>
zamiast tej jednej linijki na początku programu:

Kod: Zaznacz cały

  OneWire ds(9);
trzeba dać coś takiego:

Kod: Zaznacz cały

  #define ONE_WIRE_BUS 9
  OneWire oneWire(ONE_WIRE_BUS);
  DallasTemperature sensors(&oneWire);
dodać w funkcji setup() tą linijkę:

Kod: Zaznacz cały

  sensors.begin();
a funkcja temperature(), będzie teraz wyglądać tak:

Kod: Zaznacz cały

  void Temperature(void){

    sensors.requestTemperatures();
    Temp_c = float(sensors.getTempCByIndex(0));

  }
Dabalwwy
Młodszy majsterkowicz
Posty: 41
Rejestracja: 4 wrz 2013, 11:10

Re: Atmega328p-pu i czujnik D1820 nie działa

Post autor: Dabalwwy » 22 lut 2015, 22:31

Dobra wszystko działa dzięki
ODPOWIEDZ

Strony partnerskie: