I am so lost, I don’t understand it at all. I copy and paste the code and nothing works (it’s arduino, not trinket). I dont have things like 10k pots. The trinket doesnt work with any arduion stuff, like I tried the switch for onboard led (i even changed all pins to 1, because i know trinket led is on pin1 and i could also hook up the swtich to it). there’s no serial mode or something.
You don’t need pots, they won’t be used in your code. The Trinket doesn’t have a UART, so you’ll need to use Softserial code to do in software what’s normally done in hardware. You’ll also need to use a different servo library.
Start with code that simply reads the switch and lights the LED. Then add code to command the servo to one of 2 positions, depending on the switch reading, high or low. Then modify that to command the servo based on consecutive, debounced pushes of the button. If you fail along the way it may be worthwhile to get the serial link to the PC working so you can send debug messages.
I don’t know which pins, if any, are best used for each function. I do know only 1 pin can be used to talk to the PC.
Belial and I are kindred spirits in all of this. I’ve browsed tuts and the forums and love all the ideas, but i think we’re both mechanical and idea minded, but when it comes to this we’re a bit in the dark lol. I feel exactly like him, but I just don’t have the stuff to put into practice at the moment. Here’s hoping you figure it out Belial.
I’m not even sure what you consider most of this Arduino stuff… technically robotics I guess, but software engineering due to the extensive coding?
Also, thoughts on what Mee N Mac said, basically make a flow chart of processes from start to finish on what you need to get working. Start with the basics (leds, etc) and then move to the functionality on how to get the signals and mechanical aspects to ‘talk’ to each other and make the basics activate. Just my thoughts on it, not sure if 100% correct. I went to college for mechanical engineering some, but didn’t do any of this while I was in. (Didn’t graduate due to finances and consequential lack of interest.)
I read a lil more re; Trinket and comm back to the PC. Seems while the USB link uses 2 of the 5 I/O pins, these can’t be used to send messages back to the PC via that USB connection. The pins can be used by the code but you’ll need another serial-USB converter and USB cable to send debug msgs to the PC.
5 GPIO - 2 shared with the USB interface. The 3 independent IO pins have 1 analog input and 2 PWM output as well. The 2 shared IO pins have 2 more analog inputs and one more PWM output. Even though you can program Trinket using the Arduino IDE, it’s not a fully 100% Arduino-compatible. There are some things you trade off for such a small and low cost microcontroller! *Trinket does not have a Serial port connection for debugging so the serial port monitor will not be able to send/receive data *Some computers’ USB v3 ports don’t recognize the Trinket’s bootloader. Simply use a USB v2 port or a USB hub in between *We do not have full Windows 8 driver compatibility tested. At this time we only have it working with Mac, Linux or Windows 7/XP
-If using an FTDI Friend: The green #0 wire should connect to the RX pin and the black GND wire connects to the GND pin -If using a USB console cable: The green #0 wire connects to the White Wire on the console cable and the black GND wire to the Black ground wire on the console cable. This sketch demonstrates use of the SendOnlySoftwareSerial library, a lightweight implementation of SoftwareSerial only for transmitting serial text. You may also use the standard SoftwareSerial library but it will take up additional space in the limited Trinket or Gemma memory. If you use this library, download the zip file at the link above and install per the Adafruit tutorial All About Arduino Libraries. As you will only be using the decode sketch for getting codes, the regular SoftwareSerial should be fine - uncomment the SoftwareSerial lines, comment out the SendOnlySoftwareSerial lines (do not use both libraries at the same time). Connect Trinket GPIO #0 / Gemma Pin D0 to your FTDI Friend or USB console cable or similar receive pin and connect a ground from your circuit to the FTDI ground. The FTDI Friend is receiving what the Trinket or Gemma is transmitting.
So IMO getting a debug link to “see” what’s happening in the code is a bit of a PITA. Only do if/when needed to solve a problem … a reversal of my normal advice. But leave pin 0 open for this purpose.
Okay well I got a 2/5 wheel potentiometer, I figured it’d help me maybe learn some stuff and it could be useful in determining servo mounting position in my case.
However while I got the trinket-servo tutorial working, the servo only moves like 10* from the full throw of the wheel pot… so yea im hitting a wall here guise
I do have the adafruit softservo library, of course.
That looks proper. So you have to ask yourself how much effort you want to put into debugging a side task. Post your code and perhaps the fault will be obvious.
I was going to post a base sketch but I realized that adding the rest was a 10 min copy’n’paste from the servo code. So here’s a version that should do your task. Perhaps debug this (since I can’t run it) instead ? It will require rewiring your BB. Servo to pin #2 and momentary normally open switch btw pin #3 and gnd. Hopefully the comments explain how it’s supposed to work.
#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)
//declare the constants to be used
#define LEDPIN 1 //pin attached to led
#define servoPIN 2 //Servo control line (orange) on Trinket Pin #2
#define SWitchPIN 3 //input from N.O. momentary switch
#define posOpenCMD 170 //command in deg to servo to open the door
#define posCloseCMD 10 //command in deg to servo to close the door
#define DBdelay 50 //delay in ms btw readings of the switch
#define SRVOdelay 500 //min time in ms btw openings and closings of door
#define MAXcnt 3 //need this many same consecutive readings of button pressed
//declare the variables used
boolean doorOPEN = false; //desired door state set to closed
boolean SWstate = true; //state of switch, open = TRUE = not pushed
byte SWcnt = 0; //counter of same consecutive switch readings w/button = pressed
Adafruit_SoftServo myServo; //create a servo object
void setup() {
// 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!)
//set the pins to be ins or outs
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW); //turn LED off
pinMode(servoPIN, OUTPUT);
pinMode(SWitchPIN, INPUT_PULLUP);
//CMD the servo to close the door
myServo.attach(servoPIN); // Attach the servo on Trinket
myServo.write(posCloseCMD); // Tell servo to go to closed position
delay(SRVOdelay); // Wait 500 ms for the servo to reach the position
}
void loop() {
//read the switch
if(digitalRead(SWitchPIN) == LOW){ //see if switch is being pushed
SWcnt ++; //SW reads low = pushed, increment debounce counter
if(SWcnt >= MAXcnt){ //test if switch is really being pushed AND done bouncing
SWcnt = 0; //done bouncing, reset counter for next push
doorOPEN = !doorOPEN; //reverse desired door state
if(doorOPEN == true){
digitalWrite(LEDPIN, HIGH); //turn LED on
myServo.write(posOpenCMD); //tell servo to go to open position
} else {
digitalWrite(LEDPIN, LOW); //turn LED off
myServo.write(posCloseCMD); //tell servo to go to closed position
}
delay(SRVOdelay); //min wait btw open/close cmds for servo to go to position
}
} else {
SWcnt = 0; //switch is released or bouncing
}
delay(DBdelay); //wait for next read of switch
}
// 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
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 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();
}
}
/*******************************************************************
SoftServo sketch for Adafruit Trinket. Turn the potentiometer knob
to set the corresponding position on the servo
(0 = zero degrees, full = 180 degrees)
Required library is the Adafruit_SoftServo library
available at https://github.com/adafruit/Adafruit_SoftServo
The standard Arduino IDE servo library will not work with 8 bit
AVR microcontrollers like Trinket and Gemma due to differences
in available timer hardware and programming. We simply refresh
by piggy-backing on the timer0 millis() counter
Required hardware includes an Adafruit Trinket microcontroller
a servo motor, and a potentiometer (nominally 1Kohm to 100Kohm
As written, this is specifically for the Trinket although it should
be Gemma or other boards (Arduino Uno, etc.) with proper pin mappings
Trinket: USB+ Gnd Pin #0 Pin #2 A1
Connection: Servo+ - Servo1 Potentiometer wiper
*******************************************************************/
#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)
// We demonstrate two servos!
#define SERVO1PIN 0 // Servo control line (orange) on Trinket Pin #0
#define POTPIN 1 // Potentiometer sweep (center) on Trinket Pin #2 (Analog 1)
Adafruit_SoftServo myServo1, myServo2; //create TWO servo objects
void setup() {
// 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!)
myServo1.attach(SERVO1PIN); // Attach the servo to pin 0 on Trinket
myServo1.write(90); // Tell servo to go to position per quirk
delay(15); // Wait 15ms for the servo to reach the position
}
void loop() {
int potValue; // variable to read potentiometer
int servoPos; // variable to convert voltage on pot to servo position
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
delay(15); // waits 15ms for the servo to reach the position
}
// 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
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 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();
}
}
I was going to post a base sketch but I realized that adding the rest was a 10 min copy’n’paste from the servo code. So here’s a version that should do your task. Perhaps debug this (since I can’t run it) instead ? It will require rewiring your BB. Servo to pin #2 and momentary normally open switch btw pin #3 and gnd. Hopefully the comments explain how it’s supposed to work.
What do you mean debug? Check it for errors?
BB= breadboard? I dont have one, I’m just using a bit of solder and lots of crappy ways to hold the stuff together, heh. I’ll try that code.
You keep saying btw (by the way) and i’m confused every time you say it because it doesnt make sense. I’ll try that code, thanks.
Adafruit guy says that I’m using an audio pot, which is not linear, which is maybe why i’m getting a weak response.
I tried that code with Servo 5v=usb, Grnd=Grnd, Signal=#2, and switch to Grnd/#3 (i dont think it matters which is which, but i do i put grnd-grnd, lead to #3)
and i get ‘myServo1’ was not declared in this scope in the bold oragen line. The black area says:
sketch_oct11a.ino: In function ‘void __vector_10()’:
sketch_oct11a:71: error: ‘myServo1’ was not declared in this scope
Did you write that code up to basically do what I am asking for, with juts replace the angle for the throw? That’s really awesome of you man, thanks a lot. I’m not sure what the issue is, I searched the issue, but myServo1.refresh(); is exactly in the code for the trinket pot tutorial so I have no idea why it wouldnt work here.
The error occurs because the servo tutorial used 2 servos, myServo1 and myServo2, and I used just one and changed the name to myServo. Except I missed 1 instance.
Change this part:
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();
}
}
to;
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;
myServo.refresh();
}
}
As for the pot’s taper … I don’t buy it. I do worry that the refresh code (the stuff above) isn’t working as advertized. Assuming it now compiles and runs, watch the onboard LED. It should cycle w/alternate pushes of the button. The servo should alternate btw 2 positions. We’ll see …
However, the problem is still the throw degree. I recognize that the following:
#define posOpenCMD 170 //command in deg to servo to open the door
#define posCloseCMD 10 //command in deg to servo to close the door
Is the throw degrees, but when I mess with those it just goes from 20* of throw to 0-15*. I’m trying to get around 180* here (i know it’s usually only like 160-170 on servos, that’s fine too).
Could this be due to the usb voltage or something?
edit: Is it possible the servo is damaged? At one point I recall a smokey smell but I looked at the gears inside and they all look alright.
If the LED cycles then I’m reasonably sure the initial servo commands are going out. You see the servo move. Same symptoms as the example servo code. Either the servo is damaged (unlikely IMO) or the example code is wrong for the ATTiny when it comes to doing the interrupt. Thus the refresh isn’t happening and the pulses stop and so does the servo. Should be easy to fix, just need to ditch the interrupt and send the refresh in a 20 ms loop. Give me a few minutes.
Here’s some code to see if my guess above is correct. The main loop() now runs about every 20 msecs and does the refresh. However the switch debouncing is probably AFU as a result. That’s easily fixable if this solves the servo problem.
#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)
//declare the constants to be used
#define LEDPIN 1 //pin attached to led
#define servoPIN 2 //Servo control line (orange) on Trinket Pin #2
#define SWitchPIN 3 //input from N.O. momentary switch
#define posOpenCMD 170 //command in deg to servo to open the door
#define posCloseCMD 10 //command in deg to servo to close the door
#define DBdelay 20 //delay in ms btw readings of the switch
#define SRVOdelay 20 //min time in ms btw openings and closings of door
#define MAXcnt 3 //need this many same consecutive readings of button pressed
//declare the variables used
boolean doorOPEN = false; //desired door state set to closed
boolean SWstate = true; //state of switch, open = TRUE = not pushed
byte SWcnt = 0; //counter of same consecutive switch readings w/button = pressed
Adafruit_SoftServo myServo; //create a servo object
void setup() {
// 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!)
//set the pins to be ins or outs
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW); //turn LED off
pinMode(servoPIN, OUTPUT);
pinMode(SWitchPIN, INPUT_PULLUP);
//CMD the servo to close the door
myServo.attach(servoPIN); // Attach the servo on Trinket
myServo.write(posCloseCMD); // Tell servo to go to closed position
delay(SRVOdelay); // Wait 20 ms for the next servo cmd
}
void loop() {
myServo.refresh(); //resend the servo pulse
//read the switch
if(digitalRead(SWitchPIN) == LOW){ //see if switch is being pushed
SWcnt ++; //SW reads low = pushed, increment debounce counter
if(SWcnt >= MAXcnt){ //test if switch is really being pushed AND done bouncing
SWcnt = 0; //done bouncing, reset counter for next push
doorOPEN = !doorOPEN; //reverse desired door state
if(doorOPEN == true){
digitalWrite(LEDPIN, HIGH); //turn LED on
myServo.write(posOpenCMD); //tell servo to go to open position
} else {
digitalWrite(LEDPIN, LOW); //turn LED off
myServo.write(posCloseCMD); //tell servo to go to closed position
}
//delay(SRVOdelay); //min wait btw open/close cmds for servo to go to position
}
} else {
SWcnt = 0; //switch is released or bouncing
}
delay(SRVOdelay); //wait for next read of switch
}
// 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
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 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;
//myServo.refresh();
//}
//}
You can see where I commented out code, put in the refresh and changed the delay values. Since the debounce may be iffy now pay attention to the LED to see the desired door state.