Offline
Newbie
Karma: 0
Posts: 42
|
 |
« on: May 15, 2012, 11:20:44 pm » |
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! #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
Tesla Member
Karma: 96
Posts: 6368
|
 |
« Reply #1 on: May 16, 2012, 08:08:38 am » |
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
|
|
|
|
|
South Texas
Offline
God Member
Karma: 8
Posts: 976
|
 |
« Reply #2 on: May 16, 2012, 11:42:49 am » |
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
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #3 on: May 16, 2012, 05:18:21 pm » |
I want it to stop when I release the button.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #4 on: May 16, 2012, 05:37:57 pm » |
I also want it to continue from previous point.
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6368
|
 |
« Reply #5 on: May 16, 2012, 08:36:52 pm » |
Something like this: 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
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19034
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: May 17, 2012, 01:56:40 am » |
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #7 on: May 17, 2012, 05:52:33 pm » |
Thank you! It works now.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #8 on: May 17, 2012, 08:49:22 pm » |
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
Tesla Member
Karma: 96
Posts: 6368
|
 |
« Reply #9 on: May 17, 2012, 09:10:33 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #10 on: May 19, 2012, 02:15:14 pm » |
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
Tesla Member
Karma: 96
Posts: 6368
|
 |
« Reply #11 on: May 19, 2012, 04:42:35 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #12 on: May 20, 2012, 02:38:19 pm » |
Thank you!
|
|
|
|
|
Logged
|
|
|
|
|
|