Servo rotation and 315Mhz T/R help
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
I'm going to be using momentary switches, so will that make a difference? I'm not sure how you get the loop to run only once, maybe detach the servo? It I were to do that I would have to reattach it to the pin as the first step in the loop correct?
- Franklin97355
- Posts: 23912
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Servo rotation and 315Mhz T/R help
You need to fix the for loops first. Step through what you want to happen in english and then figure what code you need to accomplish each step, one at a time.
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
Im not sure what you mean by "fix the for loops first". Also do you mean write it out in pseudocode? Ok, but I thought that I did that. If you could point me in the direction of what I should read or look up that wouold be great. Would the finished code be something fairly easy, or is going to become very complex for someone like me with about zero C language programming skill?You need to fix the for loops first. Step through what you want to happen in english and then figure what code you need to accomplish each step, one at a time.
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
Code: Select all
CODE: SELECT ALL
#include <Servo.h>
int button1 = 4;
int press1 = 0;
int button2 = 5;
int press2 = 0;
Servo servo1;
int pos = 0;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(9);
Serial.begin(9600);
servo1.write(10);
}
void loop()
{
if ( (digitalRead(button1) == HIGH) && (pos == 10) )
{
for (pos = 10; pos < 100; pos +=1)
{
servo1.write(pos);
Serial.println(pos);
delay(15);
}
}
if ( (digitalRead(button2) == HIGH) && (pos == 100) )
{
for (pos = 100; pos > 10; pos -=1)
{
servo1.write(pos);
Serial.println(pos);
delay(15);
}
}
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Servo rotation and 315Mhz T/R help
Looks pretty close. But since you set pos to 0 at the start, you will never pass any of the 'if' tests. It is safer to be a bit more forgiving in your test conditions so that you don't get stuck:
Code: Select all
CODE: SELECT ALL
#include <Servo.h>
int button1 = 4;
int press1 = 0;
int button2 = 5;
int press2 = 0;
Servo servo1;
int pos = 0;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(9);
Serial.begin(9600);
servo1.write(10);
}
void loop()
{
if ( (digitalRead(button1) == HIGH) && (pos < 50) )
{
for (pos = 10; pos < 100; pos +=1)
{
servo1.write(pos);
Serial.println(pos);
delay(15);
}
}
if ( (digitalRead(button2) == HIGH) && (pos >=50) )
{
for (pos = 100; pos > 10; pos -=1)
{
servo1.write(pos);
Serial.println(pos);
delay(15);
}
}
}
It should be.Will the high output of the receiver be enough to set the input pin it is connected to change it so the servo will rotate?
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
So I should change the int pos=0 to int pos=10? I had everything set to 0 at first because I thought that I had the servo at the end of its rotation because it was humming pretty loud. I will make some changes to the code after work, post it here and see what you think. It did work one time (the servo rotated and the serial monitor showed the rotation) but after the first time nothing else happened. I think that my transmitter batteries may be low because I could only get LEDs to light very sporadically that I had set up in a different circuit to check that the r/t system was working.
Another question, what do you think I should use to power the whole setup? Could I get away with one battery to power everything: servo, reciever, and the trinket that will be running the code? I have a voltage regulator that works up to 25DCV and outputs 5DCV at 1.5amps max. I as thinking two 9v batteries in parallel to get enough current for the servo, but I'm not sure.
Another question, what do you think I should use to power the whole setup? Could I get away with one battery to power everything: servo, reciever, and the trinket that will be running the code? I have a voltage regulator that works up to 25DCV and outputs 5DCV at 1.5amps max. I as thinking two 9v batteries in parallel to get enough current for the servo, but I'm not sure.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Servo rotation and 315Mhz T/R help
The code I posted should work regardless of the starting value of pos.
Servos will consume 9v batteries in no time. You will want a battery pack with better high-current performance. How much space do you have to work with?
Servos will consume 9v batteries in no time. You will want a battery pack with better high-current performance. How much space do you have to work with?
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
Not much, the servo and the control board and the receiver will be going inside of a helmet. If I can get the code to work on a trinket(which was my original intention), that will free up some space. A rechargeable battery pack would be cool, but I'm not sure the size of the one I would need to power everything. Is it even possible to power everything with just one power source or will I need two? I am planning on using a micro sized servo, so that should cut down on the current draw a bit.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Servo rotation and 315Mhz T/R help
If you are going with a Trinket and a micro servo, then LiPo is an option. There is a 3.3v version of the Trinket, and most micro servos will run on as little as 3v. LiPo cells are 3.7v. They pack a lot of power for their size and perform will with high-current loads like servos.
I would think twice before putting my head inside a helmet with a liPo though. External mounting would be best.
http://www.adafruit.com/index.php?main_ ... po+battery
I would think twice before putting my head inside a helmet with a liPo though. External mounting would be best.
http://www.adafruit.com/index.php?main_ ... po+battery
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
Yeah not a big fan of having a lipo pack inside the helmet. I will see what other options I can come up with.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Servo rotation and 315Mhz T/R help
You might want to look into primary lithium cells like the CR123. These are less volatile than LiPos, but still have good energy density and current capacity.
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
How much current does he receiver draw? I looked at the schematic and I didn't see anything.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Servo rotation and 315Mhz T/R help
There is no manufacturer spec on that. If you have a multimeter, you should be able to measure it.
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
Ok, I was just wondering if there was a manufacturer spec. When I play with the code this weekend I will see what the current draw is as well as how much the servo will draw. I found a lithium 9v battery with 1200mah which should be able to power everything. Once the code works, then I have to alter it to work on a trinket, then I have to design a PC board that will have everything on it. The work has just begun lol.
- jc27
- Posts: 185
- Joined: Sun Nov 24, 2013 5:05 am
Re: Servo rotation and 315Mhz T/R help
Code: Select all
#include <Servo.h>
int button1 = 4;
int press1 = 0;
int button2 = 5;
int press2 = 0;
Servo servo1;
int pos = 0; // I'm resetting everything to 0-90 degree rotation
void setup()
{
pinMode (button1, INPUT);
pinMode (button2, INPUT);
servo1.attach (9);
Serial.begin(9600);
servo1.write (0); // set start position to 0 degrees?
}
void loop()
{
if ((digitalRead(button1)) == HIGH && (pos == 0)) // 0 to 90 rotation
{
for (int i = 0; i < 90; i ++) // using your recommendation but I thought the i is supposed to go in there
{
pos ++;
servo1.write(pos);
Serial.println(pos);
delay(15);
}
}
if ((digitalRead(button2)) == HIGH && (pos == 90))
{
for (int i = 90; i > 0; i --) // same as above not sure if its correct
{
pos --; // is this correct?
servo1.write(pos);
Serial.println(pos);
delay(15);
}
}
}
Please be positive and constructive with your questions and comments.