Olá estou fazendo um controle de um motor, onde ele reconhece pulsos e aciona a partida e atinge a velocidade conforme está setado o valor do potenciômetro.
O grande problema é que só consegui fazer com que ele pare de 50 ms sem gerar pulsos e gostaria de fazer que ele também pare se os pulsos em um determinado tempo reduzam , ou o tempo entre pulsos aumente em 10% , por exemplo , em um determinado tempo. Podem me ajudar?
//Hardware constants
const int PASPin = 2; // entrada do PAS
const int ledPin = 13, PWMOut=11; // o pino que o LED esta conectado e o pino de saida PWM
const int PinoPotenciometro = A0;
int ValorPot = 0;
int pwm1 = 0;
//Software constants
const unsigned long activityTimeoutMS = 50; // Allowed PAS signal inactivity time before turning off
const int startPulses = 8; // Number of PAS pulses needed before turning on
const int lowPWMValue = 0; // PWM values to drive throttle input, default 56 (1,1 V) and 170 (3,4 V), U=n/255*5V, n=U/5V*255
int highPWMValue =0;
// Variables
volatile int inputEdges = 0; // counter for the number of pulses since last reset
volatile unsigned long lastEdgeTime = 0; // timestamp of last PAS pulse
bool state=false; // variable holding information about the state of the output
void setup() {
pinMode(PASPin, INPUT); // initialize the PAS pin as a input
attachInterrupt(digitalPinToInterrupt(PASPin), pulse, RISING); //Each rising edge on PAS pin causes an interrupt
pinMode(ledPin, OUTPUT); // initialize the LED as an output
pinMode(PWMOut, OUTPUT); // initialize the PWM pin as an output
}
void loop() {
ValorPot = analogRead(PinoPotenciometro);
pwm1 = map(ValorPot, 0, 1023, 0, 255);
//If PAS signal is inactive for too long, turn off everything
unsigned long curTime=millis();
if ((curTime>lastEdgeTime)&&((curTime-lastEdgeTime)>activityTimeoutMS)) {
turnOff();
}
//If system is off, check if the impulses are active
if ((!state)&&((millis()-lastEdgeTime)<activityTimeoutMS)) {
//if impulses are active, check if there were enough pulses to turn on
if (inputEdges>startPulses) {
turnOn();
}
}
//Use LED for status info
digitalWrite(ledPin, state);
}
//Turn off output, reset pulse counter and set state variable to false
void turnOff() {
noInterrupts();
analogWrite(PWMOut,0 );
inputEdges=0;
state=false;
interrupts();
}
//Turn on output and set state variable to true
void turnOn() {
analogWrite(PWMOut, pwm1);
}
//Interrupt subroutine, refresh last impulse timestamp and increment pulse counter (until 10000 is reached)
void pulse() {
lastEdgeTime=millis();
if (inputEdges<100) {
inputEdges++;
}
}
Tags:
Boa noite JW2R, (se não gosta que te chame pelas iniciais, avise),
gostaria muito de te ajudar, mas para isto recomendo as seguintes ações:
1. Remova seu sketch da área de texto do seu tópico;
2. Leia http://labdegaragem.com/forum/topics/sugest-o-de-como-postar
3. Comente as linhas do seu sketch. Fica mais fácil entender o que vc quer fazer com cada
linha e facilita a ajuda.
4. Clique em : " Deseja carregar arquivos? " e depois em " Escolher arquivo" e anexe o arquivo
com o seu sketch.
RV
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por