Good day.
I have a project where I need to use an Arduino, a rotary encoder and a LCD display to measure a distance.
The rotary encoder is connected to a spool with string and it measures the distance.
I need help to code and connect the components.
so far I have:
1 x Arduino
1 x Rotary encoder with push button
1 x 16 x 2 LCD display with the 12 C backpack to reduce the pins from 16 to 4
I also need assistance with writing 2 sets of code that achieve the following
Set 1:
the Arduino will be collected to a laptop and will measure the distance and record the data at set time intervals until the measuring stops. the measurements need to be displayed on the LCD screen in real time or every one or two seconds. The data needs to be recorded and be exported to text or excel file.
Set 2:
the Arduino will not be connected to a computer and will be programed beforehand and connected to only a power source and needs to measure the distance in real time or at set time intervals until the distance is measured.
below is my code so far. it does not include a section for the LCD screen.
const int clkPin=2; //the clk attach to pin 2
const int dtPin=3; //the dt attach to pin 3
const int swPin=4; //the number of the button
int encoderVal = 0;
void setup()
{
//set clkPin,dePin,swPin ans INPUT
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
Serial.begin(9600); // initiate serial communications at 9600 bps
}
void loop()
{
int change = getEncoderTurn();
encoderVal = encoderVal + change;
if(digitalRead(swPin) == LOW)//if button pull down
{
encoderVal = 0;
}
double encPrintVal = encoderVal * 6.283;
Serial.print(encPrintVal, 3); //print the encoderVal on the serial monitor
Serial.print("\t");
Serial.print("mm");
Serial.print("\n");
}
int getEncoderTurn(void)
{
static int oldA = HIGH; // set the oldA as HIGH
static int oldB = HIGH; // set the oldB as HIGH
int result = 0;
int newA = digitalRead(dtPin); //read the value of clkPin to newA
int newB = digitalRead(clkPin); //read the value of dtPin to newB
if (newA != oldA || newB !=oldB) //if the value of clkPin or the dtPin haas changed
{
//something has changes
if (oldA == HIGH && newA == LOW)
{
result = (oldB*2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}