Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATTiny404: After 1556 calls to analogWrite, sketch restarts. #1130

Open
magoldsm opened this issue Jul 18, 2024 Discussed in #1129 · 3 comments
Open

ATTiny404: After 1556 calls to analogWrite, sketch restarts. #1130

magoldsm opened this issue Jul 18, 2024 Discussed in #1129 · 3 comments

Comments

@magoldsm
Copy link

Discussed in #1129

Originally posted by magoldsm July 18, 2024
In the attached code, I make continuous calls to analogWrite. After 1556 calls, the sketch appears to crash and restart. It occurs regardless of which port I use. It also seems unaffected by malloc'ing 1k of memory or adding a useless 1k byte array, which suggests the problem is not a memory leak.

The attached file is actually an INO file, renamed to TXT to make it acceptable as an attachment.

Thanks for any help anyone is able to render!

Michael

AnalogWrite_Test.txt

@magoldsm
Copy link
Author

Swapped out the 404 for a 1614. Interestingly, it now runs for 1590 calls before restarting!

@moose4lord
Copy link
Contributor

I tried your code on an ATtiny814 and didn't see a problem. I was too lazy to hook up a device to capture the Serial output, but I added some code to blink an LED during setup to detect if the 814 crashed and restarted. It did not crash and restart.

My test code:

#define PINKY   1   // pin 3, PIN_PA5
#define RING    0   // pin 2, PIN_PA4
#define MIDDLE  6   // pin 8, PIN_PB1
#define INDEX   7   // pin 9, PIN_PB0
#define THUMB   10  // pin 13, PIN_PA3

int port = INDEX;
int writes;

void setup() {
  // put your setup code here, to run once:
  pinMode(port, OUTPUT);
  for(int i=0; i<5; i++) { // blink the LED to show the ATtiny crash/restart
    digitalWrite(port, 0); delay(200);
    digitalWrite(port, 1); delay(200);
  }

  Serial.begin(115200);
  delay(1000);

  Serial.println("Starting");

  auto cp = malloc(1024);
  writes = 0;
}

byte data[1024];      // Taking up memory for this array made no difference.

void loop() {
  Serial.print("writes: "); Serial.println(writes);
  for (int i=0; i<256; i += 2) {
    Serial.print('+');
    analogWrite(port, i);
    writes++;
    delay(5);
  }
  
  Serial.println("");
  Serial.print("writes: "); Serial.println(writes);
  for (int i=254; i>=0; i -= 2) {
    Serial.print('-');
    analogWrite(port, i);
    writes++;
    delay(5);
  }
  Serial.println("");
}

@SpenceKonde
Copy link
Owner

SpenceKonde commented Jul 26, 2024

byte data[1024]; will not use any memory if the data is never accessed - it will be optimized out. To prevent that, declare the array volatile. That will allow you test if it's a memory problem, which dollars to donuts it is.
malloc and it's minion, the String class, are the hands of the devil on embedded systems like this. Avoid at almost any cost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants