Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15514

Aquarium automation : Program Freezing Issues

$
0
0

Hello everyone,

I'm starting to get to know Arduino programming and prototyping electronics well, but I need help for the first time. That's why I'm reaching out to you, hoping to find a solution.

I'm currently automating various actions on my aquarium, namely:

  • Distributing fertilizer once a day using a peristaltic pump (stepper motor + DRV8825)
  • Filling the aquarium using a non-contact sensor Xkc-Y25-Npn and a peristaltic pump (DC motor + IRF540N assembly)
  • Turning on the light using a relay at time H
  • Turning on the CO2 using a relay at time H-30 min

To do this, I've designed a PCB that controls all of this using an ATMEGA328P setup, an RTC module for time, and an FTDI for uploading the program. There are also 2 LEDs for debugging and the following buttons:

  • A button to reset the board
  • A button to update the time of the RTC module
  • A button to manually turn on the light and CO2 at the same time
    -A button to start the aquarium filling, which stops automatically

Unfortunately, I don't understand why, but the program freezes sometimes... So the light doesn't turn on, which is not good at all for my fish and plants if I go on vacation. In fact, I just left for 3 days, it bugged and I lost a fish... I feel like the problem occurs between turning on the CO2 and the LED, and it might be due to the RTC module, but I'm really not sure. I specify that the control and power circuits have two different power supplies. Does anyone have an idea of the origin of my problem?

Below is my program along with images of the electrical circuit. Feel free to ask if you have any questions or comments on my post, this is the first time I've done this! Thank you in advance!

#include <Wire.h>
#include <RTClib.h>
#include "AccelStepper.h"

RTC_DS3231 rtc;

#define dirPin 10
#define stepPin 9
int enablePin=8;
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

int pinNiveau=0;
int pinLedRouge=12;
int pinLedVerte=7;
int pinMoteur=11;

const int RelayLedPin = 6;
const int RelayCO2Pin = 5;

const int pinBout1=3; //LED
const int pinBout2=4; //OSMOLATEUR
const int pinBout3=2; //RESET TIME

int passageEngrais=0;

// Définir les moments de début et de fin pour l'allumage de la LED
int startHour = 13;   // Heure de début
int startMinute = 00;  // Minute de début
int endHour = 19;     // Heure de fin
int endMinute = 00;   // Minute de fin

int startHour3 = 00;   // Heure de début
int startMinute3 = 00;  // Minute de début
int endHour3 = 00;     // Heure de fin
int endMinute3 = 00;   // Minute de fin

int startHour2 = 18;   // Heure de début
int startMinute2 = 00;  // Minute de début
int endHour2 = 23;     // Heure de fin
int endMinute2 = 00;   // Minute de fin
int jour = 3;   // Jour (4 = jeudi / 0 = dimanche)
int passageNiveau=0;
int i=0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //initialise la communication série
  
  stepper.setMaxSpeed(1000);

  pinMode(pinMoteur,OUTPUT);
  digitalWrite(pinMoteur,LOW);

  pinMode(pinLedRouge,OUTPUT);
  digitalWrite(pinLedRouge,LOW);
  pinMode(pinLedVerte,OUTPUT);
  digitalWrite(pinLedVerte,LOW);

  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);

  pinMode(RelayLedPin, OUTPUT);
  digitalWrite(RelayLedPin, LOW);
  pinMode(RelayCO2Pin, OUTPUT);
  digitalWrite(RelayCO2Pin, LOW);

  pinMode(pinBout1,INPUT_PULLUP);
  pinMode(pinBout2,INPUT_PULLUP);
  pinMode(pinBout3,INPUT_PULLUP);

if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  else{
    delay(1000);
    Serial.println("Connection réussie");
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  afficherTemps();

  if (startMinute<30){
    startMinute3=60-(30-startMinute);
    startHour3=startHour-1;
  }
  else {
    startMinute3=startMinute-30;
    startHour3=startHour; 
  }
  if (endMinute<30){
    endMinute3=60-(30-endMinute);
    endHour3=endHour-1;
  }
  else {
    endMinute3=endMinute-30;
    endHour3=endHour; 
  }
}

