Hey everyone.
I'm seriously about to go mad. I'm trying to use a hall effect sensor to measure the RPM of a fan, in specific time intervals (as 1, 2, 3 and 4 seconds), and control them via a Python interface (which I haven't even started yet, for I can get no output) All the functions work on their own. I mean, when I move say, "void scan" onto its own sketch and just run it without a serial command, it works fine. But when they're all together and being controlled by "SerialCommand", I get no goddamn "Serial.print" or "Serial.println"'s. When I give the command for "timer" functions, they work, the fan starts turning, but I get no serial output. When I use the scan function, the sketch just goes up to "Serial.println("I'm here") and dies afterwards. What could be the problem? I'm thinking the problem is with the "if (millis()" part. Am I missing something massive?
#include <SerialCommand.h>
#define pwmA 9
#define pwmB 10
SerialCommand sCmd;
const byte interruptPin = 2;
float rpm = 0;
unsigned long lastmillis = 0;
volatile int count = 0;
bool has_written = false;
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(interruptPin), flag, CHANGE);
sCmd.addCommand("timerone", timerone);
sCmd.addCommand("timertwo", timertwo);
sCmd.addCommand("timerthree", timerthree);
sCmd.addCommand("timerfour", timerfour);
sCmd.addCommand("scan", scan);
Serial.println("Ready");
}
void loop() {
sCmd.readSerial(); // We don't do much, just process serial commands
}
void timerone() {
analogWrite(pwmA, 255);
analogWrite(pwmB, 0);
if (millis() - lastmillis == 1000) {
rpm = count * 60;
Serial.println(rpm);
count = 0;
lastmillis = millis();
}
}
void timertwo() {
analogWrite(pwmA, 255);
analogWrite(pwmB, 0);
if (millis() - lastmillis == 2000) {
rpm = count * 30;
Serial.println(rpm);
count = 0;
lastmillis = millis();
}
}
void timerthree() {
analogWrite(pwmA, 255);
analogWrite(pwmB, 0);
if (millis() - lastmillis == 3000) {
rpm = count * 20;
Serial.println(rpm);
count = 0;
lastmillis = millis();
}
}
void timerfour() {
analogWrite(pwmA, 255);
analogWrite(pwmB, 0);
if (millis() - lastmillis == 4000) {
rpm = count * 15;
Serial.println(rpm);
count = 0;
lastmillis = millis();
}
}
void scan() {
Serial.println("initializing RPM count");
int i = 0;
while (i < 256)
{
if (!has_written) {
analogWrite(pwmA, i);
has_written = true;
Serial.println("I'm here");
}
if (millis() - lastmillis == 1000) {
Serial.println("I'm here 2");
//detachInterrupt(digitalPinToInterrupt(interruptPin));
rpm = count * (60);
Serial.print(rpm);
Serial.print(" ");
Serial.println(i);
count = 0;
lastmillis = millis();
i++;
has_written = false;
attachInterrupt(digitalPinToInterrupt(interruptPin), flag, CHANGE);
}
}
}
void flag() {
count++;
}