HI, im new to arduino or any type of programming.i have a uno and i am trying to figure out how to write a program that has one servo but 3 positions and two switches to go sequentially back and forth when pressing the buttons. example: if i push button one it would move the servo to 20 degrees, then press it again and it would go 40 degrees. i need button two to do the exact opposite. ive looked all over the web and on the forums and couldnt find what im looking for. i need a code that has "3" cases or "stages" just a simple code i can input the servo positions where i want them to go would be a life saver. any help would be great. sorry for the long post just wanted to get all the info i could give. thanks. im going to try and post a code that is kind of what i need, just another button and go the back the other way. i tried modifying it several times with no luck. [code] // C++ code
//
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*
*
*
*/ #include <Servo.h>
#define button 3
Servo Tservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int state = 0;
int old = 0;
int buttonPoll = 0;
void setup() {
Serial.begin(112500);
Tservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(button, INPUT);
Tservo.write(0);
}
In the code there are only two things that are important: the positions and a variable for the current position. The positions can be in an array, and the current position will be the index.
int position[3] = { 20, 40, 60 };
int index;
When a button is pressed the index is decremented or incremented and the servo goes to Tservo.write(position[index]);
If arrays scare you, then it is possible to write code without arrays. Then you only need a 'pos' or 'state' variable (they are the same).
Is that all the code you have ? Can you add more to the loop() ? For example reading the buttons.
welcome to the arduino-Forum.
Well done to describe your knowledge-level and posting your sketch.
For best use of this forum you should learn a few things how to post
Code should be posted as a so called code-section
You almost put it right to post your code as a code-section
the initial "[ code ] must be in its own line.
The code seems truncated
void loop() {
minimum needs a closing curly brace "}"
It is a good idea to search for example-codes. Though if your functionality goes beyond blinking a single LED or sweeping a single servo. The variants go into the millions and you will not always find what you exactly need.
Your code will evolve over time and this requires basic knowledge.
Take a look into this tutorial:
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
Such a user-forum is very different from things like WhatApp, Instagram etc.
Your post has just a medium length and there ist still more information needed to help you in all details
a very important thing to know is:
Servos need their own power-supply
There are many websites showing servos power-supplied through the arduino.
It is a bad idea. It only works for small servos without any weight-load.
The arduino can't supply enough current !
Trying to supply servos from the arduino quickly leads to resetting the arduino.
Another thing to mention:
There is a costfree registering free Arduino Simulator that is useful for many projects.
(though not for all) but definitely for servos
it is called
You can easily realise your school assignment with the IPO model.
Take a look at this simple abstraction of your task:
INPUT: Read and debounce the keys. PROCESSING: When the button is pressed, copy the value from the array of angles. Process the indicator into an array of angles. OUTPUT: Write the value to the servo.
thanks for the info but its part of a bigger project. i need to add to another program. i dont know if its allowed but i am willing to pay someone over paypal for a written program. that would be more than what i asked here. i can send pictures, wiring diagrams, components. its for shifting a linkage on a automatic transmission with a wiper motor turned into a servo. if anyone would like to message me i can tell you all the safety precautions plan on using. i have spent the last three days working on this and i have a really hard time retaining any information so it gets frustrating not making any progress. i really dont plan on programming anything else anytime soon. i just need to get this project done. please message me if your interested in helping me out. thanks.
One button makes it go up: 0, 20, 40. If it can go up.
The other button makes it go down: 40, 20, 0. If it can go down.
Do you need the speed to be handled? Or does your servo want to just be told to go to an angle and it will go at whatever speed it can.
Please say so, and add anything else like this simple that you can think of now, so anyone who does go to the trouble of writing this for you leaves enough flexibility for when you start adding features…
Based on your requirements, you need to program an Arduino Uno to control a servo motor through two buttons, each button moving the servo between three positions. One button will move the servo forward through its positions, and the other button will move it backward.
This code assumes that button one (connected to pin 3) will move the servo forward, and button two (connected to pin 4) will move it backward. The servo will move between three positions: 20 degrees, 40 degrees, and 60 degrees.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int button1 = 3; // button 1 connected to digital pin 3
int button2 = 4; // button 2 connected to digital pin 4
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(button1, INPUT_PULLUP); // set button 1 as input with pullup resistor
pinMode(button2, INPUT_PULLUP); // set button 2 as input with pullup resistor
myservo.write(0); // start at 0 degrees
}
void loop() {
// check if button 1 is pressed
if (digitalRead(button1) == LOW) {
delay(200); // debounce delay
if (pos < 2) {
pos++;
} else {
pos = 0;
}
myservo.write(pos * 20); // move to next position (20, 40, 60 degrees)
while (digitalRead(button1) == LOW); // wait for button release
}
// check if button 2 is pressed
if (digitalRead(button2) == LOW) {
delay(200); // debounce delay
if (pos > 0) {
pos--;
} else {
pos = 2;
}
myservo.write(pos * 20); // move to previous position (20, 40, 60 degrees)
while (digitalRead(button2) == LOW); // wait for button release
}
}
This code initializes the servo on pin 9 and sets up two buttons for controlling the servo's position. The pos variable is used to keep track of the servo's current position. When button 1 is pressed, the servo moves to the next position, and when button 2 is pressed, it moves to the previous position. The delay(200) function is used to debounce the buttons, preventing false triggers.
Remember to connect the buttons to the Arduino Uno with pull-up resistors, as the buttons are connected to ground and the code uses INPUT_PULLUP mode. This means the buttons are normally high, and pressing them connects them to ground, making them low.
I ask because it does not work. The servo can move multiple steps with each button press and release, the only way to make it behave is to use debounced digital inputs at the pushbutton input pins.
The 200 ms delay here and there is not doing what is necessary, and it is contributing to a sluggish response as well.