/************************************************************************
*
* Test of the Pmod
*
*************************************************************************
* Description: Pmod_ENC
* The direction of rotation of the encoder axis is displayed in the serial monitor.
* Pressing the push button memorizes the state of a counter in a variable
*
*
* Material
* 1. Arduino Uno
* 2. Pmod ENC
*
************************************************************************/
// Affectation of pins
#define A 2 // output A of the encoder
#define B 3 // output B of the encoder
#define BTN 4 // button of the encoder
volatile boolean rotation;
volatile boolean sens;
int compteur = 0;
int validation = 0;
// Programme d'interruption
void Interruption ()
{
if (digitalRead(A)==HIGH)
{
sens = digitalRead(B); // Clockwise
}
else
{
sens = !digitalRead(B); // anticlockwise
}
rotation = HIGH;
}
void setup()
{
attachInterrupt (0,Interruption,FALLING); // interruption on falling edge of output A
Serial.begin(9600); // initialization of the serial monitor
pinMode(A,INPUT); // configuration of pin 2 in input
pinMode(B,INPUT); // configuration of pin 3 in input
pinMode(BTN,INPUT); // configuration of pin 4 in input
}
void loop()
{
if (rotation==HIGH) // If detection of rotation
{
if (sens==HIGH) // if clockwise
{
compteur++; // increase counter
}
else
{
compteur--; // decrease counter
}
rotation = LOW;
Serial.print ("Compteur="); // write counter variable
Serial.println (compteur);
}
if (digitalRead(BTN)==HIGH) // if the button have been activate
{
validation=compteur;
delay(200); // debounce
Serial.print ("Validation="); // write validation variable
Serial.println (validation);
}
}
Comments
Please log in or sign up to comment.