Trinket analogRead and Servos

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
co11in77
 
Posts: 5
Joined: Sun Oct 19, 2014 9:05 pm

Trinket analogRead and Servos

Post by co11in77 »

Hi there,
I am having a very difficult time trying to figure out how to hook up two potentiometers to the Trinket. check out my code below:


Code: Select all

#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)

#define SERVO1PIN 0   // Servo control line Pin #0
#define POTPIN   1 //pin 2 for analog 1
#define POTPIN2  2 //pin 4 for analog 2

Adafruit_SoftServo myservo;   //names the servo into an object
  
int const potPin = 1; // analog pin used to connect the potentiometer ,was A0
int val = 0;       // variable to store the value coming from the potentiom
int pos = 0;    // variable to store the servo position 
 
 
  
   
void setup() {
  
  myservo.attach(0);   // Attach the servo to pin 0 on Trinket

  
  analogRead(POTPIN);
  
  analogRead(POTPIN2);
  
}

void loop()  {
  
  int sensorValue=analogRead(POTPIN)*.02+2; // variable to read potentiometer on trinket pin 2
  
  int theAngle = analogRead(POTPIN2)*.1; //pin 3 on trinket
  int servoPos;  // variable to convert voltage on pot to servo position



///sensorValue *.25 + 2
  
  for(pos = 0; pos < theAngle; pos += 1)  // goes from 0 degrees to ? degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   
    delay(sensorValue *.02 + 2);                       // waits ms for the servo to reach the position 
  } 
  for(pos = theAngle; pos>=0; pos -=1)     // goes from ? degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(sensorValue *.02 + 2);                       // waits ?ms for the servo to reach the position 
  } 
}





[b]
My goal for this code is operate 1 servo, with two potentiometers, one that controls the angle and the other that controls the delay, but not too slow, that's why I multiplied the value by .02 then added 2 milliseconds, this delay is what controls the speed. Parts of this code came from sources on the internet somewhere but I forget the original source. Sure, it compiles, but nothing happens. I've tried other tests and such and the trinket is working properly. I know there are different ways to call the analogRead and declaring the pins but I have tried everything I can think of. See below for the code that I used with my Arduino Uno that worked:[/b]


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 
               
int const potPin = A0; // analog pin used to connect the potentiometer
int val = 0;       // variable to store the value coming from the potentiom
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
 
  int sensorValue = analogRead(A0);
  
  
  int theAngle = analogRead(A3)*.1;
  
  
  
  
  
///sensorValue *.25 + 2
  
  for(pos = 0; pos < theAngle; pos += 1)  // goes from 0 degrees to ? degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   
    delay(sensorValue *.02 + 2);                       // waits ms for the servo to reach the position 
  } 
  for(pos = theAngle; pos>=0; pos -=1)     // goes from ? degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(sensorValue *.02 + 2);                       // waits ?ms for the servo to reach the position 
  } 
}

THERE HAS TO BE A SIMPLER WAY THAN HOW I AM DOING THIS. Regardless, any help would be appreciated, I would much rather use a few dozen trinkets for my project because of size but I cannot figure out how to get the code in the correct format for Trinket. Thanks so much to whoever replies. Also...is there a way to map potentiometers? Just seems inefficient how I kinda tried to do it. But that's the least of my concerns haha. I just want it to work like it did with my Uno. Pleaseeee by very clear as to what pins I need to attach the potentiomers and servo to. Thanksss
Last edited by adafruit_support_bill on Mon Oct 20, 2014 5:47 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

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

Re: Trinket analogRead and Servos

Post by adafruit_support_bill »

I am having a very difficult time trying to figure out how to hook up two potentiometers to the Trinket.
It might help if you showed us your wiring.
Please see the pinout diagram in the tutorial: https://learn.adafruit.com/introducing-trinket/pinouts
Pins #2, #3 and #4 all have analog input capability as analog pins 1, 2, and 3. As written, you should have your pots wired to pins #2 and #3.

Also note that pins #3 and #4 are used for USB communication. The pullup resistors will skew the readings and you may need to disconnect your circuitry to program the board.

User avatar
co11in77
 
Posts: 5
Joined: Sun Oct 19, 2014 9:05 pm

Re: Trinket analogRead and Servos

Post by co11in77 »

Hi there,
thanks so much for the reply. I've been looking at that pinout guide for quite some time over this past week I guess there's just something I am not getting. Anyways, I switched up my wiring to the picture that is attached and I am still having no luck figuring out what it is I am doing wrong. Any help is much appreciated
Attachments
trinket.png
trinket.png (351.1 KiB) Viewed 463 times

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

Re: Trinket analogRead and Servos

Post by adafruit_support_bill »

Can you explain what it does? Have you tested the servos with some simple sweep code to verify that they work?

User avatar
co11in77
 
Posts: 5
Joined: Sun Oct 19, 2014 9:05 pm

Re: Trinket analogRead and Servos

Post by co11in77 »

