Calling funcions :>

Sorry if that topic lyies somewhere on forum...I wanna tell u that I am newbie to it.
I want to make my servomechanism rotate after clicking a buttons(there are two buttons for two directions).
It is simple to do for one button, but when second added it bugs somehow the servo instead of stopping on the 90 deggree its calling function of secondbutton.CODE HERE:


#include <Servo.h>
#define buttonPin 13
#define buttonPin 12
Servo serwo;

int pozycja = 90;//1st position of servo 0-180 pozycja+=position
int mgora = 130;
int gora = 170; //positions of servo
int mdol = 40;
int dol = 10;

void setup()
{

serwo.attach(2); //Serwomechanism pin
serwo.write(pozycja);//default setting of serwo : 90 stopni
Serial.begin(57600);//
while (!Serial); //
pinMode(2,OUTPUT);//servo output
pinMode(13, INPUT_PULLUP); //button input

}

void loop()
{
if (digitalRead(13) == HIGH)
{serwo.write(mgora);}
if(digitalRead(12) == HIGH)
{serwo.write(mdol);}
else if(digitalRead(13) == LOW){serwo.write(pozycja);}
}

It's a good thing that pin 12 defaults to input, isn't it? (Floating though, which is sad)

Please remember to use code tags when posting code

You would be interested to look into the ide example and follow the switch debounce tutorial there too.....

When you use INPUT_PULLUP the pin reads HIGH when nothing is connected to it. When a button connects the pin to Ground it reads LOW. I think you have your logic backward.

#include <Servo.h>
const byte Button1Pin = 12;
const byte Button2Pin = 13;
const byte SerwoPin = 2;

Servo serwo;

const int pozycja = 90;/ /1st position of servo 0-180 pozycja+=position
const int mgora = 130;
const int gora = 170;  //positions of servo
const int mdol = 40;
const int dol = 10;

void setup() {
  Serial.begin(57600);
  while (!Serial);

  serwo.attach(SerwoPin);
  serwo.write(pozycja); //default setting of serwo : 90 stopni

  // pinMode(2, OUTPUT); //servo output
  pinMode(Button1Pin, INPUT_PULLUP); //button input
  pinMode(Button2Pin, INPUT_PULLUP); //button input
}

void loop() {

  // If button2 is NOT pressed, go to mgors?
  if (digitalRead(Button2Pin) == HIGH) {
    serwo.write(mgora);
  }

  // Then if button1 is NOT pressed, go to mdol?
  if (digitalRead(Button1Pin) == HIGH) {
    serwo.write(mdol);
  }
  else // Button 1 is pressed
    // If both are pressed, go to pozycja?
    if (digitalRead(Button2Pin) == LOW) {
      serwo.write(pozycja);
    }
}