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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

So give me your honest opinion, knowing what the sketch is and what I plan to use it for, what would you recommend? I would like something small, easy to program, and simple. Would a mini be the easiest way to run the servo?

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 could go with a Mini, or even a Trinket. There is a SoftServo library that works on the Trinket: https://learn.adafruit.com/trinket-gemm ... ntrol/code
You could probably get it to work with an 8MHz bare 328 chip too.

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 if I were to use the bare 328 chip (no crystal) all I would need is a few caps on the input power to help filter it right? I would pop it in the arduino, load the sketch, then good to go? What is the input voltage for it, 5v?
I'm going to try the trinket this week to see how it goes since I have 3 fo them. What is the difference between the arduino mini, micro, and nano? They look the same, I wouldn't need to load the new servo library for them like the trinket correct?

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 »

So if I were to use the bare 328 chip (no crystal) all I would need is a few caps on the input power to help filter it right? I would pop it in the arduino, load the sketch, then good to go?
You would need to make sure that the appropriate fuses were set to use the internal clock.
What is the input voltage for it, 5v?
Using the internal 8MHz clock, you could run it at a lower voltage. 3.7v from a LiPo would work.
What is the difference between the arduino mini, micro, and nano? They look the same
There are a few variations on the Mini. I believe most use an Atmega 128. The Micro uses a 32U4 (same as Flora and Leonardo). I believe the nano is an Atmega 328.
You can find full specs on these boards at Arduino.cc
http://arduino.cc/en/Main/arduinoBoardMicro

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 think I am going to try the trinket, if that fails I will go with the mini.

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Made a little progress. I decided to use my Mac as the programing machine (I need practice using it), so forgive me if I ask dumb questions regarding it. Anywho, I installed the Adafruit Arduino 1.05 IDE (since I didn't want to play around with modifying the IDE myself, and thanks for providing a finished IDE) and installed the Adafruit SoftServo library. Everything looks in order since I can see the servo library and the trinket in the drop down menus. I ran a couple of the example trinket sketches and they worked, so I think I'm in business there as well.
Now here come the questions lol. What kind of alterations do I need to make to the code for it to work on the trinket? Will it make a difference if I ran the trinket at 8MHz instead of 16MHz? I think that if I run it at 16MHz that I need to add the lines of code from the trinket introduction as well as select the 16MHz from the board menu, but is that it (other than changing the pin layout in the code to match the trinket)?
I did some playing with the fob transmitter and it should work perfect with the push button switches I plan on using, so at least that part is finished.
I received the atmel chips and the boardduino Saturday, so if the trinket doesn't look like it will work, I can start playing with those, but ideally the trinket or an arduino mini are what I would like to use.

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 »

What kind of alterations do I need to make to the code for it to work on the trinket?
Post the code that you have now & we can check for things that might need to be changed.

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 <avr/power.h> // set trinket to 16MHz taken from intro to trinket learning module
#include <Adafruit_SoftServo.h>  //adafruit servo library for the trinket
int button1 = 0; //down rotation for servo
int press1 = 0;
int button2 = 2; //up rotation for servo
int press2 = 0;
Adafruti_SoftServo servo1;  //declare servo from Adafruit library ?
int pos = 0;

void setup()
{
 if (F_CPU == 16000000) clock_prescale_set(clock_div_1);  // set clock to 16MHZ from intro to trinket learning module
 pinmode (button1, INPUT);
 pinmode (button2, INPUT);
 SERVO1.ATTACH (1);
SERVO1.WRITE (0);
}
void loop()
{
  if ((digitalRead(button1) == HIGH && (pos ==0))
  {
   for (int i = 0; i < 90; i ++)
   {
    pos ++;
    servo1.write (pos);
    delay (30);
    }
   }
  if ((digitalRead (button2) == HIGH && (pos == 90))
  {
   for (int i = 90; i > 0; i --)
   {
    pos --;
    servo1.write (pos);
    delay (30);
    }
   }
}
OK, here is the code I have after reading through the "intro to trinket", "trinket servo control", and "adding libraries" learning modules. I am unsure if the servo code is correct because in the learning module there is a "#define servo" and "#define pin" in the example, but I didn't know if I needed them since the "int button1 and 2" are there already. I think I have the pins corresponding to the trinket, but I could be wrong on that too.
Now for power, I will be using 4 AA batteries. That shoould be enough to run everything on a breadboard to see if everything works. I will be powering the wireless receiver from the 5v out on the trinket, since it has a low current draw. The servo will get power straight from the battery , since it pulls around 500mA. I may try a smaller servo to see if I can get the current draw down a bit too try to save battery life, but since I will be using regular AA batteries, I don't think its too big a deal since just about everyone has some sitting around.
Would be the benefit to run the trinket at 16Mhz rather than 8Mhz? I know the current draw would be a little less, but how would it effect the execution of the code?

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 »

As long as you have the pins right, I don't see any incompatibilities.
Trinket pinouts are defined here: https://learn.adafruit.com/introducing-trinket/pinouts
The multiple use pins can be confusing, but the diagram should make it clear.

The servo library should compensate for the different timing between 8 and 16 MHz. Unless I am wrong about that, either one should work.

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 other than the current draw, there isn't any real benefit with going with 16 MHz over 8Mhz? I am going to try to do a test run later tonight, and I will post tomorrow with the results.

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 were doing some heavy processing or trying to drive a lot of neopixels, then the high speed would be a benefit. But for just running a servo, 8MHz should be sufficient.

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 <avr/power.h> // set trinket to 16mhz
#include <Adafruit_SoftServo.h>  // adafruit trinket servo library
int button1 = 0;  // down servo rotaton
int press1 = 0;
int button2 = 0;  // up servo rotation
int press2 = 0;
Adafruit_SoftServo servo1;
int pos = 0;

void setup()
{
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1); // set clock to 16Mhz
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);
  servo1.attach (1);
  servo1.write (0);
}

void loop()
{
  if ((digitalRead(button1)) == HIGH && (pos == 0))
  {
    for (int i = 0; i < 90; i ++)
    {
      pos ++;
      servo1.write (pos);
      delay (30);
    }
  }
  if ((digitalRead(button2)) == HIGH && (pos == 90))
  {
    for (int i = 90; i > 0; i --)
    {
      pos --;
      servo1.write (pos);
      delay (30);
    }
  }
}

Here is the code just as it is in the IDE. It compiles and uploads (I was using the 16mhz trinket instead of the 8Mhz and it gave an error, once I selected the 8Mhz trinket it uploaded).
Once the code is uploaded, I hit either button on the transmitter fob and nothing happens. I am powering the servo from a separate power supply from the trinket and the receiver. The receiver is powered from the 5v on the trinket and the trinket is powered from the computer USB.
I have a couple of leds connected in a separate circuit to the receiver, and when I move the output pins from the receiver to the LEDs they light up like they should when I hit a button on the fob.
Since the trinket has no serial communication, I can't see what is happening, so I don't know if I have a wiring problem or the trinket isn't receiving a signal from the receiver.
After looking at the "Trinket (and Gemma) Servo Control" learning module, the example code in that module has some lines in it that I am wondering if I should have them.
For example:
#define SERVOPIN1 0 // Servo control line on trinket pin #0 (I though that I defined the servo pin in the void setup)
#define POTPIN 1 // pot sweep on trinket pin #2 (I'm not using a pot, but again I thought that I defined the button pins in the void setup)
OCR0A = 0xAF // I have no idea what this is
TIMSK = _BV(OCIE0A) // again no idea
The last two lines say that they will refresh the servo, is that why it doesn't do anything?
I have no idea what I am doing, if I hadn't just spent a lot of money on parts, I would start thinking about giving up on this project.
Would it be easier to use an arduino mini or micro since I wouldn't have to worry about using a different servo library or the frequency of the timer?

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

Re: Servo rotation and 315Mhz T/R help

Post by jc27 »

Also, when I hit the reset button on the trinket, I hear a beep and the servo goes crazy while the read bootloader LED flashes then the servo stops when the LED stops. This tells me that I may have the trinket ouput correct to the servo, but I am probably wrong.

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 don't need the #define statements. But you do need the lines that setup the timer to refresh 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 »

Ok, I will give it a try tonight. Can I use the code from the Gemma tutorial or do I need to write new code? If so I have no idea of how to write it for refreshing the servo.

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

Return to “Arduino”