Coding Sliding potentiometer to show values going down no just up

hey there, im a student and this Is my first Time coding, I am trying to code my potentiometer to show values on the lcd screen as it is moving. my teacher has written me a code but it stops showing values once it hit its max. so I am trying to get any help on what code to use.

this is my code

#include <Wire.h>
#include "Waveshare_LCD1602_RGB.h"'

'Waveshare_LCD1602_RGB lcd(16,2); //16 characters and 2 lines of show'
'
'int r,g,b,t=0;'
'
'int max = 0;'
'const byte inPin = A0;'
'char sensorvalue [33];'

'void setup()'

'{'
' // put your setup code here, to run once:'

' Serial.begin(9600); //Set serial baud rate to 9600 bps'
' pinMode(inPin, INPUT); //Set pin mode'

' // initialize LCD screen'
' lcd.init();'
''
'lcd.setCursor(0,0);'
'lcd.send_string("Waveshare");'
'}'

'void loop()'
'{'
' // put your main code here, to run repeatedly:'
' int temp = analogRead(inPin);'
'if (temp > max)'
'{'
' max = temp;'
'Serial.print("max set to : ");'
'Serial.println(max);}'
''
'r = (abs(sin(3.14*t/180)))255;'
'g = (abs(sin(3.14
(t + 60)/180)))255;'
'b = (abs(sin(3.14
(t + 120)/180)))*255;'
't = t + 3;'
'lcd.setRGB(r,g,b);'
'delay(150);'

'itoa (max,sensorvalue,10);'
'lcd.setCursor(0,0);'
'lcd.send_string("Waveshare");'
'lcd.setCursor(0,1);'
'lcd.send_string(sensorvalue);'

Your copy/paste skills could use a refresher... for now, remove the ticks (') and be sure to copy the whole sketch (you are missing the end... is there one brace missing in this chunk, or a bunch of code un-copied?)

Here is what I guessed it might look like:

#include <Wire.h>
#include "Waveshare_LCD1602_RGB.h"

Waveshare_LCD1602_RGB lcd(16, 2); //16 characters and 2 lines of show

int r, g, b, t = 0;

int max = 0;
const byte inPin = A0;
char sensorvalue [33];

void setup()

{
  // put your setup code here, to run once:

  Serial.begin(9600); //Set serial baud rate to 9600 bps
  pinMode(inPin, INPUT); //Set pin mode

  // initialize LCD screen
  lcd.init();

  lcd.setCursor(0, 0);
  lcd.send_string("Waveshare");
}

void loop()
{
  // put your main code here, to run repeatedly:
  int temp = analogRead(inPin);
  if (temp > max)
  {
    max = temp;
    Serial.print("max set to : ");
    Serial.println(max);
  }

  r = (abs(sin(3.14 * t / 180)))255;
  g = (abs(sin(3.14(t + 60) / 180)))255;
  b = (abs(sin(3.14(t + 120) / 180))) * 255;
  t = t + 3;
  lcd.setRGB(r, g, b);
  delay(150);
  itoa (max, sensorvalue, 10);
  lcd.setCursor(0, 0);
  lcd.send_string("Waveshare");
  lcd.setCursor(0, 1);
  lcd.send_string(sensorvalue);
}

I would check that your slider is wired correctly. It is sometimes difficult to get the pin out right.

Take a volt meter and measure the voltage between ground and the analogue input pin. Do the readings match your expectation?

This demo-cdoe does what is coded.

the variable named "max" gets updated each time variable named "temp" is bigger than max.
If you have adjusted the potentiometer to its max-value the variable named "max" stays" on this value because the if-condition

if (temp > max)

is no longer true

If this code is really from your teacher. Just drop this code.
For displaying the ADC-value of the potentiometer in the code-example posted above there are a lot of unnescessary things.

Can you please give an overview about your programming experience.
The code above would be appropriate if you have learned basics about

  • what is function setup for
  • what is funcion loop() for
  • variables
  • libraries
  • if-conditions
  • Serial

if this is not the case
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Here is a bare minimum sourcecode that does nothing more than showing the analog to digital converted value of the potentiometer as a WOKWI simulation

important hint:

The firefox-browser does not manage the slider-potentiometer correctly
use a different browser like chrome or opera to play with the WOKWI-simulation

here is the same code with a rotating potentiometer which works with firefox

best regards Stefan

thank you for this I will try it out tomorrow!

and also would this code work with my slider currently?

What do you mean with "currently" ?
A potentiometer is a potentiometer. If the resistance of your potentiometer is too low
it might be that the current flowing is too much for the voltage-regulator of the arduino.

What resistance does your potentiometer have?

best regards Stefan

I only meant currently as in because it’s a different brand or something that it needs a certain code but it should work.

No, all inputs on the analogue input work the same there is no special code you need.

In this case your problem is solved by @StefanL38 in that the software is doing exactly what it is expected to do. If you want it to do any different, then you need to do different things with the numbers you are getting from them, like reset the max variable to zero regularly so that it tracks your pot.

The more often you reset this variable the closer will be the tracking.

Or instead of resetting the variable to zero then subtract a certain value from it, then it will track your pot but hold the maximum value for the time between changing the variable.

hey there I tried this and it didn't work It also didn't plot of the serial monitor/plotter
when I tried

If you want more help than just a "oh I'm sorry that it did not work for you"
you have to provide much more and specific information:

  • what did you try?

    • clicking on the WOKWI-link with the slider-potentiometer?
      • with which browser?
    • clicking on the WOKWI-link with the rotation-potentiometer?
      • with which browser?
  • uploading a modifyied code to your real microcontroller ?

  • posting the complete modified sketch ?

  • and describing in detail what behaviour the newly uploaded code showed

After reading the short, very general posting, I asked myself: "Is this a dissatisfied and frustrated student who would most like to have working code posted to him?

This is not how this forum works. This forum helps to learn fishing. It does not serve ready to eat fish-meals.

best regards Stefan

Hi Stefan, I've tried it in Edge and Vivaldi.
Never seen a wokwi before so maybe I'm doing it wrong, but I cant get the pot value to change.
Clicking and dragging with the mouse just moves the pot image.

The sign on the play-button changes to this
grafik

If you click the button in the middle the simulation will stop
if you click the right button the simulation pauses
The symbols are pretty standard to most play-media devices like blueray-players or spotify-buttons

As long as the simulation runs you can't modify the wiring or the code.
For modify the wiring or the code you have to stop the simulation
best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.