Sketch doing opposite of what I am expecting. Using an encoder and push button.

Hello,

I’m new to Arduino’s. I am trying to write a Sketch by modifying an encoder Sketch I found. What I am wanting it to do is for the Led to light up. Then when the “UpperLimitSet” Button is pushed it reads the current encoder position. The Led should be on if the encoder is lesser than this number or the led should turn off if equal to or above this number. When I start this sketch the led turns on as I would expect. But when I push the button it then works the opposite of what I am expecting. The led turns on when above the encoder number and turns off when below.

I hope that is not to confusing. I am sure it is something simple that I am over looking. (maybe there is a completely better way of doing this too) I have not done any programming since 30 years ago with and Atari computer!

The purpose of this sketch (besides just playing and learning) will be to some day put programmable limits on an ironworker I own. Currently it has mechanical limit switches which work fine but it would be nice to be able to set limits simply but pushing a button.

Thanks for the help!

Jeff T.

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);    //   avoid using pins with LEDs attached
const int ledPin =  13;      // the number of the LED pin
const int UpperLimitSetPin = 4;   //LowerLimit Button


void setup() {
  
  pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
  pinMode(UpperLimitSetPin, INPUT);
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;
int UpperLimitSet  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
  oldPosition = newPosition;
  Serial.println(newPosition);}
  if (digitalRead(UpperLimitSetPin) == HIGH) { // Push Button to set upper limit 
  UpperLimitSet = newPosition;
  Serial.println("Upper Limit Set:");}
  if (UpperLimitSet >= newPosition) digitalWrite(ledPin, LOW);  // this is the part that works opposite than should
  if (UpperLimitSet < newPosition) digitalWrite(ledPin, HIGH);  // this is the part that works opposite than should
}

Putting each { on a new line, and every } on a new line, and using Tools + Auto Format, would make your code more readable.

Depending on how the LED is wired, setting a pin HIGH could turn it on, or off. It sounds like your is wired so that setting the pin HIGH turns the LED off. Some clue as to how the LED IS wired would be useful.

Hi Paul

Thank you for your reply. Also thank you for the info on using Auto Format.

The LED I am using is the one on pin 13 of the Arduino UNO board. I do not have a seperate LED wired I’m just using the board LED for now.

I think I just figured it out… I modified this part of the sketch. I changed the “if (UpperLimitSet >= newPosition)” to “if (newPosition >= UpperLimitSet)”.

  if (digitalRead(UpperLimitSetPin) == HIGH) { // Push Button to set upper limit
    UpperLimitSet = newPosition;
    Serial.println("Upper Limit Set:");
  }
  if (newPosition >= UpperLimitSet) digitalWrite(ledPin, LOW);
  if (newPosition < UpperLimitSet) digitalWrite(ledPin, HIGH);
}

I changed the “if (UpperLimitSet >= newPosition)” to “if (newPosition >= UpperLimitSet)”.

You could have kept the code readable, and simply changed the >= to <. The second test is not necessary. If the one value is not less than or equal to the other value, it must be greater than.