Hi, Members kindly helped me out a few weeks back with my thermometer sketch, which works fine, however, I want to slow the servo movement as it's very jerky. Can anybody offer some advice as to how I can incorporate a delay? into my existing code, please. I entered a delay after the digital write command, not making any difference. (the delay command is not important to the control program) The servo is an MG90S and works directly from pin 5 and actuates a drum with an angle of 0 >180` The servo tends to be very jerky.
Here is my sketch:-
/* Atmega 328
*
* tested 10/02/2024
*/
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int SERVO_PIN = 5; // Arduino pin connected to Servo Motor's pin
const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin
const float TEMPERATURE_THRESHOLD = 30; // °C 20
Servo servo; // create servo object to control a servo
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library
float temperature;
float servopos;
void setup() {
Serial.begin(9600); // initialize serial
servo.attach(SERVO_PIN); // attaches the servo on pin 5 to the servo object
sensor.begin(); // initialize the sensor
}
void loop() {
delay(1000);
sensor.requestTemperatures(); // send the command to get temperatures
temperature = sensor.getTempCByIndex(0); // read temperature in Celsius
servopos = (180.0 / 12.0) * (temperature-10.0); //servopos = 180 - servoposraw; // 20
if (servopos < 0) servopos = 0;
if (servopos > 180) servopos = 180;
servo.write(servopos);
delay (15);
Serial.println(servopos);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C => servo angle: ");
}
3 posts - 3 participants