Uma ação durante X tempo

Bom Dia a todos

Bem eu tenho um projeto que é um cata-vento e eu quero saber depois a velocidade do vento.
Então eu coloquei um sensor Hall numa das pás e quando este passa por o iman do suporte completa uma volta ai tudo bem...No entanto a minha maneira lógica para os cálculos foi:

A variável X assume o valor do contador durante o tempo de 5000milissegundos depois passado 2000milissegundos repete infinitamente ou neste caso até a apresentação do projeto acabar ahahahhaha mas na mesma o exemplo tem de ser infinito executando enquanto estiver ligado.

Assumindo a minha lógica pensei em fazer:

time = millis()
do
x = counter
while (time < 5000)
mas desta forma só vai contar uma vez pois o millis vai contando quase infinitamente nao sei bem quanto tempo tem até dar reset mas sei que é muito... será que alguem consegue me ajudar ou então que tenha outra forma de pensar??

Eu pensei em juntar um IF que seria IF time = 5000 then time = 0 mas nao sei bem como juntar isto tudo nem sei se dá :confused:

Cumprimentos, Gonçalo

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Sua postagem foi MOVADA para o local atual, pois é mais adequada.

Você também pode dedicar alguns minutos para [url = https: //forum.arduino.cc/index.php? Topic = 710766.0] Aprenda a usar o fórum [/ url].

Outra [url = https: //support.arduino.cc/hc/en-us] ajuda geral e conselhos para solução de problemas podem ser encontrados aqui. [/ Url]
Isso o ajudará a tirar o melhor proveito do fórum no futuro.

please post the code using </>

Im new and i can´t upload attachments so i paste the code here

// declare a threshold value on top of the sketch
int threshold = 500; // change value to suit

int hallSensor_pin = A0; //function used to define the pin used
int hallSensorValue = analogRead(hallSensor_pin);
//int hallSensorStatus[10];

//int addcount(int);

// --- Const---
//const float pi = 3.14159265359; //PI number
//int period = 5000; //Measurement time (milliseconds)
int delaytime = 2000; //Interval between samples (milliseconds)
int radius = 0.155; //Anemometer radius (metros)/ 155 mm

// --- Global ---
unsigned int Sample = 0; //Stores the number of samples
unsigned int counter = 0; //counter for the sensor
unsigned int RPM = 0; //rpm
float speedwind = 0; //Wind speed (m/s)
float windspeed = 0; //Wind speed (km/h)

unsigned long tempo;

void setup()
{
Serial.begin(19200);
}

void loop()
{
Serial.println("");
Serial.print("VALOR HALLSENSOR:");
Serial.println(hallSensorValue);
Sample++;
Serial.print(Sample);
Serial.print(": Start reading...");
windvelocity();
Serial.println(" done.");
Serial.print("Counter: ");
Serial.print(counter);
Serial.print("; RPM: ");
RPMcalc();
Serial.print(RPM);
Serial.print("; Wind speed: ");

//*****************************************************************
//print m/s
WindSpeed();
Serial.print(windspeed);
Serial.print(" [m/s] ");

//*****************************************************************
//print km/h
SpeedWind();
Serial.print(speedwind);
Serial.print(" [km/h] ");
Serial.println();

delay(delaytime);
} //end setup

//Function to measure wind speed
void windvelocity()
{
speedwind = 0;
windspeed = 0;

//counter = 0;
//attachInterrupt(A0, addCount, RISING);
addCount();
/*
unsigned long millis();
long startTime = millis();
while (millis() < startTime + period) {}
*/
}

void addCount()
{
if (hallSensorValue <= 900)
{
counter++;
}
}

//Calculate revolutions per minute (RPM)
void RPMcalc()
{
tempo = millis();
do {
RPM = counter();

}while (tempo < 5001);

}

//Calculates wind speed in m/s
void WindSpeed()
{
windspeed = 0,9738937226 * RPM;
} //end WindSpeed

//Calculates wind speed in km/h
void SpeedWind()
{
speedwind = windspeed * 3.6;
} //end SpeedWind

/*
//Increment counter
void addcount() {
counter++;
}
*/

It doesn't compile.

There are several other problems, among them this:

  if (hallSensorValue <= 900)
  {
    counter++;
  }

You need to analogRead to get the hallSensor value. As it is, you read it once at startup and never again.

so i need to define again after void addCount ?

Well read again, yes.

Ok thanks that part i didn't know i was wrong :slight_smile: but about the main quest you have some ideias??

Things should change once you are reading the hall sensor properly, so it's probably worth testing again.

I'd be inclined to write a simpler program that just counts up when it sees the hall sensor. When that's working use millis to measure that five seconds have passed and calculate windspeed and zero the counter.

Okay would it be something like this??

if hallSensorValue <= 900)

counter++

time = millis

if millis > 5000 then

rpm = count
time = 0
count = 0

It is written in "sketch" missing all syntax rules

time = millis before first if

Seriously, take a copy of your sketch and make it do nothing but count pulses from the hall sensor and serial print the results.

Trying to do the whole thing in one go is going to be a recipe for frustration.

Aqui vai um código simples de uma função intervalometro.
Talvez possa ajudar:

//definiçoes antes do setup
#define intervaloX 2000 //periodico
#define intervaloY 1000 //duração do evento
unsigned long int timeX = 0;
unsigned char run = 0;
// run é a variável que indica o estado do evento : 0= aguardando
// 1= evento iniciado.

//abaixo a função.
void intervalometro() {
if (millis() - timeX > intervaloX) {
timeX = millis(); //intervalo periódico
run = 1;
}
if (run == 1) {
if (millis() - timeX > intervaloY) {
run = 0; // duração do evento
}
}
}

a função pode ser inicializada no loop()
chamando:

intervalometro();

ok ok acho que é isto mesmo que preciso!!! durante um certo intervalo que eu defeni (5sec) vai acontecer algo neste caso o contador contar e depois assim que passa o intervalo defenido ele dá reset e volta ao inicio "infinitamente"

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.