Hi guys? How are you doing?
I'm new at programming so i don't know some functions that can help with this problem.
My project is an automatic animal fedding that when the timer hits 10 secs, the progam verify the weight in load cell. If less than 0,5kg it turns on the motor, if is higher it won't let turn on, keeps is false.
But the problem is that i don't know how to make the condition "turn off the motor" running every time while the rest of the code respect the timer of 10sec.
What's happing now: I tried two types of codes, one more complex than the other, the simple one turn the motor on, but keeps this value until the next verification, what means that if the weight pass the 0,5kg in this 10sec interval, the motor don't turn off.
The complex one don't change the state of the motor, keeps always running.
So i really don't know how to make it work, i read a lot of sites, but nothing help me ![]()
I'm realy thankful for any kind of suggestion! Thanks a lot.
Complex One:
#include "HX711.h"
#define DT A1
#define SCK A0
HX711 scale;
void StartMotor();
void StopMotor();
void wait();
unsigned long timeMotor=0;
unsigned long timeStop=0;
bool comutarSMOT = false;
bool comutarSM = false;
void setup() {
scale.begin (DT, SCK);
pinMode (7,OUTPUT);
Serial.begin(9600);
Serial.print("Leitura do Valor ADC: ");
Serial.println(scale.read()); // Wait until the device is ready
Serial.println("Nao coloque nada na balanca!");
Serial.println("Iniciando...");
scale.set_scale(2428.33333); // Substitue the value for set scale
scale.tare(20); // Set tare.
Serial.println("Insira o item para Pesar");
}
void loop() {
StartMotor();
StopMotor();
wait();
}
void StartMotor(){
if(comutarSMOT){
Serial.print("Peso: ");
Serial.print(scale.get_units(20), 3);
Serial.println(" kg ");
if ((scale.get_value(2)) < 2428.33333) {
digitalWrite (7, HIGH);
timeMotor=millis();
comutarSMOT = false;
}
}
}
void StopMotor(){
if(comutarSM){
if ((scale.get_value(2)) >= 2428.33333) {
digitalWrite (7, LOW);
timeStop=millis();
comutarSM = false;
}
}
}
// Turn on the variable StartMotor
void wait(){
if(millis()-timeMotor>=10000){
comutarSMOT = true;
}
// Turn off the variable StopMotor
if(millis()-timeStop<=100){
comutarSM = true;
}
}
Simple One:
#include "HX711.h" //Inclui a bilioteca do sensor de peso
#define DT A1 // Define os pinos referntes no arduino
#define SCK A0 // Define os pinos referentes no arduino
HX711 escala; // Define o nome escala como referêncial
void setup() {
escala.begin (DT, SCK); //Inicia os pinos referidos
pinMode (7,OUTPUT); //Configura o pino para saída (OUTPUT).
Serial.begin(9600); // Define velocidade do Serial
Serial.print("Leitura do Valor ADC: "); // Mostra no serial o texto entre ""
Serial.println(escala.read()); // Aguada até o dispositivo estar pronto
Serial.println("Nao coloque nada na balanca!"); // Mostra no serial o texto entre ""
Serial.println("Iniciando..."); // Mostra no serial o texto entre ""
escala.set_scale(109182); // Substituir o valor encontrado para escala
escala.tare(20); // Define o peso base incial (tara).
Serial.println("Insira o item para Pesar"); // Mostra no serial o texto entre ""
}
void loop() {
Serial.print("Peso: "); // Mostra no serial o texto entre ""
Serial.print(escala.get_units(5), 3); // Seleciona os valores medidos e aplica uma divisão para converter ao formato de kg
Serial.println(" kg "); // Mostra no serial o texto entre ""
if ((escala.get_value(5)) < 109182) { // Condição, se o valor medido é menor que o setado, liga o pino 7 (Motor)
digitalWrite (7, HIGH);
}
else {
digitalWrite(7,LOW); // Se o valor medido for maior, desliga o pino 7 (Motor)
}
}
Thanks!!!