So i want my servo to start at 90 degrees then go to 135 degrees stay there for x amount of time go to 45 degrees stay there for x amount of time and then back to 90 degrees. But when i upload this code to my arduino it starts at 90 degrees goes to 135 degrees stays there for x time then goes to 120 and then back to 135. It might be something minor or a small numeric mistake but i cant figure it out.
Below is a zip video of what i am describing above.
Thank you
servo.zip (2.8 MB)
// Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include <Servo.h>
// Variables
float hum; // Stores humidity value
float temp; // Stores temperature value
int degrees = 90; // Initialize degrees to 90
unsigned long lastServoMoveTime = 0;
unsigned long servoMoveInterval = 8000; // Interval between servo movements (milliseconds)
// Constants
#define DHTPIN 2
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTTYPE DHT22
dht DHT;
Servo SERVO;
#define RELAYPIN 10
#define SERVOMOTOR 9
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.init();
lcd.clear();
lcd.backlight();
pinMode(RELAYPIN, OUTPUT);
pinMode(SERVOMOTOR, OUTPUT);
SERVO.attach(9);
if (degrees != 90) {
// if degrees not 90 make 90
degrees = 90; // Make degrees 90
}
}
void loop() {
byte CelsiusSymbol[] = {
0b11100,
0b10100,
0b11100,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
int chk = DHT.read22(DHTPIN);
hum = DHT.humidity;
temp = DHT.temperature;
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print('c');
lcd.createChar(0, CelsiusSymbol);
lcd.setCursor(11, 0);
lcd.write((byte)0);
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(hum);
lcd.print("%");
unsigned long currentMillis = millis();
// Move servo every servoMoveInterval milliseconds
if (currentMillis - lastServoMoveTime >= servoMoveInterval) {
lastServoMoveTime = currentMillis;
if (degrees == 90) {
// 90 to 135 degrees counter clockwise (aristerostrofa)
degrees = 105; // Go from 90 to 105
servoMoveInterval = 2000; //stay 2 seconds
} else if (degrees == 105) {
degrees =120; // Go from 105 to 120
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 120) {
degrees = 135; // Go from 120 to 135
servoMoveInterval = 8000; // Stay at 135 degrees for 8 seconds $$$$
// 135 to 45 degrees clockwise (deksiostrofa)
} else if (degrees == 135) {
degrees = 120; // Go from 135 to 120
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 120) {
degrees = 105; // Go from 120 to 105
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 105) {
degrees = 90; // Go from 105 to 90
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 90) {
degrees = 75; // Go from 90 to 75
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 75) {
degrees = 60; // Go from 75 to 60
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 60) {
degrees = 45; // Go from 60 to 45
servoMoveInterval = 7000; // Stay at 45 degrees for 7 seconds $$$$$
// 45 to 90 degrees counterclockwsise (aristerostrofa)
} else if (degrees == 45) {
degrees = 60; // Go from 45 to 60
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 60) {
degrees = 75; // Go from 60 to 75
servoMoveInterval = 2000; // stay 2 seconds
} else if (degrees == 75) {
degrees = 90; // Go from 75 to 90
servoMoveInterval = 8000; // Stay at 90 degrees for 8 seconds $$$$$$$$
}
SERVO.write(degrees);
}
// Adjust as needed, this is a small delay to prevent constant servo movement
delay(2000);
}
[/code]