Here is my program now:
In which you did not make cycleTime volatile...
Here is my program now:
In which you did not make cycleTime volatile...
Here is an update of my sketch. I included a library that allows the Arduino to go to sleep if the interrupt isn't triggered in 500ms (400ms + 100ms). It wakes up as soon as the interrupt is triggered again. I still need a way to tell the program to start reading at the beginning of the main loop() after the ISR terminates.
#include <Servo.h>
#include <Adafruit_MotorShield.h>
#include <Wire.h>
#include <Sleep_n0m1.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Servo servo1;
Sleep sleep;
volatile unsigned long interruptTime;
volatile unsigned long cycleTime;
float RPM;
byte pos = 180;
boolean sleepmode = true;
void setup() {
Serial.begin(4800);
servo1.attach(10);
pinMode(2, INPUT_PULLUP);
servo1.write(180);
attachInterrupt(0, measureCycleLength, RISING);
}
void loop() {
RPM = cycleTime / 1000;
RPM = 1 / RPM;
RPM = 120 * RPM;
if (RPM > 9000) {
servo1.write(0);
Serial.println("Overrev");
Serial.println(RPM);
}
else if (RPM < 750) {
servo1.write(180);
Serial.println("Underspeed");
Serial.println(RPM);
}
else {
pos = map(RPM, 750, 9000, 170, 0);
servo1.write(pos);
Serial.println(RPM);
}
delay(400); //Timeout between samples. If no sample is taken in 400ms, it assumes the engine stalled.
servo1.write(180);
Serial.println("Stall");
if (sleepmode = true) { //If the sleepmode variable is true, the Adruino is put to sleep, if not, it is locked in a while loop.
Serial.println("Sleeping");
delay(100);
sleep.sleepInterrupt(0, CHANGE); //Puts the Arduino to sleep until pin 2's state changes.
} else {
while (sleepmode = false) {
delay(1000);
}
}
}
void measureCycleLength() {
unsigned long now = millis();
if(interruptTime > 0)
{
cycleTime = now - interruptTime;
}
interruptTime = now;
Serial.println(cycleTime);
}
I need help. All of my friends agree.
Delta_G:
Serial code in an ISR is a recipe for a locked up board.
It doesn't lock up and it responds the same way without that line. I don't know why, but I know what you are trying to say.
electrodog321:
It doesn't lock up ...
It will, sooner than later.
I agree. I took it out anyway.
I have tried what I thought was everything I could do with the interrupts. (Which was probably next to nothing, like my IQ score). I found a library called FreqMeasure. You can read about it on this page. I can't seem to get the FreqMeasure.available() command figured out. I know it has to be my error. I have an unfinished version of my sketch that I have been playing around with to sort it out. Here it is:
#include <FreqMeasure.h>
float frequency;
boolean timeSample = false;
unsigned long then;
unsigned long now;
unsigned short timeOut;
byte samples;
void setup() {
Serial.begin(57600);
FreqMeasure.begin();
}
void loop() {
Serial.println(FreqMeasure.available());
samples = FreqMeasure.available();
if (samples != 0) {
Serial.println("err");
timeSample = false;
frequency = FreqMeasure.countToFrequency(FreqMeasure.read());
Serial.println(frequency);
} else {
if (timeSample = false) {
then = millis();
timeSample = true;
} else {
now = millis();
timeOut = now - then;
if (timeOut > 400) {
Serial.println("Stall");
}
}
}
Serial.println(timeOut);
}
I hope you can point out a simple error or two in this sketch. Sorry this topic is taking so long to resolve.