Hey Guys, I’m trying to calculate a position on the LCD screen by getting input from a pot, which in turn will determine the position of a cursor on screen. In effect being able to move a character from left to right, by turning the pot.
Now, I am able to read in the value of the pot, and display it on the LCD, and I have the function in place to calculate the cursor position on stage… but the position tracker wont update.
However, when the pot value is at 1023, making the position 16, it will display correctly.
Code is as follows. I cannot see any reason in the code why it would be doing this… Maybe it’s something else that I’m missing. I wouldn’t usually ask about something so trivial… But I can’t for the life of me figure out why it’s behaving this way. Especially when it works for that 1 pot value.
Could it have something to do with me not limiting the way that the result is displayed? Because 16 is an easier number to display than 3.912873471927346789125973488176238478… But then again, I would expect a display overflow with a bunch of random numbers if that was the case.
#include <LiquidCrystal.h>
#include <math.h>
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonState = 1;
int counter = 0;
int potVal;
double cursorPos;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
potVal = analogRead(0); //read value of the pot
lcd.setCursor(0,0);
lcd.print(cursorPos);
lcd.setCursor(0,1);
lcd.print(potVal);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
// if (buttonState == LOW) {
// turn LED on:
// }
// else if(buttonState == HIGH)
// {
// }
delay(1);
cursorPos = calcCursor(potVal);
}
double calcCursor(int potVal)
{
double result;
result = ((potVal / 1023) * 16);
return result;
}