Servomotor Project 5 (edited)

Hello,

I want to modify project 5 (ServoMoodindicator) so that servomotor goes back to his starting angle after adjusting the potmeter, see code (??).

I hope someone can help me out.

Greetings,

Kaz

/*
Arduino Starter Kit example
Project 5 - Servo Mood Indicator

This sketch is written to accompany Project 5 in the Arduino Starter Kit

Parts required:

  • servo motor
  • 10 kilohm potentiometer
  • two 100 uF electrolytic capacitors

created 13 Sep 2012
by Scott Fitzgerald

Arduino Starter Kit Multi-language — Arduino Official Store

This example code is part of the public domain.
*/

// include the Servo library
#include <Servo.h>

Servo myServo; // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int potVal; // variable to read the value from the analog pin
int angle; // variable to hold the angle for the servo motor
int pos = 0;

void setup() {
myServo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); // open a serial connection to your computer
}

void loop() {
potVal = analogRead(potPin); // read the value of the potentiometer
// print out the value to the Serial Monitor
Serial.print("potValue: ");
Serial.print(potVal);

// scale the numbers from the pot
angle = map(potVal, 0, 1023, 0, 179);

// print out the angle for the servo motor
Serial.print(", angle: ");
Serial.println(angle);

// set the servo position
myServo.write(angle);

// wait for the servo to get there
delay(15);

// back to angle 0
// ???
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

myServo.write(0);
1 Like

Thank you for the reply Paul, but now my servomotor can't be controlled with the potmeter.

// back to angele 0
myServo.write(0);
delay(1000);

Hello kazvr013
Take some time and describe the desired functionality of the sketch in simple words and this very simple and short form, including all dependencies between inputs and outputs.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

1 Like

re-edit your first posting.
Your code should be formatted as a code-section.
This manual explains how to do this

best regards Stefan

I'm going to take a guess that once you adjust the pot and the servo moves to it's set position and back to 0 that you do not want the servo to move again unless the pot value (and subsequently the angle) changes.

In order to accomplish this I would recommend the following:

  1. Save the last value of angle
  2. If the the current angle is different than the last angle then move the servo to it's set position and back to 0; otherwise do nothing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.