Hey everybody, I have the following problem: I'm working on a project where I'm reading potentionmeter values and sending them to Max as OSC messages via USB Serial (SlipSerial encoded). So far so good, the program works as expected when I upload it to my teensy 3.2. However, when the teensy is then disconnected and reconnected (i.e. restarted) the Serial connection doesn't work anymore. There is no output to Max, the Arduino Serial Monitor, or other Serial Logger software. As soon as I reupload the code it works again as expected. It seems, cutting the power somehow breaks something. Any suggestions on what I might be doing wrong?
Code:
#include <Fader.h>
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial(Serial);
const int faderAmount = 5;
const int ledPin = 13;
Fader* fader = new Fader[faderAmount];
void setup() {
SLIPSerial.begin(115200);
pinMode(ledPin, OUTPUT);
fader[0].init(A0, "/fader0");
fader[1].init(A1, "/fader1");
fader[2].init(A2, "/fader2");
fader[3].init(A3, "/fader3");
fader[4].init(A4, "/fader4");
digitalWrite(ledPin, HIGH);
}
void loop() {
for(int i = 0; i < faderAmount; i++)
{
fader[i].handleInput(SLIPSerial);
}
}
Code:
#ifndef Fader_h
#define Fader_h
#include <Arduino.h>
#include <OSCMessage.h>
#include <SLIPEncodedUSBSerial.h>
#include <ResponsiveAnalogRead.h>
class Fader
{
public:
void init(int _inputPin, char* _oscAdress);
void handleInput(SLIPEncodedUSBSerial& SLIPSerial);
private:
int inputPin;
char* oscAdress;
ResponsiveAnalogRead analog {inputPin, true};
};
#endif
Code:
#include <Arduino.h>
#include <Fader.h>
void Fader::init(int _inputPin, char* _oscAdress)
{
pinMode(_inputPin, INPUT);
inputPin = _inputPin;
oscAdress = _oscAdress;
analog.setActivityThreshold(3);
analog.enableEdgeSnap();
analog.update(0);
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}
void Fader::handleInput(SLIPEncodedUSBSerial& SLIPSerial)
{
analog.update();
if(analog.hasChanged())
{
OSCMessage msg(oscAdress);
msg.add(analog.getValue());
SLIPSerial.beginPacket();
msg.send(SLIPSerial);
SLIPSerial.endPacket();
}
else if(analog.getValue() == 1 && analog.isSleeping())
{
OSCMessage msg(oscAdress);
msg.add(0);
SLIPSerial.beginPacket();
msg.send(SLIPSerial);
SLIPSerial.endPacket();
}
}