SSD1306, showing servo position with pot control.

good afternoon,

this is my first project and i would like to ask a question regarding my code.

i have a wemos d1 and the ssd1306 display.

i am trying to display the position of the servo on the display.

at the moment i can control the servo with a pot and i have something on the display.

but the display only shows the number 203 and the rectangle bar is full and they wont change if i move the servo arm.

i have the pot conected to 5v, A0 and gnd
the oled conected to 5v, gnd, D1scl and D2sda.

// include the librerys needed
#include <Servo.h>
#include <stdio.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int potPin = 2; int val = 0;
#define OLED_RESET -1
#define OLED_SCREEN_I2C_ADDRESS 0x3C
Adafruit_SSD1306 display(OLED_RESET);

Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin

void setup()

{
display.begin(SSD1306_SWITCHCAPVCC, OLED_SCREEN_I2C_ADDRESS);
myservo.attach(D5); // attaches servo to pin D5
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);

}

void loop()

{

val1 = analogRead(potpin); // reads the value of the potentiometer
val1 = map(val1, 0, 1023, 0, 180); // sets the value between 0 and 180 degree
myservo.write(val1); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there before printing to the oled
display.setCursor(0,0);
display.setTextSize(1);
display.println(" Angle");
display.drawFastHLine(0, 10, 128, 1);
display.setCursor(46,17);
display.setTextSize(2);
val = analogRead(potPin);
display.println(val/5-1);
display.drawRect(0, 38, 128, 24, 1);
display.fillRect(2, 40, val/7-1, 20, 1);
display.display();
display.clearDisplay();

}

Read the pot once once per loop, use Serial for debug.

Please remember to use code tags when posting code

You say that your pot is attached to A0 but you've set potpin to 2, so you're actually reading A2.

Why potpin and potPin and val and val1?

Steve

slipstick:
Why potpin and potPin and val and val1?

Steve

Oops. That invalidates my criticism :confused:

…and mine.