i have a problem..
i have 2 pushbutton 1= for rpm speed counter and 2 is for motor control PMW
if i run pushbutton 1 with no push the 2 the attachinterrupt works FINE!
if i run first pushbutton 2 and after pushbutton 1 the attachinterrupt going a crazy number ?
my code
#include <LiquidCrystal.h>
int ledPin = 13; //IR TX
int readrpm;
int rpmpin = 6; //pushbutton 1
int inPin = 4; ///pushbutton 2
int state = 0;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;
int counter = 0;
int count = 0;
const int analogInPin = A0;
const int analogOutPin = 5;
int sensorValue = 0;
int outputValue = 0;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
volatile float timer = 0;
volatile float time_last = 0;
volatile int rpm_array[5] = { 0,0,0,0,0};
void setup() {
lcd.begin(16, 2);
pinMode(inPin, INPUT);
pinMode(rpmpin, INPUT);
attachInterrupt(0, fan_interrupt, FALLING);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
}
void loop() {
readrpm = digitalRead(rpmpin);
if (readrpm == HIGH)
rpms();
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == 1){
state = 0;
}
else {
state = 1;
}
time = millis();
}
if (state == 1){
sensorValue = analogRead(analogInPin);
}
else {
sensorValue = 0;
}
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
float resistor = sensorValue * (10.0 / 1023.0);
float pulseRatio = (outputValue * (10000.0 / 255.0))/100.0;
float volt = outputValue * (5.0 / 255.0);
lcd.setCursor(0, 0);
lcd.print(F("Rin = "));
lcd.print(resistor);
lcd.print(F(" Kohm "));
lcd.setCursor(0, 1);
lcd.print(F("PWM = "));
lcd.print(int(pulseRatio));
lcd.print(F(" % "));
delay(350);
if (counter == 35){
lcd.clear();
while (count < 2 ){
for (int i=16; i > 0; i--)
{
readrpm = digitalRead(rpmpin);
if (readrpm == HIGH)
rpms();
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == 1){
state = 0;
}
else {
state = 1;
}
time = millis();
}
if (state == 1){
sensorValue = analogRead(analogInPin);
}
else {
sensorValue = 0;
}
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
float volt = outputValue * (5.0 / 255.0);
lcd.setCursor(0, 0);
lcd.print(F("PWMout = "));
lcd.print(volt);
lcd.print(F(" V "));
lcd.setCursor(i, 1);
lcd.print(F("|Stratos r00t| "));
delay(400);
}
lcd.clear();
previous = reading;
count++;
if (count == 2){
counter = 0;
}
//delay(300);
}
count = 0;
lcd.clear();
}
previous = reading;
counter++;
}
void rpms() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Curent RPM : "));
int rpm = 0;
while(1){
//Slow Down The LCD Display Updates
delay(400);
//Clear The Bottom Row
lcd.setCursor(0, 1);
lcd.print(" ");
//Update The Rpm Count
lcd.setCursor(0, 1);
lcd.print(rpm);
////lcd.setCursor(6, 1);
////lcd.print(timer);
//Update The RPM
if(timer > 0)
{
//5 Sample Moving Average To Smooth Out The Data
rpm_array[0] = rpm_array[1];
rpm_array[1] = rpm_array[2];
rpm_array[2] = rpm_array[3];
rpm_array[3] = rpm_array[4];
rpm_array[4] = 60*(1000000/(timer*7)); //7 is the blade of the fan
//Last 5 Average RPM Counts Eqauls....
rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
}
readrpm = digitalRead(rpmpin);
if (readrpm == HIGH)
return;
}
}
//Capture The IR Break-Beam Interrupt
void fan_interrupt()
{
timer = (micros() - time_last);
time_last = micros();
}
can anyone help me ?
thanks