simple class problem

Status
Not open for further replies.

AdmiralCrunch

Well-known member
hi,

I have adopted the adruino libary-tutorial to a very simple class.. the problem is, that it compiles till the end.. then my windows shows that a usb-device disconnected.. immediately re-connect again.. and nothing else..

I just can't figure out what causes this problem :/

Mux.h
Code:
#ifndef Mux_h
#define Mux_h

#include "Arduino.h"

class Mux {
  public: 
    Mux();
    void pollMuxDigital();
  private:
   
};

#endif

Mux.cpp
Code:
/*
  Mux.cpp 
*/

#include "Arduino.h"
#include "Mux.h"

Mux::Mux() {
  Serial.println("MUX INIT"); 
}

void Mux::pollMuxDigital() {
    Serial.println("pollMuxDigital");
}

Sketch
Code:
#include <Mux.h>

Mux mux;

void setup() {
  // put your setup code here, to run once:
Serial.begin(38400);
}

void loop() {
  mux.pollMuxDigital();
  delay(500);

}

can someone please give me a hint?
 
if you're using a teensy, i'd recommend putting a delahy at the begin of your sketch, so your pc has time to set the device up before the serial port opens, i had this several times as well.

for example, an arduino uno has a 'startup delay' of about 2000 milliseconds, though i generally go for 500 or so.

maybe that helps?
 
ah, i think i see your problem (it's late here :p)

you're making the object with the initializer and are printing a serial message at it.

that's not the right way to do it. (initializers can only set/init variables as far as i know)

i would do it like this:

Code:
/*
  Mux.cpp 
*/

#include "Arduino.h"
#include "Mux.h"

Mux::Mux() {
//does nothing
}


//added this function
void Mux::begin() {
  Serial.println("MUX INIT"); 
}

void Mux::pollMuxDigital() {
    Serial.println("pollMuxDigital");
}

and in your setup after serial.begin there needsto be the mux.begin();
 
Last edited:
you need to declare the mux::begin void in mux.h as well ;)

i just was too quick with posting my answer and am too tired right now to help further, so night.
:)
 
I've seen this alluded to - to summarize:
> during the constructor anything but 'internal housekeeping of class data/setup' - like a .print - needs to be avoided as the natural "C" events precede the Teensy actually being online and ready to run.

A good way to have the Teensy wait 'only as long as needed' to get Serial/USB online is something like this that will give you 4 seconds for SerMon to catch the output: while ( !Serial && (millis()<4000) )

*Note: TYQT will reliably catch it and be online in under 800ms [ just over 400 with a hack I made to init ] without having to click or (re)open the port like the IDE seems to confusingly do on Windows.

MichaelM posted this link that was good for not using delay() :: learn.adafruit.com __ multi-tasking-the-arduino-part-1/all-together-now?view=all#all-together-now

It then creates two classes - except it leaves all in the same sketch.ino file rather than exporting to a header file. In the linked example they do .attach rather than .begin and neither on the simpler LED flasher.

I added this tutorial link to the WIKI_COMING list - you have the class working now - but it might help to get you away from using delay().
 
hi,
thanks for the advice :) .. yes, delay() is also no option for me, as I need everything running all the time ..

but
during the constructor anything but 'internal housekeeping of class data/setup' - like a .print - needs to be avoided as the natural "C" events precede the Teensy actually being online and ready to run.

constructor ok.. but I just did
Code:
void setup() {
  Serial.begin(38400);
  Serial.println("HELLO IM THE INIT");

}

void loop() {
  
}

and there is nothing in the serial-monitor.. so is that rule for the setup(), too? (I mean, ste setup() is a kind of constructor, too.. maybe? :D)
 
hi,
thanks for the advice :) ..

constructor ok.. but I just did
Code:
void setup() {
  Serial.begin(38400);
  Serial.println("HELLO IM THE INIT");

}

and there is nothing in the serial-monitor.. so is that rule for the setup(), too? (I mean, ste setup() is a kind of constructor, too.. maybe? :D)

Once setup() is called the Teensy is ready - the rest of the world may not see it yet - like USB needing the while() below. Other devices may not be ready yet - they may have just seen power in the last half second as well and still be powering up - so they may need a delay() in setup AFTER the Serial.println() is working before a .begin() would work.

The line from post above is needed HERE:
Code:
void setup() {
  Serial.begin(38400);
  [B]while ( !Serial && (millis()<4000) )[/B]
  Serial.println("HELLO IM THE INIT");
}

Once the Teensy is fully configured [ setup() ] entered - the PC will then begin to respond to the USB activity - and after that is complete it can then begin to transmit data - after that while() completes ( as long as it didn't time out for the 4000ms / 4 second wait ).
 
Status
Not open for further replies.
Back
Top