Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Adafruit Sample servo project won't compile Trinket M0
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit Sample servo project won't compile Trinket M0

by guitartoys on Thu Apr 16, 2020 11:09 pm

Hi,

I'm really struggling here. I'm not a programmer, and am really trying to learn.

I downloaded the code from here: https://learn.adafruit.com/trinket-gemm ... ntrol/code

I've got the libraries loaded, drivers, etc. I can get the blink code to work fine. I was able to get their Guardian Robot to work with the regular Trinket.

All I'm trying to do is move a servo 90 degrees with a single button push, open/close.

I couldn't find anything for that, so I thought I would start with this project.

Help...

I'm getting all of these errors.

Trinket_Gemma_Servo_Control:57:8: error: expected constructor, destructor, or type conversion before '(' token

SIGNAL(TIMER0_COMPA_vect) {

^

D:\Documents\Arduino\Trinket_Gemma_Servo_Control\Trinket_Gemma_Servo_Control.ino: In function 'void setup()':

Trinket_Gemma_Servo_Control:34:3: error: 'OCR0A' was not declared in this scope

OCR0A = 0xAF; // any number is OK

^~~~~

Trinket_Gemma_Servo_Control:35:3: error: 'TIMSK' was not declared in this scope

TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!)

^~~~~

Trinket_Gemma_Servo_Control:35:16: error: 'OCIE0A' was not declared in this scope

TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!)

^~~~~~

Trinket_Gemma_Servo_Control:35:12: error: '_BV' was not declared in this scope

TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!)

^~~

D:\Documents\Arduino\Trinket_Gemma_Servo_Control\Trinket_Gemma_Servo_Control.ino:35:12: note: suggested alternative: '_B'

TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!)

^~~

_B

D:\Documents\Arduino\Trinket_Gemma_Servo_Control\Trinket_Gemma_Servo_Control.ino: At global scope:

Trinket_Gemma_Servo_Control:57:7: error: expected constructor, destructor, or type conversion before '(' token

SIGNAL(TIMER0_COMPA_vect) {

^

exit status 1
expected constructor, destructor, or type conversion before '(' token
Code: Select all | TOGGLE FULL SIZE
/*******************************************************************
  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)

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

guitartoys
 
Posts: 10
Joined: Wed Feb 27, 2019 6:18 pm

Re: Adafruit Sample servo project won't compile Trinket M0

by dastels on Thu Apr 16, 2020 11:51 pm

First of all, can you confirm that you have the correct board selected in the Tools menu.

Dave

dastels
 
Posts: 3023
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit Sample servo project won't compile Trinket M0

by guitartoys on Fri Apr 17, 2020 12:02 am

Yep,

Adafruit Trinket M0
USB Stack - TinyUSB
Programmer USBtinyISP

As I mentioned, I was able to get the Blink program working OK.

I was just figuring that a tutorial program would at least compile and work.

The big offender seems to be:

Trinket_Gemma_Servo_Control:57:8: error: expected constructor, destructor, or type conversion before '(' token

SIGNAL(TIMER0_COMPA_vect) {

But the entire error message is in my earlier post.

Thanks.

Michael

guitartoys
 
Posts: 10
Joined: Wed Feb 27, 2019 6:18 pm

Re: Adafruit Sample servo project won't compile Trinket M0

by adafruit_support_bill on Fri Apr 17, 2020 5:37 am

That example uses a library that is specific to the older 8-bit ATTiny-based Trinkets and Gemmas.

For the newer M0 versions, you can use the standard Arduino Servo library and example code.

adafruit_support_bill
 
Posts: 78210
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Sample servo project won't compile Trinket M0

by guitartoys on Fri Apr 17, 2020 6:29 pm

Thank you for the actual help.

I both learned that the Blink program was using pin one, when now it's pin 13.

And I switched over to the Servo library, and have the basic code working.

Just trying to get some actual help on how to incorporate some code into my code to make the servo run slower.

I found 2 chunks of code that work by themselves, but I haven't been able to get any real help in trying to incorporate it.

Thanks.

Michael

guitartoys
 
Posts: 10
Joined: Wed Feb 27, 2019 6:18 pm

Re: Adafruit Sample servo project won't compile Trinket M0

by adafruit_support_bill on Fri Apr 17, 2020 7:17 pm

I found 2 chunks of code that work by themselves, but I haven't been able to get any real help in trying to incorporate it.

Without any details about what chunks of code you are referring to or how you are trying to incorporate them, it is not possible to offer any advice on that front.

adafruit_support_bill
 
Posts: 78210
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Sample servo project won't compile Trinket M0

by guitartoys on Fri Apr 17, 2020 7:56 pm

I was hoping to hear back from you. :-)

Basically, I grabbed some old bounce code (I tried the new bounce 2 code, and couldn't seem to get it to work, but figured if it ain't broke...), that toggled the LED, and then added the servo code to make the servos go. It's working as expected.

But as I mentioned I wanted to add some code to slow the speed of the servo (I'm making a fence gate for a buddy of mine's HO Train set)

So I came across some code to do run the servo's slower, but can't sort out how to make it work inside my code.

Here's my code:

Code: Select all | TOGGLE FULL SIZE
/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin 4 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 4;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

#include <Servo.h>

Servo myservo;  // create servo object to control a servo


// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int pos = 0;
int lastpos = 90;
unsigned long currentTime = millis();
unsigned long previousTime = currentTime;
unsigned long delayTime = 50;

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers




void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object
  // set initial LED state
  digitalWrite(ledPin, ledState);



}

void loop() {
 
 
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);
 if (ledState== HIGH)
 {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   //myservo.write(0);
 }
 else
 {
 myservo.write(90);
 }
 
  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}


And this is the sweep code:

Code: Select all | TOGGLE FULL SIZE
/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}


Any help would be appreciated.

Thanks much

Michael

guitartoys
 
Posts: 10
Joined: Wed Feb 27, 2019 6:18 pm

Re: Adafruit Sample servo project won't compile Trinket M0

by adafruit_support_bill on Sat Apr 18, 2020 6:14 am

this code will sweep the servo from 0 to 180 degrees over about 2.7 seconds.
Code: Select all | TOGGLE FULL SIZE
 if (ledState== HIGH)
 {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   //myservo.write(0);
 }

And this code will move it back quickly:
Code: Select all | TOGGLE FULL SIZE
 else
 {
 myservo.write(90);
 }


If you want to slow the return, you just need to do the same thing as before in reverse:
Code: Select all | TOGGLE FULL SIZE
else
{
  for (pos = 180; pos >0 180; pos -= 1) { // goes from 180 degrees to 0 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

adafruit_support_bill
 
Posts: 78210
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Sample servo project won't compile Trinket M0

by guitartoys on Mon Apr 20, 2020 6:51 pm

Hey, thank you very, very much for this.

I'm currently swamped with work, and I will get back to this, this week.

Again, thanks.

Michael

guitartoys
 
Posts: 10
Joined: Wed Feb 27, 2019 6:18 pm

Re: Adafruit Sample servo project won't compile Trinket M0

by guitartoys on Wed Apr 29, 2020 3:12 pm

OK,

Now I've got an entirely different problem.

I finally got round to working on the code ya'll gave me and now my Trinket doesn't seem to be recognized. But only after initial programming.

First I need to say that the 1st time I brought up the compiler, it told me there was a library update for the boards I should load it, which I did. So this is the one thing that changed. So this could be the root of the problem.

But I would grab a fresh Trinket M0, plug it into the USB, it would be found drivers load, the compiler sees it, I upload some code, unplug it, hook it up to a power supply, and the blink or bounce program runs fine.

But I go to plug it back into the USB to continue programming work and my computer tells me that the USB device is not recognized.

Any ideas?

Thanks.

Michael

guitartoys
 
Posts: 10
Joined: Wed Feb 27, 2019 6:18 pm

Please be positive and constructive with your questions and comments.