Hi,
Yes I have, although the sweep code example only had one potentiometer. Everything does work...just not with my code. The goal for this is to have one potentiometer control the angle and the other to control the speed (or the delay). Essentially this servo arm is moving back and forth continually, the one pot telling it how fast, the other pot telling it how much to move back and forth
thanks

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

Re: Trinket analogRead and Servos

Post by adafruit_support_bill »

You servo code is much different from the example code. The example uses the map function to choose a servo position then simply writes it.

Code: Select all

  potValue=analogRead(POTPIN);                // Read voltage on potentiometer
  servoPos = map(potValue, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180) 
  myServo1.write(servoPos);                    // tell servo to go to position
Instead of using the map function, your code is multiplying the raw reading by 0.1 which will limit the range of motion to about 100 degrees.
The sensorValue is multipled by 0.02 at the time you read it, which will put the result in the range of about 0-20.
You then multiply that again by 0.02 at point of use, which makes it effectively a zero delay.

I'd expect it to be moving erratically - regardless of the speed setting.

User avatar
co11in77
 
Posts: 5
Joined: Sun Oct 19, 2014 9:05 pm

Re: Trinket analogRead and Servos

Post by co11in77 »

Hi there,
I'm fairly new to coding, how would I go about implementing this into the code that I have? I completely missed that I was multiplying it twice (I was aiming for a limited motion of 100 degrees and about a 20 milisecond minimum delay) here is what I have now:


Code: Select all

#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)

#define SERVO1PIN 0   // Servo control line Pin #0
#define POTPIN   1 //pin 2 for analog 1
#define POTPIN2  2 //pin 4 for analog 2

Adafruit_SoftServo myservo;   //names the servo into an object
  
int const potPin = 1; // analog pin used to connect the potentiometer ,was A0
int val = 0;       // variable to store the value coming from the potentiom
int pos = 0;    // variable to store the servo position 
 
 
  
   
void setup() {
  
  myservo.attach(0);   // Attach the servo to pin 0 on Trinket

  
  analogRead(POTPIN);
  
  analogRead(POTPIN2);
  
}

void loop()  {
  
  int sensorValue=analogRead(POTPIN)*.02+2; // variable to read potentiometer on trinket pin 2, this makes the angle a maximum of about 100 degrees
  
  int theAngle = analogRead(POTPIN2)*.1; //pin 3 on trinket
  int servoPos;  // variable to convert voltage on pot to servo position



///sensorValue *.25 + 2
  
  for(pos = 0; pos < theAngle; pos += 1)  // goes from 0 degrees to ? degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   
    delay(sensorValue);                       // waits ms for the servo to reach the position 
  } 
  for(pos = theAngle; pos>=0; pos -=1)     // goes from ? degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(sensorValue);                       // waits ?ms for the servo to reach the position 
  } 
}

How do I go about adding the map function so I can control the speed and angle for the continuous loop? A constant motion is what I'm looking for...like how will it look/where will it go in the code? thanks so much in advance
Last edited by adafruit_support_bill on Mon Oct 20, 2014 6:19 pm, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
co11in77
 
Posts: 5
Joined: Sun Oct 19, 2014 9:05 pm

Re: Trinket analogRead and Servos

Post by co11in77 »

Not sure if you saw this in the other response, but this is what I have on my Uno and it works perfectly, I'm just looking for the same thing, but the Trinket version;

Code: Select all

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 
               
int const potPin = A0; // analog pin used to connect the potentiometer
int val = 0;       // variable to store the value coming from the potentiom
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
 
  int sensorValue = analogRead(A0);
  
  
  int theAngle = analogRead(A3)*.1;
  
  
  
  
  

  
  for(pos = 0; pos < theAngle; pos += 1)  // goes from 0 degrees to ? degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   
    delay(sensorValue *.02 + 2);                       // waits ms for the servo to reach the position 
  } 
  for(pos = theAngle; pos>=0; pos -=1)     // goes from ? degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(sensorValue *.02 + 2);                       // waits ?ms for the servo to reach the position 
  } 
}
Last edited by adafruit_support_bill on Mon Oct 20, 2014 6:20 pm, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

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

Re: Trinket analogRead and Servos

Post by adafruit_support_bill »

How do I go about adding the map function so I can control the speed and angle for the continuous loop?
Scaling by simple multiplication will work. It is just less obvious what the result will be. The map() function is very explicit:

Code: Select all

result = map(rawValue, rawLowLimit, rawHighLimit, scaledLowLimit, scaledHighLimit);
To map from a 0-1024 range to a 0-100 range you would use:

Code: Select all

angle = map(rawValue, 0, 1024, 0, 100);
what I have on my Uno and it works perfectly, I'm just looking for the same thing, but the Trinket version;
Although the API is similar, the trinket servo library is fundamentally different than the standard Servo Library. It requires you to explicitly set up the interrupt in your setup:

Code: Select all

   // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)
And an explicit interrupt handler.

Code: Select all


// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo1.refresh();
    myServo2.refresh();
  }
}
Both of which can be found in the TrinketKnob example.

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

Return to “Microcontrollers”