Tuesday 27 March 2018

Rotary Encoder & switch/button press - arduino

I was looking for some code that utilised the encoder's switch.
The switch goes LOW when its pressed.
 
This is a very simple test of your encoder.
We Need to use the serial monitor to test.

It has 5 pins:
1. CLK - Clock (Out B)
2. DT - data (Out A)
3. SW - switch (when we press the shaft, the switch connects to GND. Otherwise it is floating.)
4. + : Vcc
5. GND
 

You most often will find encoders without the breakout board.
 
They still have 5 pins.
Here there are two GNDs
There is no Vcc
 
 
 
 
 
 
Make sure you connect both ground pins.
Connect the CLK (out B) to digital pin#2
DT (out A)  to #3 
SW (switch) to digital pin 4
 


 
 This is the code.
// *****************************
// Rotary Encoder Inputs
// the declaration of the Arduino pins to which the encoder’s CLK, DT 
// and SW pins are connected. 
#define CLK 2
#define DT 3
#define SW 4

int counter = 0; //  represents the count that will be modified each time that the knob is rotated one detent (click).
int currentStateCLK; // hold the state of the CLK output and are used for determining the amount of rotation.
int lastStateCLK;
String currentDir =""; //used when printing the current direction of rotation on the serial monitor.
unsigned long lastButtonPress = 0;//  used to debounce a switch

void setup() {
	
	// Set encoder pins as inputs
	pinMode(CLK,INPUT);
	pinMode(DT,INPUT);
	pinMode(SW, INPUT_PULLUP);

	// Setup Serial Monitor
	Serial.begin(9600);

	// Read the initial state of CLK and store it in the lastStateCLK variable
	lastStateCLK = digitalRead(CLK);
}

void loop() {
	
	// Read the current state of CLK
	currentStateCLK = digitalRead(CLK);

	// If last and current state of CLK are different, then pulse occurred
	// React to only 1 state change to avoid double count
	if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

		// If the DT state is different than the CLK state then
		// the encoder is rotating CCW so decrement
		if (digitalRead(DT) != currentStateCLK) {
			counter --;
			currentDir ="CCW";
		} else {
			// Encoder is rotating CW so increment
			counter ++;
			currentDir ="CW";
		}

		Serial.print("Direction: ");
		Serial.print(currentDir);
		Serial.print(" | Counter: ");
		Serial.println(counter);
	}

	// Remember last CLK state
	lastStateCLK = currentStateCLK;

	// Read the button state
	int btnState = digitalRead(SW);

	//If we detect LOW signal, button is pressed
	if (btnState == LOW) {
		//if 50ms have passed since last LOW pulse, it means that the
		//button has been pressed, released and pressed again
		if (millis() - lastButtonPress > 50) {
			Serial.println("Button pressed!");
		}

		// Remember last button press event
		lastButtonPress = millis();
	}

	// Put in a slight delay to help debounce the reading
	delay(1);
}
//**************************************************


 
 Links
 Last minute engineers provided the code in this post. Many thanks.
 
+ https://www.instructables.com/Improved-Arduino-Rotary-Encoder-Reading/ 
+ https://playground.arduino.cc/Main/RotaryEncoders/
 
 ---------------------------------
-------------------------------------

No comments:

Post a Comment