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

Thread: T3.5 Locks Up when assigning Long to Float... (Surprised)

  1. #1
    Junior Member
    Join Date
    Jan 2017
    Posts
    10

    T3.5 Locks Up when assigning Long to Float... (Surprised)

    Just a thought for discussion on something that Surprised me.

    I have an Array of Floats, a Long, and an Int.

    Code:
    float graphData[80][2];
    long weight = 0;
    int graphPoint = 0;
    The code below was locking up the T3.5. Why?

    Code:
    graphPoint = 0;
    graphData[graphPoint][1] = weight;    << Locks up here.
    weight contained a value > 0 and < 1000
    Unfortunately, I am also controlling a motor via PWM to a motor Driver. When the T3.5 locks up, PWM keeps on trucking and the motor makes bad stuff happen when the Teensy can't monitor Force and OverTravel switches.. eek..

    What's weird is I'm fairly confident that this code was working previously. How?? I am using Visual micro now instead of Arduino IDE is the only difference I can think of.
    I fixed it with:
    graphData[graphPoint][1] = float(weight);

    This has me looking into implementing the Watchdog timer to reset on lockup.. to keep the PWM'd motor from destroying the earth.
    hopefully I can use this thread to implement watchdog..

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,355
    This isn't how we do things here. You need to post a complete program. Tiny fragments that don't even show the variable types are not enough.

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,862
    Paul is quite right - moving to a simple posted example allows cut and paste leading to help, and a clear idea of the problem without assumptions or inference. And often going to a simple sample shows the problem isn't as presented.

    For instance - this works on a T_3.5 and a T_3.6 - but the T_3.5 showed no output at first - then I added: "while(!Serial...)" and qBlink - and it is always printing and working.
    Code:
    #define qBlink() (digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) ))
    float graphData[80][2];
    long weight = 0;
    int graphPoint = 0;
    void setup() {
      /*
        pinMode(LED_BUILTIN, OUTPUT);
        while ( !Serial && millis() < 10000 ) {
        qBlink();
        delay(500);
        }
      */
      graphPoint = 0;
      Serial.println( weight );
      Serial.flush();
      graphData[graphPoint][1] = weight;
      for ( int ii = 10; ii >= 0; ii-- ) {
        weight = ii;
        Serial.println( weight );
        Serial.flush();
        graphData[graphPoint][1] = weight;
        weight = -ii;
        Serial.println( weight );
        Serial.flush();
        graphData[graphPoint][1] = weight;
      }
      pinMode(LED_BUILTIN, OUTPUT);
    }
    
    void loop() {
      qBlink();
      delay(1000);
    }

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,588
    more blind speculation: whenever subscripts are involved, strange random behavior often results from subscript out of bounds

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,355
    Just to show *why* we have the "Forum Rule" requiring a complete program when reporting problems, here is a complete program with those 2 tiny code fragments.

    Code:
    float graphData[80][2];
    long weight = 0;
    int graphPoint = 0;
    int count = 0;
    
    void setup() {
    }
    
    void loop() {
      Serial.print(count++);
      Serial.print(", ");
      graphPoint = 0;
      graphData[graphPoint][1] = weight;
      Serial.println(graphData[graphPoint][1]);
      delay(20);
    }
    I'm running this right now on a Teensy 3.5. Unsurprisingly, it's not locking up.

    Please, post a *complete* program which demonstrates the problem. Tiny code fragments don't allow us to get to the bottom of what's going wrong. It just wastes everyone's valuable time.

  6. #6
    Junior Member
    Join Date
    Jan 2017
    Posts
    10
    Sorry.
    Just a thought for discussion
    Looks like I got more than I bargained for.

    I was just up half the night trying to hit a deadline and decided to post this while i took a break.

    My main program is currently 4014 lines of code and has 11 #includes for libraries so it isn't trivial to pair it down to something that can be posted when in a rush.
    (Or maybe it would have been quicker to troubleshoot if I had done that, in fact....)
    I did get some help out of this discussion though. Thank you Paul and defragster who made the test programs showing that the portion i posted shouldn't be causing the problems.

    It turned out that I had passed the bounds of the array by 1 in an earlier part of the program but it wasn't locking up the program until I was assigning the
    Code:
    graphpoint = 0;
    Serial.println("This was being printed");
    graphData[graphPoint][1] = weight;
    Serial.println("This wasn't being printed");
    I fixed the array bounds issue at the same time as I added the float(weight).
    I'm not an idiot.. I promise.
    Weird how lack of sleep can cause such things.

    Again, Sorry.
    I'll try not to post here again unless I have a small program with a problem that can be duplicated. I'll just keep buying and shipping products with Teensy.

Posting Permissions

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