void loop() {
  Serial.println("test");

  if (digitalRead(pinBout3)==0){
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    afficherTemps();
    delay(1000);    
  }
  if (digitalRead(pinBout1)==1){
    verifHeure ();
  }
  else {
    digitalWrite(RelayLedPin, HIGH);
    digitalWrite(RelayCO2Pin, HIGH);
  }
  if (digitalRead(pinBout2)==1){
      if (analogRead(pinNiveau)>5 && passageNiveau==0){
        digitalWrite(pinMoteur,HIGH); //le moteur se lance
        digitalWrite(pinLedRouge,HIGH);
        passageNiveau=1;

      }
      if (passageNiveau==1 && analogRead(pinNiveau)<5){
        i++;
      }
      else{
        i=0;
      }
      if (i==5){
        i=0;
        digitalWrite(pinMoteur,LOW); // le moteur s'arrête
        digitalWrite(pinLedRouge,LOW);
      }
  }
  else {
    digitalWrite(pinLedRouge,LOW);
    verifNiveau();
  }
Engrais();
delay(1000);  // Attendez une seconde avant de vérifier à nouveau
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Engrais (){
  DateTime now = rtc.now();
  if (now.hour() == startHour && now.minute()==startMinute+5 && passageEngrais==0) {
  digitalWrite(pinLedVerte,HIGH);
  digitalWrite(enablePin, LOW);
  delay(1000);
  stepper.setCurrentPosition(0);
  while(stepper.currentPosition() != -60000)
  {
    stepper.setSpeed(-2000);
    stepper.runSpeed();
  }
  delay(500);
  stepper.setCurrentPosition(0);
  while(stepper.currentPosition() != 35200)
  {
    stepper.setSpeed(2000);
    stepper.runSpeed();
  }
  delay(500);
  stepper.setCurrentPosition(0);
    while(stepper.currentPosition() != 6000)
  {
    stepper.setSpeed(400);
    stepper.runSpeed();
  }
  delay(500);
  stepper.setCurrentPosition(0);
    while(stepper.currentPosition() != -2000)
  {
    stepper.setSpeed(-400);
    stepper.runSpeed();
  }
  delay(500);
  passageEngrais=1;
  digitalWrite(pinLedVerte,LOW);
  digitalWrite(enablePin, HIGH);
  }
  if (now.hour() == startHour+1){
    passageEngrais=0;
  }
}
void verifNiveau (){
  DateTime now = rtc.now();
 if (now.dayOfTheWeek()==jour && (now.hour() > startHour2 || (now.hour() == startHour2 && now.minute() >= startMinute2)) &&
      (now.hour() < endHour2 || (now.hour() == endHour2 && now.minute() <= endMinute2))) {
        //Serial.println(analogRead(pinNiveau));
  }
  else {
    passageNiveau=0;
    digitalWrite(pinMoteur,LOW); // le moteur s'arrête
  }
}
void verifHeure (){
  DateTime now = rtc.now();
  
    // Vérifiez si l'heure actuelle est entre 10h et 16h
  if ((now.hour() > startHour || (now.hour() == startHour && now.minute() >= startMinute)) &&
      (now.hour() < endHour || (now.hour() == endHour && now.minute() <= endMinute))) {
    digitalWrite(RelayLedPin, HIGH);
  }
  else {
    // Éteindre la LED
    digitalWrite(RelayLedPin, LOW);
  }
  if ((now.hour() > startHour3 || (now.hour() == startHour3 && now.minute() >= startMinute3)) &&
      (now.hour() < endHour3 || (now.hour() == endHour3 && now.minute() <= endMinute3))) {
    digitalWrite(RelayCO2Pin, HIGH);
  }
  else {
    // Éteindre la LED
    digitalWrite(RelayCO2Pin, LOW);
  }
}
void afficherTemps(){
  Serial.println("Date et heure d'initialisation :");
  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}


17 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 15514

Trending Articles