Servo rotation and 315Mhz T/R help

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Servo rotation and 315Mhz T/R help

Post by jc27 »

Ok, this is both my first post in any kind of programming or development forum and my attempt at modifying another person's sketch so please bare with me. I am trying to do something that seems so simple, but since I have no real idea what I'm doing, its getting frustrating.
I would like to thank Zoomkat over on the Arduino forums for the code. It does pretty much what I am looking for, but I want to rotate the servo using the 2ch 315 Mhz toggle T/R kit instead of a button or switch.
I would like to try to slow down the rotation a hair, so I increased the delay, but that didn't really do anything, is that the right idea?
In the code it has int press1 = 0. I know that it is referring to the button connected to pin 4, but what is the 0 doing, setting pin 4 to low?
To slow down the rotation do I have to put in a line like for(pos =0; pos> 90 pos =+1)? I think that I do, but once I start putting extra lines of code, I always get errors concerning the location of curly braces.

One more quick question and hopefully it is possible. This whole thing is for a costume and size is a big deal where the servo board is going to be located. I just bought 3 of the ATtiny trinkets hoping that they would be able to do the same thing as this code (once it works). Now I know the ATtiny85 is only an 8-bit chip and the IDE uses 16-bit. Is it possible to get the trinket to work like this? I installed the ATtiny library on my IDE, but I wasn't sure if the Servo8bit library would work.

Any help, ideas, or pointers in the right direction would be fantastic.
Hopefully I followed all the protocols for posting code and my questions, if not let me know and I will correct it.
Thanks for your help,
JC

Code: Select all


//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;   //[color=#0080FF]What does this mean[/color][/color]
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;   //[color=#0080FF] same here[/color]
Servo servo1;
int pos = 0;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(9);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(0);
    delay(500);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(90);
    delay(500);
  }
}


 

 
 
 


User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Anybody have any pointers or ideas of how I can get this to work?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_rick »

jc27 wrote:In the code it has int press1 = 0. I know that it is referring to the button connected to pin 4, but what is the 0 doing, setting pin 4 to low?
Press1 is just a variable intended to contain the state of the button when you read it - it's not connected to the button itself.
jc27 wrote:To slow down the rotation do I have to put in a line like for(pos =0; pos> 90 pos =+1)? I think that I do, but once I start putting extra lines of code, I always get errors concerning the location of curly braces.
Your code should look something like:

Code: Select all

for(int pos =0; pos< 90 pos++)
{
  servo1.write(pos);
  delay(10);  //some delay value which results in the speed you want
}

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

I was thinking that needed code like that. The code I have in the post is based off of setting the buttons to ground to get the servo to rotate. I tried to switch it to high for the wireless kit, but I couldn't get it to work. When I used the 315 MHz R/T to rotate the servo, either the servo wouldn't rotate at all or it won't stop. I have the toggle type of the 315 MHz kit, should I have gotten the momentary type? Does it matter if the high signal going to the input pin is steady or momentary, if so which is easier to code for?
I know this is probably a simple sketch, in my mind it should be, if pin x is high rotate +90, if pin y is high rotate -90. I am just starting out and it is throwing me for a bit of a loop. Does the 315 MHz kit have enough power to put a high signal on a pin that the arduino can read?
The code I have does work, but I was using a SPDT switch, and I want to utilize two separate SPST switches in the final version.
I am actually planning on using the trinket (which should be here any day now) to run the code, but that is way down the road. I know that is going to be another big road block due to the 16-8 bit libraries and such. One must first learn to crawl before they can walk.
Anyway thanks for the input and I will be asking for more help soon most likely.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_rick »

You don't need to turn on the pullups with the 315, but that shouldn't matter either way. You might want to change the logic to look for press == HIGH instead of LOW.

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Code: Select all


//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 90;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(9);
  digitalWrite(4, LOW); //enable pullups to make pin low
  digitalWrite(5, LOW); //enable pullups to make pin low
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == HIGH)
  {
    servo1.write(90);
    delay(500);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == HIGH)
  {
    servo1.write(180);
    delay(500);
  }
}
 
I changed the low to high and once its uploaded it keeps rotating, if I hit the button on the RF fob or not. When I connect the button pins to ground, it stops rotating and when I pull them off, it rotates according to which wire was pulled. So I ran the Xtran pins to ground and connected the Uno input pins to the RF pins and it does nothing. I haven't tried changing the code for the rotation speed yet, I want the RF system to work before I start messing with that part.

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Ok, I made some changes regarding the for (pos =0;) lines. When I would change (either up or down), I didn't really notice a change in speed. And now I'm getting an error: avrdude: stk500_getsync(): not in sync: resp=0x00
What does that mean?
Now with this sketch, as long as I move the switch back to center it seems to work, but if I leave it at one of the rotation positions (left or right), the servo keeps moving. How do I keep this from happening? I am going to be using SPDT switches for the rotation, so having it continuously moving is going to be a problem.
And finally, is there a way to get the servo stop making noise once it gets to its desired position? Its going to he close to my ear and hearing that constant humming from the servo is going to drive me crazy.

Code: Select all

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 0;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(9);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    for (pos =0; pos <= 90; pos +=1)
    {
      servo1.write(pos);
      delay(5);
    }
  }    
  
  press2 = digitalRead(button2);
 
  if (press2 == LOW)
  {
    for (pos =90; pos >= 0; pos-=1)
    {
      servo1.write(pos);
      delay(5);
    }
  }
}


User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

avrdude: stk500_getsync(): not in sync: resp=0x00
What does that mean?
Double check that you have the right COM port.
And finally, is there a way to get the servo stop making noise once it gets to its desired position?
To stop the movement and the noise, just call servo.detach(). That will stop sending control signals.

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

