Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Encoder Manager
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2024-06-22 18:19:22
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Develop an Arduino project to interface with two */
- /* KY-040 rotary encoders. Use SimpleEncoder and */
- /* EncButton libraries to manage encoder and button */
- /* inputs. Configure pins D2-D7 and display encoder */
- /* positions and button states on Serial Monitor. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EncButton.h> // https://github.com/GyverLibs/EncButton
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void myTurn1();
- void myRight1();
- void myLeft1();
- void myClick1();
- void myHolded1();
- void myPress1();
- void myRelease1();
- void myTurn2();
- void myRight2();
- void myLeft2();
- void myClick2();
- void myHolded2();
- void myPress2();
- void myRelease2();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t _1enc_KY040_CLK_PIN_D2 = 2;
- const uint8_t _1enc_KY040_DT_PIN_D3 = 3;
- const uint8_t _1enc_KY040_SW_PIN_D4 = 4;
- const uint8_t _2enc_KY040_CLK_PIN_D5 = 5;
- const uint8_t _2enc_KY040_DT_PIN_D6 = 6;
- const uint8_t _2enc_KY040_SW_PIN_D7 = 7;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EncButton<EB_CALLBACK, _1enc_KY040_CLK_PIN_D2, _1enc_KY040_DT_PIN_D3, _1enc_KY040_SW_PIN_D4> enc1;
- EncButton<EB_CALLBACK, _2enc_KY040_CLK_PIN_D5, _2enc_KY040_DT_PIN_D6, _2enc_KY040_SW_PIN_D7> enc2;
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(9600);
- // Configure pins for the first encoder
- pinMode(_1enc_KY040_CLK_PIN_D2, INPUT);
- pinMode(_1enc_KY040_DT_PIN_D3, INPUT);
- pinMode(_1enc_KY040_SW_PIN_D4, INPUT_PULLUP);
- // Configure pins for the second encoder
- pinMode(_2enc_KY040_CLK_PIN_D5, INPUT);
- pinMode(_2enc_KY040_DT_PIN_D6, INPUT);
- pinMode(_2enc_KY040_SW_PIN_D7, INPUT_PULLUP);
- // Attach handlers for various events for the first encoder
- enc1.attach(TURN_HANDLER, myTurn1);
- enc1.attach(RIGHT_HANDLER, myRight1);
- enc1.attach(LEFT_HANDLER, myLeft1);
- enc1.attach(CLICK_HANDLER, myClick1);
- enc1.attach(HOLDED_HANDLER, myHolded1);
- enc1.attach(PRESS_HANDLER, myPress1);
- enc1.attach(RELEASE_HANDLER, myRelease1);
- // Attach handlers for various events for the second encoder
- enc2.attach(TURN_HANDLER, myTurn2);
- enc2.attach(RIGHT_HANDLER, myRight2);
- enc2.attach(LEFT_HANDLER, myLeft2);
- enc2.attach(CLICK_HANDLER, myClick2);
- enc2.attach(HOLDED_HANDLER, myHolded2);
- enc2.attach(PRESS_HANDLER, myPress2);
- enc2.attach(RELEASE_HANDLER, myRelease2);
- }
- // Event handlers for the first encoder
- void myTurn1() {
- Serial.print("Encoder 1 TURN_HANDLER: ");
- Serial.println(enc1.counter); // Print the counter value
- }
- void myRight1() {
- Serial.println("Encoder 1 RIGHT_HANDLER");
- }
- void myLeft1() {
- Serial.println("Encoder 1 LEFT_HANDLER");
- }
- void myClick1() {
- Serial.println("Encoder 1 CLICK_HANDLER");
- }
- void myHolded1() {
- Serial.println("Encoder 1 HOLDED_HANDLER");
- }
- void myPress1() {
- Serial.println("Encoder 1 PRESS_HANDLER");
- }
- void myRelease1() {
- Serial.println("Encoder 1 RELEASE_HANDLER");
- }
- // Event handlers for the second encoder
- void myTurn2() {
- Serial.print("Encoder 2 TURN_HANDLER: ");
- Serial.println(enc2.counter); // Print the counter value
- }
- void myRight2() {
- Serial.println("Encoder 2 RIGHT_HANDLER");
- }
- void myLeft2() {
- Serial.println("Encoder 2 LEFT_HANDLER");
- }
- void myClick2() {
- Serial.println("Encoder 2 CLICK_HANDLER");
- }
- void myHolded2() {
- Serial.println("Encoder 2 HOLDED_HANDLER");
- }
- void myPress2() {
- Serial.println("Encoder 2 PRESS_HANDLER");
- }
- void myRelease2() {
- Serial.println("Encoder 2 RELEASE_HANDLER");
- }
- void loop(void)
- {
- // Process the encoder and button actions for both encoders
- enc1.tick();
- enc2.tick();
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement