Problem with getSingleDebouncedPress() function in Atmel Studio 7

Hardware:
I have a A-star 32U4 SV (item #: 3115) and am using the external pololu programmer v2 (item #: 3170).

Setup:
I’m using atmel studio 7 (7.0.1006). To create my project I use the “Create project from Arduino sketch” option and choose “Arduino Leonardo” as my board. I also use the “Bottons.ino” example sketch from the A-Star 32U4 Arduino library. I then add the A-Star 32U4 Arduino library in the solution explorer and add the library directory as an “include path” in the “toolchain” setup.

Problem:
I change the example code a bit as shown in the following:

#include <AStar32U4.h>

AStar32U4LCD lcd;
AStar32U4ButtonA buttonA;

int main()
{
  lcd.clear();
  lcd.print("Prs BtnA");

  while (1)
  {
    if (buttonA.getSingleDebouncedPress())
    {
      // blink LED
      ledGreen(1);
      delay(200);
      ledGreen(0);
    }
  }
}

The code compiles fine and the controller is programmed without any problems. The controller displays to the LCD “Prs BtnA”, but when I press button A nothing happens. I think the problem is with the function “getSingleDebouncedPress()” and here’s why…

When I compile and program the following code to the controller there are no errors.

#include <AStar32U4.h>

int main(void)
{
     // Turn the LEDs on.
     ledRed(1);
     ledYellow(1);
     ledGreen(1);

     // Wait for a second.
     delay(1000);

     // Turn the LEDs off.
     ledRed(0);
     ledYellow(0);
     ledGreen(0);

     // Wait for a second.
     delay(1000);
}

But the LEDs just turn on and stay on. The controller seems to “freeze” when it gets the “delay()” function. So I changed the “delay()” function to “_delay_ms()” (which is just avr-libc function) as shown in the following:

#include <AStar32U4.h>

int main(void)
{
     // Turn the LEDs on.
     ledRed(1);
     ledYellow(1);
     ledGreen(1);

     // Wait for a second.
     _delay_ms(1000);

     // Turn the LEDs off.
     ledRed(0);
     ledYellow(0);
     ledGreen(0);

     // Wait for a second.
     _delay_ms(1000);
}

and the program runs fine; the LEDs flash. My guess is the function “getSingleDebouncedPress()” uses the “delay()” function.

The Question:
Does any know why the “getSingleDebouncedPress()” and “delay()” functions aren’t working and how I can fix it?

Obviously there is nothing wrong with the library because these functions work just fine in the demo program that comes preprogrammed on the controller.

I tried this in “debug” mode and “release” mode with no change.

I included a PDF with snapshots showing how I setup up the project in Atmel Studio 7. I also included the output from the “Buttons” code and the makefiles (there are two: one for the Arduino library, which I appended with “(ArduinoCore)”, and one for the actual program).

Makefile (6.2 KB)
Makefile (ArduinoCore) (8.5 KB)
output.txt (35.7 KB)
setup.pdf (955.9 KB)

Hello.

From your code, it looks like you are defining your own main() and are not using the main() that Atmel Studio provides (see main.cpp in the src folder of your project for more details). If you want to define your own main() then you need to call init(), which will setup the microcontroller for use with the Arduino core features (e.g. using delay()). However, when creating projects from an Arduino sketch, it is probably better for you to keep the Arduino IDE’s setup() and loop() format rather than trying to bypass it.

- Amanda