Close

Code UltraTower V2

A project log for UltraTower 2023

Grow vegetables with ultraponics tower

j-gleyzesJ Gleyzes 07/04/2023 at 09:230 Comments

You can find the new code in the github associated with this project or by following this link.

The major change compared to the first code is the search for the best frequency to make the discs vibrate.

I use 16 mm discs with a resonance frequency of 108 kHz +/- 3 kHz.

The disc may also change frequency slightly as it ages. That's why, at Esp32 start-up, the code now analyzes the current consumption of each frequency in the 108kHz +/-3 kHz interval.

The code is as follows when the esp32 has found the consumption peak, it will use this frequency until it restarts (forced or automatic every 24 hours) 

void searchForBestFrequency (int mistPin) {

  int32_t buf[ADC_BUFFER_SIZE];
  int minFreq = frequencyMisterDefault - 3000;
  int maxFreq = frequencyMisterDefault + 3000;
  int freqMaxADC = 0;
  int minADC = 0;
  int maxADC = 0;
  int32_t sample = 0;

 // analogSetPinAttenuation(ADC_PIN, ADC_0db);


  for (int i = minFreq; i < maxFreq ; i += 250)
  {

    ledcSetup(LED_CHANNEL_MIST_TEST, i , LED_CHANNEL_RESOLUTION);
    ledcAttachPin(mistPin, LED_CHANNEL_MIST_TEST);
    ledcWrite(LED_CHANNEL_MIST_TEST, LED_CHANNEL_DUTYCYCLE);

    Serial.println("************************************");
    Serial.print("Freq : ");
    Serial.println(i);

    delay(500);


    for (int j = 0; j < ADC_BUFFER_SIZE; j++)
    {
      buf[j] = analogRead(ADC_PIN);
      delay(4);
    }

    sample = getAverage(buf , ADC_BUFFER_SIZE);


    Serial.print("ADC : ");
    Serial.println(sample);

    if (i == minFreq)
    {
      minADC = sample;
      maxADC = sample;
      freqMaxADC = minFreq;
    } else
    {
      if (sample + OFFSET_ADC < minADC) {
        minADC = sample;
      }

      if (sample - OFFSET_ADC > maxADC ) {
        maxADC = sample;
        freqMaxADC = i;

      }
    }
  }


  Serial.println("************************************");
  Serial.print("MAX Freq = ");
  Serial.println(freqMaxADC);
  Serial.print("MIN ADC = ");
  Serial.println(minADC);
  Serial.print("MAX ADC = ");
  Serial.println(maxADC);

  if (maxADC <= 80 && maxADC - minADC <= 70) {

    if (maxADC <= 80) {
      Serial.println("ERROR INIT MIST : DISK NOT DETECTED");
    } else {
      Serial.println("INIT MIST : PEAK DETECTED");
    }

    blinkLED(LED_ERROR_PIN);
    if (mistPin == MIST1_PIN) {
      statutMist1 = MIST_ERROR;
    } else {
      statutMist2 = MIST_ERROR;
    }

  } else {
    Serial.println("INIT MIST : PEAK DETECTED");

    if (mistPin == MIST1_PIN) {
      freqMist1 = freqMaxADC;
      statutMist1 = MIST_OK;
      ledcSetup(LED_CHANNEL_MIST_1, freqMist1 , LED_CHANNEL_RESOLUTION);
    } else {
      freqMist2 = freqMaxADC;
      statutMist2 = MIST_OK;
      ledcSetup(LED_CHANNEL_MIST_2, freqMist2 , LED_CHANNEL_RESOLUTION);
    }
  }

}

Discussions