GALERA ESTOU COM UM PROBLEMA, ESTOU FAZENDO UM PROGRAMA PARA CONTROLAR O MOTOR DE PASSO, 2 INTERRUPÇÕES PARA LER O RPM DE 2 MOTORES E MOSTRAR A RPM NO SERIAL... FUNCIONA NORMAL MAS QUANDO EU COLOCO A FUNÇÃO PARA CONTROLAR O MOTOR DE PASSO NÃO É ENVIADO MAIS NENHUM DADO NA SERIAL... ALGUÉM SABE O PQ?


#include <Stepper.h>

const int stepsPerRevolution = 48; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// read RPM
int half_revolutions = 0;
int rpm = 0;
unsigned long lastmillis = 0;

int half_revolutions1 = 0;
int rpm1 = 0;
unsigned long lastmillis1 = 0;
void setup(){
myStepper.setSpeed(150);
Serial.begin(115200);
attachInterrupt(0, rpm_fan, FALLING);
attachInterrupt(1, rpm_fan, FALLING);
pinMode(6, OUTPUT);
}
void loop(){
analogWrite(8, 255);
motordepasso();
if (millis() - lastmillis == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = half_revolutions * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use half_revolutions * 30.
Serial.print("RPM RODA=\t"); //print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(half_revolutions); //print revolutions per second or Hz. And print new line or enter.
if(rpm>10){digitalWrite(12,HIGH);}else{digitalWrite(12,LOW);}
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
}

if (millis() - lastmillis1 == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(1);//Disable interrupt when calculating
rpm1 = half_revolutions1 * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use half_revolutions * 30.
Serial.print(" RPM MOTOR =\t"); //print the word "RPM" and tab.
Serial.print(rpm1); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(half_revolutions1); //print revolutions per second or Hz. And print new line or enter.
half_revolutions1 = 0; // Restart the RPM counter
lastmillis1 = millis(); // Uptade lasmillis
attachInterrupt(1, rpm_fan1, FALLING); //enable interrupt
}

}
// this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan(){
half_revolutions++;
}
// this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan1(){
half_revolutions1++;
}
void motordepasso(){
myStepper.step(stepsPerRevolution);
// delay(10);
myStepper.step(-stepsPerRevolution);
//delay(10);
}

Exibições: 726

Responder esta

Respostas a este tópico

Oi HF, boa tarde.

Tem 2 erros  em 2 linhas do seu code.

Nesta:  if (millis() - lastmillis == 1000)

e nesta  if (millis() - lastmillis1 == 1000)  

Os erros são iguais.

Primeiro, falta parêntesis na subtração, deveria ser assim :   if ((millis() - lastmillis) == 1000) {

depois você testa um valor exato "== 1000",  se for maior que isto, e muito provavelmente será,

o if não será satisfeito

Modifique ambas linhas para >=;    ficando assim:     if ((millis() - lastmillis) >= 1000) {      

e     if ((millis() - lastmillis1) >= 1000) {

Isto deverá resolver seu problema.

Rui

Graças ao RUI VIANA, segue a solução. 

Substituir : if (millis() - lastmillis == 1000){

por: if ((millis() - lastmillis) >= 1000) {

Muito Obrigado Rui, você é o cara!

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço