MTobjets: Example 1
Event management for Uno, Nano, Mega

Rotation monitoring

Problem posed

An axis must run continuously. If it stops, the Nano must send a signal indicating the judgment.

 

Solution

A sensor (NPN, mechanical, hall effect) is on the axis and delivers inpulses in each turn. If there is no more inpulse for 3s, the axis no longer turns. A Nano will then activate an output pin.

 

Realization

An MTbutton object uses the input sensor. Each level change indicates that the axis turns (you cannot be interested in the level because a stop of the axis can occur as well when the sensor is high or low. We can only use the front information, here I opt for the two levels fronts.

If after 3s we have no information, we will activate an LED, for example BUILTIN_LED. It is therefore necessary to use a monostable that we will reset with each change of level of the sensor. As long as we have inpulses, the reset of the monostable will be that we will not arrive at the 3s.

 

Complete program recommended

This program is completely under interruption, and releases loop which can be used to do something else.

// An axis must run permanently. If it stops, the nano must send a
// signal indicating the stop.

#include <MTobjects.h> // V1.0.6 See http://arduino.dansetrad.fr/en/MTobjects

const byte CAPTEUR = 2;


void lights(void) // Called in the event of a stop of more than 3s
{
  digitalWrite(LED_BUILTIN, HIGH);
}
MTclock Timer(3000 milli_seconds, lights);


void lightOff(void) // Appelée pour chaque front du capteur
{
  pinMode(LED_BUILTIN, OUTPUT); // Can be put in the setup
  Timer.start(); // Reset the timer
  digitalWrite(LED_BUILTIN, LOW);
}
MTbutton Capteur(CAPTEUR, lightOff, lightOff);


void setup(){}
void loop(){}

setup and loop being empty, it is possible to use this program without any modification with any other code, for example by deleting the last two lines of this code and putting the rest before the other code.