How to Connect KX-040 to Arduino Uno - Different Way!

by InventionsLab in Circuits > Arduino

413 Views, 1 Favorites, 0 Comments

How to Connect KX-040 to Arduino Uno - Different Way!

How to connect KX-040 to arduino uno , dont work..JPG
How To Connect KX-040 To Arduino Uno - Different Way!

How To Connect KX-040 To Arduino Uno - Different Way!

Today we will look at how to connect the KX-040 to the Arduino Uno. I'll walk you through it, if another code doesn't work for you and you're still looking for a program that will work for you, this might be the solution! I spent some time on this and finally succeeded and created a working program for this button where there was a lot of interference going on. If you have any questions or additions to your solution, don't forget to write in a comment.





#include <LiquidCrystal_I2C.h>


//button commands only for button and potentiometer

int pin3= 5;  //Pin SW

const int pin1 = 2;

const int pin2 = 3;

volatile int statePin1 = 0; // Pin 1 status

volatile int statePin2 = 0; // Pin 2 status

volatile int laststatePin1 = 0;

volatile int laststatePin2 = 0;

volatile unsigned long timePin1 = 0; // Time for pin 1

volatile unsigned long timePin2 = 0; // Time for pin 2

volatile unsigned long lasttimePin1 = 0; // last Time for pin 1

volatile unsigned long lasttimePin2 = 0; // last Time for pin 2

int countererror2;

int countererror1;

int counter = 0;


//button

unsigned long lastButtonPress = 0;

bool buttonState = HIGH;

bool buttoncheck;



LiquidCrystal_I2C lcd(0x27, 16, 2);


void setup() {

  Serial.begin(9600);

  lcd.init();

  lcd.backlight();

  lcd.setCursor(0, 0);

  lcd.display();


  // BUTTON set pin

  pinMode(pin3, INPUT_PULLUP);

  pinMode(pin1, INPUT);

  pinMode(pin2, INPUT);

  attachInterrupt(digitalPinToInterrupt(pin1), handlePin1Change, CHANGE);

  attachInterrupt(digitalPinToInterrupt(pin2), handlePin2Change, CHANGE);

}




void handlePin1Change() {

   

  statePin1 = digitalRead(pin1);

  timePin1 = millis(); // Record the change time on pin 1

 

  //filtering annoying duplicates, both temporal and identical consecutive values, omitting zeros, lets only 1

   if (timePin1 != lasttimePin1 && statePin1 != laststatePin1 ) {

  if ( statePin1 == 1) {

   countererror1++; }


  compareStates();

 

 

 lasttimePin1=timePin1;

    laststatePin1=statePin1;

   

  }

   

 

}


void handlePin2Change() {

 

  statePin2 = digitalRead(pin2);

  timePin2 = millis(); // Record the change time on pin 2

 

 //filtering annoying duplicates, both temporal and identical consecutive values, omitting zeros, lets only 1

  if (timePin2 != lasttimePin2 && statePin2 != laststatePin2 ) {

   

   if ( statePin2 == 1) {  

   countererror2++; }


  compareStates();


   

 lasttimePin2=timePin2;

      laststatePin2=statePin2;

  }


 

}




void compareStates() {


 //checking the last pin, which is always 1 + checking if the pins have changed at least 1x to check the wrong direction

 if (statePin2 == 1 && statePin1 == 1  && countererror1 > 0 && countererror2 > 0 ) {


  //finding which direction it is facing

  if (timePin2 > timePin1 ) {


 

counter--;

countererror1= 0;

countererror2= 0;


}else

  {

     counter++;

   


countererror2=0;

countererror1=0;

}


Serial.println(counter);


}

 

}



void loop() {


  lcd.setCursor(0,0);

  lcd.print("Counter:");

  lcd.print(counter);

 lcd.print("     ");

 


  //check button pressed

   buttoncheck = digitalRead(pin3);


     if ((millis() - lastButtonPress) > 50) {

    if (buttoncheck != buttonState) {

      buttonState = buttoncheck;


      if (buttonState == LOW) {

        Serial.println("Button pressed!");

        lcd.setCursor(0,1);

        lcd.print("Button pressed!");

        delay(300);

        lcd.setCursor(0,1);

        lcd.print("                ");


       lastButtonPress = millis();

      }

    }

  }

  delay(1);


       

   


 

}