/*
Inspired by my very first Wokwi project
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#define width 128
#define height 64
Servo servo;
Adafruit_SSD1306 oled(width, height, &Wire, -1);
float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
servo.attach(9);
servo.write(0);
}
void loop() {
int analogValue = analogRead(A0);
float voltage = floatMap(analogValue, 0, 1023, 0, 5);
/*
Serial.print("Analog: ");
Serial.print(analogValue);
Serial.print(", Voltage: ");
Serial.println(voltage);
delay(1000);
*/
if (analogValue < 100) {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
oled.println("The Servo has spun to the right!");
oled.display();
for (int pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(10);
}
}
if (analogValue > 100) {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
oled.println("The Servo has spun to the left!");
oled.display();
for (int pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(10);
}
}
}