Hello guys,
I am new with Arduino and I need some help with this error.
I have a magnetic switch interrupt that is counting the rotations of an object. I need to send the RPMs value via I2C to the master Arduino but the loop function freezes even without the I2C code.
Heres the relevant code:
int firstMagneticSensor = 2; // choose the input pin
int secondMagneticSensor = 3; // choose the input pin
volatile int firstMagneticSensorValue = 0;
volatile int rpm = 0;
volatile unsigned long firstMagneticSensorTime = 0;
const long debouncingInterval = 1000;
void setup() {
pinMode(firstMagneticSensor, INPUT); // declare pushbutton as input
pinMode(secondMagneticSensor, INPUT); // declare pushbutton as input
Serial.begin(115200); // ONLY DEBUG
attachInterrupt(digitalPinToInterrupt(firstMagneticSensor), firstMagneticSensorActive, RISING);
attachInterrupt(digitalPinToInterrupt(secondMagneticSensor), secondMagneticSensorActive, RISING);
}
void loop(){
String rotation_str = String(rotationDirection, DEC);
String rpm_str = String(rpm, DEC);
String to_send = String("02;" + ";" + rpm_str);
char buffer[8];
to_send.toCharArray(buffer, 32);
Serial.print("Sent: ");
Serial.println(buffer);
}
void proccess_first_sensor() {
unsigned long currentMillis = millis();
// TODO Change to Hardware
if (currentMillis - firstMagneticSensorTime >= debouncingInterval) {
//Serial.println("Entrou 1");
/* Count RPM */
rpm = 60/((currentMillis - firstMagneticSensorTime)/1000);
firstMagneticSensorTime = currentMillis;
}
}
void firstMagneticSensorActive() {
firstMagneticSensorValue = digitalRead(firstMagneticSensor);
if(firstMagneticSensorValue == 1) {
proccess_first_sensor();
}
}
The loop function prints everything ok until the first interrupt, then it freezes and don't print anymore.
What am I doing wrong?
Thanks,
Artur