Remove Encoder and Replace with Switch, I'm Lost, Help

Let me start off by saying that I am a beginner. I had lots of help with this code but he isn't able to help me at the moment.

I have been working on a smoker controller for a couple years. Now that I have the smoker design complete I am back to the controller. I have been having issue with the rotary encoder restarting the arduino or just putting random characters all over the screen when being rotated. I have tried software and mechanical debouncing and nothing seems to work so I am wanting to switch to a manual set of buttons to raise and lower the temperature.

I have tried different approaches and I keep getting errors. I am hoping someone can assist me in getting this to work. Any help is much appreciated.

Attached is the last working code using the rotary encoder. It is over 9000 characters so I cant just add to post.

Working_5_22_16.ino (8.94 KB)

void set() {
  i=i+1;
  if((digitalRead(read1)==1) && (i%2!=1)) {
    int newPos = myEnc.read();
    //Serial.print(newPos);
    lcd.setCursor (12,1);
    lcd.print("SET");
    delay(10);
    }

delay() does NOT work in an ISR. An ISR is supposed to be fast. Writing to the LCD is NOT fast, and delay() is just not possible.

You really should understand what you are doing with interrupts, before you try to replace interrupt driven code.

If you take ALL the Serial and LCD stuff and ALL the delay()s out of the set() function what happens?

Then post your revised code if that does not work

If you are just using an encoder turned by a person as an input device I can see no reason why it should not work with the correct code.

Post a link to the datasheet for the encoder.

...R

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is/was the encoder shaft connected to?
What is/was it supposed to do in your sketch?

Thanks.. Tom.. :slight_smile:

PaulS:
delay() does NOT work in an ISR. An ISR is supposed to be fast. Writing to the LCD is NOT fast, and delay() is just not possible.

You really should understand what you are doing with interrupts, before you try to replace interrupt driven code.

I will read more about delays and interrupts. Just as a beginner its a tough section for me.

Robin2:
If you take ALL the Serial and LCD stuff and ALL the delay()s out of the set() function what happens?

Then post your revised code if that does not work

If you are just using an encoder turned by a person as an input device I can see no reason why it should not work with the correct code.

Post a link to the datasheet for the encoder.

...R

The Encoder is the KY-040. Can find many examples just not a true data sheet.

TomGeorge:
Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is/was the encoder shaft connected to?
What is/was it supposed to do in your sketch?

Thanks.. Tom.. :slight_smile:

I will do that tonight with fritzing.

Listen2this1:
The Encoder is the KY-040. Can find many examples just not a true data sheet.

Well post the link that you think will educate me about the device with the least effort on my part.

...R

Robin,

This is one site I used.

Robin2:
If you take ALL the Serial and LCD stuff and ALL the delay()s out of the set() function what happens?

Then post your revised code if that does not work

If you are just using an encoder turned by a person as an input device I can see no reason why it should not work with the correct code.

Post a link to the datasheet for the encoder.

...R

I removed Serial and the 2 delays. Seems to be more responsive. But after a few interrupts it fails. I have attached a video. I also attached a diagram.

Remove_Comments_and_Delay.ino (8.03 KB)

Hi,

If you read the encoder link you posted, you will find that they use pullup resistors on the two encoder outputs.
You don't appear to have any fitted in your firtzy, or a pullup statement in your code.
I don't know if the library turns pullups on.
Without pullups the inputs are floating when the encoder output switches are open, so the input is neither 0 or 5V.

The other thing is the capacitor and resistor in your encoder press switch.
I would assume its debounce, but your arrangement does not make sense, with the capacitor in series with the switch and arduino input.

Have you tried the suggested code that the site provides to see if you are reading the encoder correctly?

Tom.... :slight_smile:

I agree that trying the example code should be the first step. It looks very simple.

...R

Update:

After a long night I have made good process. After removing the delays, serial communication, and the latest debounce circuit WE (with you help) have it almost working. The error is now happening only when I press the button, either to enter or exit interrupt. This causes me to think I was chasing 2 different things.

The new debounce:

As for the pull up resistors, the switch has them on the back.
The De-Bounce sequence was from a tutorial I watched.

You did not respond to the advice in Replies #8 and #9. It makes it much easier to help if you do.

If you have a new version of your program you need to post it.

I thought the problem was reading an encoder rather than debouncing a switch?

...R

Robin2:
You did not respond to the advice in Replies #8 and #9. It makes it much easier to help if you do.

I haven't tried the code suggested in my link, yet. As for the pullup resistors the KY040 had 10k integrated in the circuit.

Robin2:
If you have a new version of your program you need to post it.

I will when I change the encoder section.

Robin2:
I thought the problem was reading an encoder rather than debouncing a switch?

Your suggestion to remove all the serial and delays helped the rotation errors. Once I was able to get past that the rotation errors I found the momentary switch issue. The switch issue was being masked by the encoder issue. I did some late night reading and decided to try a debouncing circuit. I will post my new attempt in the next day or two. I have sons t-ball tonight so could be tomorrow. This family stuff is getting in the way of my arduino stuff. :slight_smile: