2

I'm creating a controller for a small greenhouse, I have a DHT22 for measuring and two relay module that controls heating and cooling systems, and I will use a piezo to produce humidity when it goes below X amount. This piezo works at 1.7 MHz and I know that Arduino can provide 1.779Mhz, I think It's close enough. After that I'll place a MOSFET at the output and then connect the piezo.

I'm quite new so I'm trying copy-paste some codes and this is what I've done:

I don't know how to turn on and off the frequency generator, I would like to know the code for that

           #include <DHT.h>
            #include <DHT_U.h>
        #define DHTPIN 6 
        #define DHTTYPE DHT11 //Define sensor as DHT11

        #define RELAY1  4  // Relay heating
        #define RELAY2  5  // Relay cooling

        int red = 8; // red LED heating
        int blue = 9; // blue LED cooling

        DHT dht(DHTPIN, DHTTYPE);

        float temperature;
        float humidity;

        // Frequency Generator code
        const uint8_t OUTPUT_PIN = 3;  // = OC2B pin inamovible
        const uint8_t PERIOD = 9;      // 9 CPU cycles ~ 1.778 MHz

        void setup()
        {

          pinMode(red, OUTPUT);
          pinMode(blue, OUTPUT);
          pinMode(RELAY1, OUTPUT);
          pinMode(RELAY2, OUTPUT);

          // Freq generator code
            pinMode(OUTPUT_PIN, OUTPUT);
            TCCR2B = 0;           // stop timer
            TCNT2  = 0;           // reset timer
            TCCR2A = _BV(COM2B1)  // non-inverting PWM on OC2B
                   | _BV(WGM20)   // fast PWM mode, TOP = OCR2A
                   | _BV(WGM21);  // ...ditto
            TCCR2B = _BV(WGM22);  // ...ditto
            OCR2A = PERIOD - 1;
            OCR2B = PERIOD/2 - 1;
        }

        void soundBuzzer() {
            TCCR2B |= _BV(CS20);  // F_CPU / 1
        }

        void silenceBuzzer() {
            TCCR2B &amp;= ~_BV(CS20);
            TCNT2 = 0;
        }
          //End of freq generator code
        void loop()
        {
          delay(1000); //delay time between measurements. 1 sec for DHT11

          temperature = dht.readTemperature(); //Reads temperature
          humidity = dht.readHumidity(); //Reads humidity

         if(temperature &lt; 25)
            {
        digitalWrite(red,HIGH);
        digitalWrite(blue,LOW);


        digitalWrite(RELAY1,0); 
        digitalWrite(RELAY2,1);   
             }
         else if(temperature &gt; 25)
            {
        digitalWrite(RELAY1,1);
        digitalWrite(RELAY2,0); 
        digitalWrite(red,LOW);
        digitalWrite(blue,HIGH);

        }

    if(humidity &lt; 30)
     {

TCCR2A |= _BV(COM2B1); } else if(humidity > 35) { TCCR2A &= ~_BV(COM2B1);

        }
    }

I don't even know if i'm doing it right, please let me know and thanks

Edgard
  • 21
  • 3

0 Answers0