Hello, I am currently carrying out an internship assignment where I have to automate a door for a solar-powered chicken coop.
I am currently using an Arduino UNO board, a 2 channel relay module and a 3.7V lipo battery.
The door is opened and closed using a motor that turns in either direction and rolls or unwinds a wire that opens or closes the door.
There I have to do a program loop where:
- When starting, relay 1 is energized for 10 seconds, where the motor gradually closes the door.
-10 seconds later, the 2 relays are deactivated and the motor is paused for 5 seconds.
-Then, relay 2 is energized for 10 seconds, which turns the motor in the other direction and gradually opens the door.
-10 seconds later, another engine pause for 5 seconds.
And so on.
Now I want to do a parallel action where I have to measure the voltage with an analog pin to check the motor consumption. The goal is to take a measurement every 250 milliseconds.
Here is the program I performed:
int RelayPin1 = 6;
int RelayPin2 = 7;
int valeur = analogRead(A0); // Mesure la tension sur la broche
void setup() {
// On met les pin de chaque relais en sortie
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
// Initialise la communication avec le PC
Serial.begin(9600);
}
void loop() {
// Sous traite les différentes tâches
task_moteur1();
task_moteur2();
}
void task_moteur1() {
// Transforme la mesure (nombre entier) en tension via un produit en croix
float tension = valeur * (5.0 / 1023.0);
// Envoi la mesure au PC pour affichage
Serial.println(tension);
delay(250);
}
void task_moteur2() {
// On allume l'un des relais alors que l'autre est éteint...
digitalWrite(RelayPin1, LOW);
digitalWrite(RelayPin2, HIGH);
delay(10000);
// On éteint ensuite le ler relais: le moteur est en pause pendant 5 secondes...
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
delay(5000);
// Puis on allume l'autre...
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, LOW);
delay(10000);
// Enfin, on éteins le 2ème relais: le moteur est encore en pause pendant 5 secondes...
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
delay(5000);
}
The problem is that when I test my program, the voltage is only measured whenever relay 1 is activated. Either every 30 seconds.
I would then like to create a non-blocking program, and I have heard of the millis () function. The problem is, I don't know anything about this function.
Could someone help me ?!
Thank you in advance.