Кейс Автополив растений

Пример кода 1

#define Pump_Pin 11
#define Sensor1_Pin A0
int Sensor1 = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
  Serial.begin(9600);
  pinMode(Pump_Pin, OUTPUT);
}
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    Sensor1 = analogRead(Sensor1_Pin);
    Serial.print("Sensor 1 ");
    Serial.println(Sensor1);
    if (Sensor1 >= 100 && Sensor1 <=300)
    {
      analogWrite(Pump_Pin, 255);
      Serial.println("Pump ON");
      
    }
  }
}

Пример кода 2

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define Pump_Pin 11
#define Sensor1_Pin A0
#define LED_PIN    4
#define LED_COUNT 16
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); int Sensor1 = 0;
unsigned long previousMillis = 0;
const long interval = 3000;
void setup() {
  Serial.begin(9600);
  pinMode(Pump_Pin, OUTPUT);
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  strip.begin();
  strip.show();
  strip.setBrightness(50);
}
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    Sensor1 = analogRead(Sensor1_Pin);
    Serial.print("Sensor 1 ");
    Serial.println(Sensor1);
    if (Sensor1 >= 300 && Sensor1 <= 500)
    {
      analogWrite(Pump_Pin, 255);
      Serial.println("Pump ON");
      theaterChaseRainbow(50);
      //      delay(1000);
    }
    else
    {
      colorWipe(strip.Color(0, 40, 0), 0);
      analogWrite(Pump_Pin, 0);
    }
  }
}
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  for (int a = 0; a < 30; a++) { // Repeat 30 times...
    for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in increments of 3...
      for (int c = b; c < strip.numPixels(); c += 3) {
        // hue of pixel 'c' is offset by an amount to make one full
        // revolution of the color wheel (range 65536) along the length
        // of the strip (strip.numPixels() steps):
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);                 // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
  }
}
void colorWipe(uint32_t c, uint8_t wait)
{
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    // заполняем текущий сегмент выбранным цветом
    strip.setPixelColor(i, c);
    strip.show();
    // ждём
    delay(wait);
  }
}

Пример кода 3

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define Pump_Pin 11
#define Sensor1_Pin A0
#define LED_PIN    4
#define LED_PIN2    6
#define Button_Pin 3
#define LED_COUNT 16
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); int Sensor1 = 0;
unsigned long previousMillis = 0;
const long interval = 3000;
void setup() {
  Serial.begin(9600);
  pinMode(Pump_Pin, OUTPUT);
  pinMode(Button_Pin, INPUT);
  pinMode(LED_PIN2, OUTPUT);
  digitalWrite(LED_PIN2, HIGH);
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  strip.begin();
  strip.show();
  strip.setBrightness(50);
  delay(1000);
  digitalWrite(LED_PIN2, LOW);
}
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    Sensor1 = analogRead(Sensor1_Pin);
    Serial.print("Sensor 1 ");
    Serial.println(Sensor1);
    if (Sensor1 >= 300 && Sensor1 <= 500)
    {
      analogWrite(Pump_Pin, 255);
      Serial.println("Pump ON");
      theaterChaseRainbow(50);
      //      delay(1000);
    }
    else
    {
      colorWipe(strip.Color(0, 40, 0), 0);
      analogWrite(Pump_Pin, 0);
    }
  }
}
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  for (int a = 0; a < 30; a++) { // Repeat 30 times...
    for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
      strip.clear();
      for (int c = b; c < strip.numPixels(); c += 3) {
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);                 // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
  }
}
void colorWipe(uint32_t c, uint8_t wait)
{
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    // заполняем текущий сегмент выбранным цветом
    strip.setPixelColor(i, c);
    strip.show();
    // ждём
    delay(wait);
  }
}

У этой записи один комментарий

Добавить комментарий