Go Down

Topic: One Servo with Two Switches (Read 1 time) previous topic - next topic

ebun91

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: [Select]
#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.

johnwasser

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?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

kf2qd

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?

ebun91

I want it to stop when I release the button.

ebun91

I also want it to continue from previous point.

johnwasser

Something like this:
Code: [Select]

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.
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

AWOL

"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.

ebun91


ebun91

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

johnwasser


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.

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

ebun91

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.

johnwasser


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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview