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

Thread: Wrote a library to simplify sketch. All compiles in IDE, but setup won't run.

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    5

    Wrote a library to simplify sketch. All compiles in IDE, but setup won't run.

    Hello,

    So here is what I'm doing. I have six printer carriages that each have an encoder strip, quadrature encoder and DC motor. For each of these, I have an arduino uno serving as a position decoder and motor controller. The arduinos are I2C slaves to a Teensy 3.5 board which requests the position from the Unos, receives midi notes which dictate the next position to set the carriages at, and sends commands to the uno to tell it what to set the PWM to and what DOs to toggle. I am able to read the encoders (I tested by reading from two encoders) via I2C in an earlier version of my code (Maestro_old.ino).

    Now since each printer carriage can have a different travel length, lines per inch of the code strip, and MIDI note offset, I thought it would greatly organize my sketch create a class to handle those properties and also the methods for sending that information via I2C. I then followed the arduino site's tutorial for creating a library. After getting it how I wanted it, I put the header and cpp files in an appropriately named folder in the arduino libraries directory, revised my Teensy 3.5 sketch to use the library, Verified, addressed all the errors in the sketch and library files, and now it can be verified without any errors showing. I uploaded my code to the teensy while it was connected to the Unos via I2C to read the encoders, but I was not getting the encoder positions to print in the Serial window like I was expecting. In fact, after unplugging the Teensy entirely from the circuits, adding Serial.println("SETUP"); to the beginning of setup(), and reuploading, it appears that my setup function isn't being executed at all.

    I'm certain I've made a mistake in my library, but without anymore compiler warnings to work from, I'm not sure how to troubleshoot this issue. Can you help me figure out what's wrong in my code? I'll gladly take any pointers concerning style or precautions or how to troubleshoot this. Thanks for your time.

    Teensy 3.5 configured as a USB Type: MIDI - Maestro.ino and Maestro_old.ino
    Library: DiddleyBow.h and .cpp
    Arduino Uno: Decotor.ino

    Libraries used: usbMIDI, Wire, IntervalTimer, Serial
    Attached Files Attached Files

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,769
    Not sure what sketch is Teensy main? But suspect it is possibly entering setup() and passing those first Serial.println("SETUP"); before the computer completes the connect to the Teensy?

    Starting setup() like this will show if that is the case:
    Code:
    void setup() {
    ... // setup code not needing to print
    	Serial.begin(115200);
    	while (!Serial && millis() < 4000 );
    	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
    	Serial.println("SETUP");
    ... // rest of setup where printing will work if connected when above times out ...

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Teensy main is Maestro.ino. I'll try this.

  4. #4
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    So the problem was actually that I did not put Wire.begin() before calling the findFrets() function in the constructor. findFrets() makes a call to home() which tries to send data over I2C. I must give credit to user Rajat_Bandejiya who apparently created an account on the Arduino forum just to point this out amidst some other ramblings about why programs hang at line Wire.endTransmission() (his only post).

    Another piece of the puzzle was like you said - giving setup enough time to connect to the computer after uploading. Your code helped.

    Thanks for the help!

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,769
    Glad that helped, Teensy gets into setup less than 400ms after power on, and Host Computer never has USB is never connected by the time that happens because until Teensy startup.c finishes its work (and waits until 300 ms after the clock started) it doesn't exist as a USB device since it is part of the processor, not an independent chip like some.

    It may only be another 50 to 150 ms (or more) depending on the computer if SerMon is ready to make the connection on the Host.

    So those early messages without that checking and waiting go into the bitbucket and that can leave it looking like the Teensy isn't running.

Posting Permissions

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