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