#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BUTTON_PIN 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Servo servo;
Servo servo1;
int xValue;
int yValue;
int mapMin = 0;
int mapMax = 210;
String speedMessage;
void setup() {
servo.attach(9);
servo1.attach(10);
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("Press the button to enjoy the expedition");
display.display();
}
void loop() {
xValue = analogRead(A0);
yValue = analogRead(A1);
xValue = constrain(map(xValue, 210, 290, mapMin, mapMax), mapMin, mapMax);
yValue = constrain(map(yValue, 210, 290, mapMin, mapMax), mapMin, mapMax);
int servo1angle = map(yValue, mapMin, mapMax, 0, 180);
servo1.write(servo1angle);
if (xValue <= 30) {
speedMessage = "HIGHEST SPEED";
servo.write(84);
} else if (xValue <= 60) {
speedMessage = "MEDIUM SPEED";
servo.write(86);
} else if (xValue <= 90) {
speedMessage = "LOWEST SPEED";
}else if(xValue <=120){
speedMessage = "LOWEST SPEED";
}else if(xValue <=150){
speedMessage = "MEDIUM SPEED";
}else{
speedMessage = "HIGHEST SPEED";}
}