Author Options:

How should i write this program (arduino)? Answered

Hi,To whoever is reading this.I've been working with arduino for a few days and I don't knoiw how to make a program where if you press on button the arduino receives an input on some pin as HIGH  and then it increases the position of the servo clockwise and when you press another button and the arduino receives HIGH on some other pin and makes the servo tern anticlockwise.I've been trying to nail it but i just can't get it .I even tried this program and it did'nt work .Can anyone HELP me?.Thanks in advance. 
#include <Servo.h>

Servo myservo; 

int pos = 0;
int up=13;
int down=12;
int a=0;

void setup()

void loop()
{ if(up==HIGH)
  for(a=1; pos = 0; pos <= 180; pos += 1)
  for(a=0; pos = 180; pos>=0; pos-=1)    



Your "For" loops are written wrong. The way they should be used is that:

#1- You set up a variable with a startup value (for example "x=0")

#2- You state the conditional statement to tell the program what the conditions have to be in-order to run the loop code. (for example: Run this code over and over as long as X is less than 11. This would be written as "X<11")

#3- You state how that starting value (X) should change each time the loop is run (for example X= X+1 or X++ for the abbreviation, would tell the program to add 1 to the value of X.

#4- Brackets {} containing the code to be run over and over again.

All together, it would look like this:

For(X=0; X<11;X++) { repeated code }

A lot of time, you'd use the X again within the repeated code, like telling the program to print the value of X during the repeated code, would result in having the numbers 0 through 10 printed.

First rule of programming: state the problem clearly.

1.) Press button A, servo turns clockwise AND STOPS.
2.) Press button B, servo turns anticlockwise AND STOPS.
3.) Press the opposite button, servo immediately goes in the opposite direction,
4.) When buttons are held in, servo stops moving.

Does that describe the points of what you want to do ?

1)press button A , servo turns clockwise until button a is pressed or till pos=180
2)press button B ,servo turns anti-clockwise until button b is pressed or till pos=0

So you press it in, the servo starts, you release and it continues, you press again and it stops ?