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.
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'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?

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

Re: Servo rotation and 315Mhz T/R help

Post by Franklin97355 »

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.

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

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

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

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);
     }
   }
 }

Here is some new code. I think its close to working. Another question I have is whether or not the 315 MHz transmitter/receiver system will even work for this. 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?

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

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

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);
     }
   }
 }
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?
It should be.

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

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.

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

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

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?

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

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.

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

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

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

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Yeah not a big fan of having a lipo pack inside the helmet. I will see what other options I can come up with.

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

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

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.

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

How much current does he receiver draw? I looked at the schematic and I didn't see anything.

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

Re: Servo rotation and 315Mhz T/R help

Post by adafruit_support_bill »

There is no manufacturer spec on that. If you have a multimeter, you should be able to measure it.

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

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

#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);
    }
  }
}
      
It seems to work. I had to use the 5v from the breadboard unit, but it drew too much current and I got a warning bubble on the windows task bar. Also, I think it fried my backlit keyboard.I can type but no lights, I hope it will return when I reboot the computer. Fingers crossed.

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

Return to “Arduino”