I am working on a project on my arduino uno in which I want to change the text on the LCD every time the potentiometer has been rotated. In order to do this, I have stored the angle values into an 'int', constantly check if the angle has been rotated enough to change the text, and if it has, print the next message. I am not sure if either my code or circuit is wrong as nothing happens at all when i rotate the potentiometer.
Here is my code so far and an image of the circuit (the innermost potentiometer is for the brightness, the one on the right is for changing the text on the LCD. I think I may have wired the second potentiometer incorrectly but IIam not sure how to fix this).
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // removed 3
// set up a constant for the button
const int buttonPin = 6;
// variable to hold the value of the button
int buttonState = 0;
// variable to hold previous value of the button
//int prevbuttonState = 0;
// a variable to choose which reply will be displayed
int reply = -1;
int prevReply = -1;
int potPin = A0;
int potVal = 0;
int angle = 0;
int prevAngle = 0;
//int startTime = 0;
//int timeElasped = 0;
//int workoutTime = 0;
void setup() {
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);
// set up the switch pin as an input
pinMode(buttonPin, INPUT);
Serial.begin(9600);
prevAngle = map(potVal,0,1023,0,300); // set prevAngle to original angle. later, code that if the angle isnt 0 tell user to reset it
// Print a message to the LCD.
lcd.print("Pick a");
// set the cursor to column 0, line 1
// line 1 is the second row, since counting begins with 0
lcd.setCursor(0, 1);
// print to the second line
lcd.print("Muscle Group!");
}
void loop() {
// check the status of the switch
buttonState = digitalRead(buttonPin);
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle=map(potVal,0,1023,0,300);//used to be 179, changed to 300 assuming it equals the max angle available in the potentiometer
Serial.print(", angle: ");
Serial.println(angle);
if (angle > prevAngle+69){ // if the pot has been rotated enough clockwise, display next group
if (reply!=3)
reply = reply+1;//provide next muscle group
else
reply = 0;//return to first muscle group
/*if (prevReply==-1)
prevReply=reply;*/
}
else if (angle < prevAngle-69){ // if the pot has been rotated enough counterclockwise, display prev group
if (reply!=0 && reply!=-1)
reply = reply-1;//provide previous muscle group
else if (reply==0)// check if reply is still equal to -1 btw
reply = 3;//return to last muscle group
else if (reply==-1){
reply = 0;
//prevReply=reply;
}
}
//prevReply = reply;
if (buttonState == HIGH) { //if the button has been pressed, display new random exercise within muscle group that was chosen
if (reply == 0)
reply = random(4,6);
if (reply == 1)
reply = random(7,9);
if (reply == 2)
reply = random(10,12);
if (reply == 3)
reply = random(13,15);
// clean up the screen before printing a new reply
lcd.clear();
// set the cursor to column 0, line 0
lcd.setCursor(0, 0);
// print some text
lcd.print("Time to do some");
// move the cursor to the second line
lcd.setCursor(0, 1);
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
/*if (workoutTime - millis() + startTime==0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You did it!! :]"); //if reply keeps looping, stop loop once timer=0. if reply never loops, figure out how to make it loop for the timer*/
}
//if (prevReply != reply) // if we have chosen a new workout, change the starting time
//startTime = millis(); // record millis() function to see how much time passes btwn start of program to start of timer
if (prevReply != reply){
switch (reply) { // choose a saying to print based on the value in reply
case 0:
lcd.print("Arms");
break;
case 1:
lcd.print("Core");
break;
case 2:
lcd.print("Chest");
break;
case 3:
lcd.print("Legs");
break;
case 4:
//workoutTime = 2400;
lcd.print("Arm wrkt 1; " /*+ workoutTime - millis() + startTime*/); // temporary workout names, to be replaced in a later draft
break;
case 5:
//workoutTime = 1200;
lcd.print("Arm wrkt 2; " /*+ workoutTime - millis() + startTime*/);
break;
case 6:
//workoutTime = 1500;
lcd.print("Arm wrkt 3; " /*+ workoutTime - millis() + startTime*/);
break;
case 7:
//workoutTime = 1800;
lcd.print("Core wrkt 1; " /*+ workoutTime - millis() + startTime*/);
break;
case 8:
//workoutTime = 900;
lcd.print("Core wrkt 2; " /*+ workoutTime - millis() + startTime*/);
break;
case 9:
//workoutTime = 2400;
lcd.print("Core wrkt 3; " /*+ workoutTime - millis() + startTime*/);
break;
case 10:
//workoutTime = 2100;
lcd.print("Chest wrkt1; " /* + workoutTime - millis() + startTime*/);
break;
case 11:
//workoutTime = 1200;
lcd.print("Chest wrkt2; " /* + workoutTime - millis() + startTime*/);
break;
case 12:
//workoutTime = 1200;
lcd.print("Chest wrkt3; " /* + workoutTime - millis() + startTime*/);
break;
case 13:
//workoutTime = 1500;
lcd.print("Leg wrkt 1; " /* + workoutTime - millis() + startTime*/);
break;
case 14:
//workoutTime = 2400;
lcd.print("Leg wrkt 2; " /* + workoutTime - millis() + startTime*/);
break;
case 15:
//workoutTime = 1800;
lcd.print("Leg wrkt 3; " /*+ workoutTime - millis() + startTime*/);
break;
prevReply=reply;
}
}
prevAngle=angle;
}
