Go Down

Topic: Making a measuring wheel for my project. (Read 1 time) previous topic - next topic

angelmoraga


Project is making a measuring wheel for my build. Im using an arduino mega, Incremental rotary encoder with a 2" wheel and would like the TFT display to read in Standard and Metric measurements, also to read forward and backwards

PaulMurrayCbr

You may also want a 'zero' button. The project framework looks a bit like this:

Code: [Select]


long ticks = 0;

double TICKS_TO_FEET_CONVERSION = 2.0 * PI / ENCODER_TICKS / 12.0;
double TICKS_TO_METERS_CONVERSION = TICKS_TO_FEET_CONVERSION * 12.0 * 2.54 / 100.0;

const byte zeroPin, encoderAPin, encoderBPin;
byte zero = HIGH, encoderA = HIGH, encoderB = HIGH;

void setup() {
  // setup the pins

  // draw display picture - text that doesn't change

  // for now, we will just output to serial

  Serial.begin(9600);
  while(!Serial);
  Serial.println("READY!");
}

void loop() {
  byte zeroPrev = zero; zero = digitalRead(zeroPin);
  byte encoderAPrev = encoderA; encoderA = digitalRead(encoderAPin);
  byte encoderBPrev = encoderB; encoderB = digitalRead(encoderBPin);

  if(zeroPrev == HIGH && zero == LOW) {
    ticks = 0;
    updateDisplay();
  }

  if(encoderAPrev != encoderA) {
    if(encoderA == encoderB) ticks ++; else ticks--;
    updateDisplay();
  }

  if(encoderBPrev != encoderB) {
    if(encoderA == encoderB) ticks --; else ticks++;
    updateDisplay();
  }
}


void updateDisplay() {
  // convert ticks to distance.

  float feet = ticks * TICKS_TO_FEET_CONVERSION;
  float meters = ticks * TICKS_TO_METERS_CONVERSION;

  // display the result on the TFT. This usually involves
  // clearing a rectangle and drawing the number in the rectangle

  // for now, we will just output to serial

  Serial.print("ticks: ");
  Serial.print(ticks);
  Serial.print(", feet: ");
  Serial.print(feet);
  Serial.print(", meters: ");
  Serial.print(meters);
  Serial.println();
}



http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up