KY40 and while loop

My sketch is a menu driven Unit Conversion program using a KY40 Function that is called many times.

The KY40 function needs to loop continuously until the desired count is reached by rotating the KY40;s shaft and then exit to the calling function when the KY40's button-switch is pressed.

I'm trying to use 'while' to control the looping, but can't seem to get it to work.

HELP please.

Oh, please ignore a prior post (if posted). It was incomplete.

Below is the KY40 function code.

void KY40() {//get the KY40 counter. Needs While loop here using BUTTON PRESS to escape, but it does'nt work !!)
Serial.println("This is function KY40");
int btnState = digitalRead(SW);

  // Read the initial state of CLK
  lastStateCLK = digitalRead(CLK);
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);
Serial.println("line 112 reached");
  // 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(" | Counter: ");

  // Remember last CLK state
  lastStateCLK = currentStateCLK;

  // Read the button state
  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
  return counter;
lastStateCLK = digitalRead(CLK);
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

You are making these two values the same here and they will never differ as the readings are microseconds apart.

//lastStateCLK = digitalRead(CLK);
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

At the end of the routine you have this code and there is no need to do it again at the start.

lastStateCLK = currentStateCLK;

You want btnState == high to get into the while() block but then you check for btnState == low within.


Do you mean this ??

int KY40Function(){ //begin KY40 function
int btnState = digitalRead(SW);
btnState = digitalRead(SW);
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);

btnState==HIGH;Why would you do a comparison, and throw away the result?

Please remember to use code tags when posting code

I'll reword.

'while' btnState is HIGH execution passes into the block under the while. Within that block you're testing to see if btnState is (==) LOW. How can it be LOW if it took a HIGH to get there?

Possibly what you want is:

if(btnState == HIGH){
// blah blah blah

// yada yada yada

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.