For that error, It seems to go away if I recompile and send the sketch to the Uno a couple of times. I'm not too worried about that error right now, but thanks for the answer on it.
As for the code, the rotation speed is good now with the new lines of code (pos += seemed to do the trick), so that part is ok. Now the issue is as the code stands the servo will continue to rotate when one of the switches is left on. That's how the sketch is written, but since I will be using SPDT push on push off switches, that's going to be a problem. I am guessing that I will need something that watches for a change of state on the switch input pin, so when switch 1 is pushed on it rotates, but just once, and will only rotate again from switch 1 when it is pushed again. The same for switch 2, but here is another potential problem, different switch configurations of on and off.
Here is what I would like the servo to do:
Switch 1 is pushed and the servo rotates 90 degrees (12 o'clock to 3 o'clock) if s1 is pushed again nothing happens until servo is returned to the 12 o'clock position by s2
Switch 2 is pushed and the servo rotates -90 degrees (3 o'clock to 12 o'clock) if s2 is pushed again nothing happens until servo is moved to the 3 o'clock position by s1

I hope this makes sense. And here is one more question; once I get this code to work the way I want it to, will it be able to be run by a trinket (with little to no changes)? I know that servos and the trinket have some issues with one another, but I am hoping that it will work, I need the servo control to be as small as possible because its going inside of a helmet and the trinket will be connected to the 315 MHz RF R/T set up (which doesn't seem to be able to run off of the Uno along with the servo. It looks like I will have to use a voltage regulator and or a separate power source for the RF R/T).
Thanks again for the help. Hopefully we can get this figured out.

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

Here is what I would like the servo to do:
Switch 1 is pushed and the servo rotates 90 degrees (12 o'clock to 3 o'clock) if s1 is pushed again nothing happens until servo is returned to the 12 o'clock position by s2
Switch 2 is pushed and the servo rotates -90 degrees (3 o'clock to 12 o'clock) if s2 is pushed again nothing happens until servo is moved to the 3 o'clock position by s1
There are two ways to do this. You can either use an analog feedback servo that tells you what position it is in, or you can add a state variable to your code that remembers the last command it completed. Then you add that information into your conditional check before moving the servo.

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

The analog feedback servo would work, but I I'm trying to keep the cost down so I will be using a standard mini servo.
I just got an arduino for dummies book, so I will do some reading on the state variable coding this week and see what I can come up with. Thanks for your help and I will let you know of my progress, and probably ask more questions.

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Sorry its been so long, another project has taken up more of my tiime than I thought it would. I have a momentarty 315 MHz reciever and it rotates the servo pretty close to how I want it to, except for the switch scenario. I did a little research and am I correct ini thinking that the Uno cannot read the position of the servo unless it has a feedback from it? How would I get the switches to be ignored until the servo is in the correct position for rotation, a if/else statement? Right now if I push a switch the servo will rotate, but if I hit it again it will jerk back and move forward.

Here is what I would like the servo to do:
Switch 1 is pushed and the servo rotates 90 degrees (12 o'clock to 3 o'clock) if s1 is pushed again nothing happens until servo is returned to the 12 o'clock position by s2
Switch 2 is pushed and the servo rotates -90 degrees (3 o'clock to 12 o'clock) if s2 is pushed again nothing happens until servo is moved to the 3 o'clock position by s1

Is there a way to do this with code and not require the use of an analog feedback servo?
Another question, would it be possible to use two pots o set start and stop points for the servo and code that wouold rotate it within those given points?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

am I correct ini thinking that the Uno cannot read the position of the servo unless it has a feedback from it?
Correct.
Here is what I would like the servo to do:
Switch 1 is pushed and the servo rotates 90 degrees (12 o'clock to 3 o'clock) if s1 is pushed again nothing happens until servo is returned to the 12 o'clock position by s2
Switch 2 is pushed and the servo rotates -90 degrees (3 o'clock to 12 o'clock) if s2 is pushed again nothing happens until servo is moved to the 3 o'clock position by s1
This should be very straightforward. On every s1 push, write the +90 degree position to the servo. If the servo is already there, another press will have no effect.
Same deal for S2. Ever time the s2 button is pressed, write the -90 degree position to the servo.
Another question, would it be possible to use two pots o set start and stop points for the servo and code that wouold rotate it within those given points?
Yes. That would be possible.

User avatar
jc27
 
Posts: 185
Joined: Sun Nov 24, 2013 5:05 am

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Here is what I would like the servo to do:
Switch 1 is pushed and the servo rotates 90 degrees (12 o'clock to 3 o'clock) if s1 is pushed again nothing happens until servo is returned to the 12 o'clock position by s2
Switch 2 is pushed and the servo rotates -90 degrees (3 o'clock to 12 o'clock) if s2 is pushed again nothing happens until servo is moved to the 3 o'clock position by s1
This should be very straightforward. On every s1 push, write the +90 degree position to the servo. If the servo is already there, another press will have no effect.
Same deal for S2. Ever time the s2 button is pressed, write the -90 degree position to the servo.
This should be very straightforward. On every s1 push, write the +90 degree position to the servo. If the servo is already there, another press will have no effect.
Same deal for S2. Ever time the s2 button is pressed, write the -90 degree position to the servo.
So how would I got about doing that? Can it be done with if/else staements or is it something more complex than that?

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Servo rotation and 315Mhz T/R help

Post by Franklin97355 »

Code: Select all

 press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    for (pos =0; pos <= 90; pos +=1)
    {
      servo1.write(pos);
      delay(5);
    }
  }    
Your code says:

if switch1 LOW
set servo to pos 0 (12 o'clock)
rotate 90 deg (3 o'clock)
Then the program loops (as it should) and you do it all over again

You need to only run the for loop once as long as the switch is closed.
Try modifying the code and see what you come up with and get back to us.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”