Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: cannot blink led with button push and arduino IDE crashes with memory error

  1. #1

    cannot blink led with button push and arduino IDE crashes with memory error

    This code correctly senses and prints the state of the button on the serial monitor:


    Code:
    byte btnPins[] = {
    19,20,21,22,23
    }; 
    byte btnCount = 5;  
    byte inputBtn = 0; 
    byte buttonState = 0;
    const int ledPin = 13;
    
    void setup()  {
    pinMode(ledPin, OUTPUT);
    
    for (byte thisBtn = 0; thisBtn < btnCount; thisBtn++) 
         pinMode(btnPins[thisBtn], INPUT_PULLUP);
    Serial.begin (9600);
        while (!Serial) { delay(10);}
    }
    
    void loop()  {
    for (byte thisBtn = 0; thisBtn < btnCount; thisBtn++)  {
      inputBtn = digitalRead(btnPins[thisBtn]);
      Serial.print("button ");
      Serial.print(thisBtn);
      Serial.println(inputBtn);
      if (buttonState == HIGH) {
        // turn LED on:
        digitalWrite(ledPin, HIGH);
      } else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
      }//end if
     }//end for
    }//end loop
    but the LED never blinks regardless of my button mashing.
    The code seems to upload fine


    Memory Usage on Teensy 4.1:
    FLASH: code:11212, data:3992, headers:8344 free for files:8102916
    RAM1: variables:4512, code:8544, padding:24224 free for local variables:487008
    RAM2: variables:12384 free for malloc/new:511904

    The serial monitor properly reports the state of buttons

    However the serial monitor eventually get bogged down and stops. The arduino IDE then stops with this error:

    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at javax.swing.text.GapContent.allocateArray(GapContent.java:94)
    	at javax.swing.text.GapVector.resize(GapVector.java:214)
    	at javax.swing.text.GapVector.shiftEnd(GapVector.java:229)
    	at javax.swing.text.GapContent.shiftEnd(GapContent.java:345)
    	at javax.swing.text.GapVector.open(GapVector.java:201)
    	at javax.swing.text.GapVector.replace(GapVector.java:142)
    	at javax.swing.text.GapContent.insertString(GapContent.java:132)
    	at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:723)
    	at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
    	at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
    	at javax.swing.JTextArea.append(JTextArea.java:477)
    	at processing.app.AbstractTextMonitor.updateTextArea(AbstractTextMonitor.java:224)
    	at processing.app.AbstractTextMonitor.lambda$message$2(AbstractTextMonitor.java:214)
    	at processing.app.AbstractTextMonitor$$Lambda$182/26316087.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at javax.swing.text.GapContent.allocateArray(GapContent.java:94)
    	at javax.swing.text.GapVector.resize(GapVector.java:214)
    	at javax.swing.text.GapVector.shiftEnd(GapVector.java:229)
    	at javax.swing.text.GapContent.shiftEnd(GapContent.java:345)
    	at javax.swing.text.GapVector.open(GapVector.java:201)
    	at javax.swing.text.GapVector.replace(GapVector.java:142)
    	at javax.swing.text.GapContent.insertString(GapContent.java:132)
    	at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:723)
    	at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
    	at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
    	at javax.swing.JTextArea.append(JTextArea.java:477)
    	at processing.app.AbstractTextMonitor.updateTextArea(AbstractTextMonitor.java:224)
    	at processing.app.AbstractTextMonitor.lambda$message$2(AbstractTextMonitor.java:214)
    	at processing.app.AbstractTextMonitor$$Lambda$182/26316087.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at javax.swing.text.GapContent.allocateArray(GapContent.java:94)
    	at javax.swing.text.GapVector.resize(GapVector.java:214)
    	at javax.swing.text.GapVector.shiftEnd(GapVector.java:229)
    	at javax.swing.text.GapContent.shiftEnd(GapContent.java:345)
    	at javax.swing.text.GapVector.open(GapVector.java:201)
    	at javax.swing.text.GapVector.replace(GapVector.java:142)
    	at javax.swing.text.GapContent.insertString(GapContent.java:132)
    	at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:723)
    	at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
    	at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
    	at javax.swing.JTextArea.append(JTextArea.java:477)
    	at processing.app.AbstractTextMonitor.updateTextArea(AbstractTextMonitor.java:224)
    	at processing.app.AbstractTextMonitor.lambda$message$2(AbstractTextMonitor.java:214)
    	at processing.app.AbstractTextMonitor$$Lambda$182/26316087.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at javax.swing.text.GapContent.allocateArray(GapContent.java:94)
    	at javax.swing.text.GapVector.resize(GapVector.java:214)
    	at javax.swing.text.GapVector.shiftEnd(GapVector.java:229)
    	at javax.swing.text.GapContent.shiftEnd(GapContent.java:345)
    	at javax.swing.text.GapVector.open(GapVector.java:201)
    	at javax.swing.text.GapVector.replace(GapVector.java:142)
    	at javax.swing.text.GapContent.insertString(GapContent.java:132)
    	at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:723)
    	at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
    	at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
    	at javax.swing.JTextArea.append(JTextArea.java:477)
    	at processing.app.AbstractTextMonitor.updateTextArea(AbstractTextMonitor.java:224)
    	at processing.app.AbstractTextMonitor.lambda$message$2(AbstractTextMonitor.java:214)
    	at processing.app.AbstractTextMonitor$$Lambda$182/26316087.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

    What?!

  2. #2
    Yep, you are writing to the serial monitor on every pass inside the loop. Meaning you are dumping tons of stuff to frequently. I'm guessing the buffer is getting overloaded and crashing the IDE.

    The Serial.prints inside the for are not really needed, OK for initial debugging, but once the code worked as expected, comment them out.

    look in your if statement (buttonstate is initialized but never used) don't you mean if (inputBtn == HIGH)?

    Also your are using input pullup and testing for high--meaning an un-pressed state will turn LED on (assuming you are using a NO button). You sure this is how you want it to operate?

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,223
    When you click Tools > Ports, you should see Teensy in 2 places. Select the one under "Teensy Ports". Then Arduino will use a highly optimized version of the serial monitor which can (usually on most computers) handle the high speed of Teensy 4 printing at maximum speed.

    If you select the one under "Serial Ports", the original Arduino serial monitor is used. It can't handle Teensy 4 max printing speed. Internally, its "GapContent" management consumes far too much memory. Shortly after Teensy 4 was released, a *lot* of work went into a "FifoDocument" replacement which can handle the high speed. But you only get it if you select the port under "Teensy Ports".

    Here's a blog article I wrote at that time, if you want to understand the problem.

    https://www.pjrc.com/improving-ardui...r-performance/

  4. #4
    @ KrisKasprzak thank you for picking up my absentminded mistake. yes the variable I need to check is indeed inputBtn. The idea of blinking an LED was my solution to debugging so I could remove the serial.print. The serial monitor scrolled so fast I couldnt see when a pushed button would give a zero. And if I put a delay in the loop, the loop ran so slowly, that I had to keep the button pressed a lonng time so it would be detected. I thought about using interrupts but after reading a bit, there seems to be some issues with using interrupts to detect button presses and debouncing the presses.

    @PaulStoffregen. AHA! that's why the teensy shows up with two serial ports in arduino IDE. I thought it was a bug or something. Thank you for the reference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •