Pages: [1]   Go Down
Author Topic: One Servo with Two Switches  (Read 1377 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. I'm trying to control a servo with two 6mm tact switches. One makes it rotate clockwise and the other makes it rotate counterclockwise. I don't know if this code will work or not. I can't test it out, since I haven't soldered the servo's wires. Here is the code. Can someone please tell me if this will work? I know it is kinda of a lot to ask for, but this is for a school project and all help is really appreciated. Thank you!
Code:
#include <Servo.h>

Servo myservo;

int pos = 0;
int inputPin2 = 2;
int inputPin3 = 3;
int val1 = 0;
int val2 = 0;

void setup()
{
  myservo.attach(9);
  myservo.writeMicroseconds(1500);  // set servo to mid-point
}

void loop()
{
val1 = digitalRead(inputPin2);
val2 = digitalRead(inputPin3);
  if (val1 == HIGH && val2 == LOW);
  {
    for(pos = 0; pos < 180; pos += 0.1); 
    {                               
      myservo.write(pos);             
      delay(0);                     
    }
  }

  if (val2 == HIGH && val1==LOW);
  {
    for(pos = 0; pos < 180; pos -=0.1);
    {
      myservo.write(pos);
      delay(0);
      }
    }
  }


Once again, thank you.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That won't work.  You can't add 0.1 to an integer and expect it to change.   If pos is 0, pos + 0.1 is 0.1 which gets truncated to 0 when you store it in an integer variable.

The way the code is written the servo will start at around 90°.  When you push a button it will go to 0° and sweep slowly to 180° or go to 180° and sweep slowly to 0°.  Did you want the servo to stop where it is when you release the button or continue from where it last left off when you press the other button?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1024
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you want the servo to rotate slowly you will need to either use delay() or the more complex using millis().

Do you want the servo to be at either 0 degrees or 180 degrees? or just move when you press the button?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want it to stop when I release the button.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also want it to continue from previous point.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this:
Code:
void loop()
    {
    if (digitalRead(inputPin2))
        if (pos > 0)
            pos -= 1;

    if (digitalRead(inputPin3))
        if (pos < 180)
            pos += 1;

    myservo.write(pos);
    delay(10);  // Step about every 10 milliseconds.  100 steps per second.  Full sweep in 1.8 seconds.
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try here
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you! It works now.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Once again, thank you very much. I used the void loop you made for me and it works perfectly. Thank you.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Once again, thank you very much. I used the void loop you made for me and it works perfectly. Thank you.

I hope I made it clear enough so you could understand how it was working in case you wanted it to work differently.

Note that these variations all have roughly the same effect:
    pos = pos + 1;
    pos += 1;
    pos++;
    ++pos;

Typically one would use pos++ for increment and pos-- for decrement but I used the slightly more verbose "pos += 1" and "pos -= 1" to be more explicit.

Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it's very clear. I understand it now. Thank you. One last question. Sorry. Is there any way to make the servo go slower? I mean this speed is okay, but slower might work better in my case. Thank you.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it's very clear. I understand it now. Thank you. One last question. Sorry. Is there any way to make the servo go slower? I mean this speed is okay, but slower might work better in my case. Thank you.

Increasing the delay() at the end of loop() would make the servo go slower.  I had it set to 10 milliseconds which allows no more than 100 steps per second.  Change it to a higher value to get slower servo motion.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you!
Logged

Pages: [1]   Go Up
Jump to: