/* USB MIDI AnalogControlChange Example
You must select MIDI from the "Tools > USB Type" menu
http://www.pjrc.com/teensy/td_midi.html
This example code is in the public domain.
*/
#include <Encoder.h>
#include <Bounce.h>
// the MIDI channel number to send messages
const int channel = 1;
// the MIDI continuous controller for each analog input
Encoder myEnc(0, 1);
const int controller0 = 21; // 10 = pan position
float multi = 4; // 0.5 = 16 steps; 1 = 32 steps; 2 = 64 steps; 4 = 127 steps; 8 = 256 steps (2 steps for one increase)
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
// store previously sent values, to detect change
long previous0 = -1;
long sent0 = -1;
elapsedMillis msec = 0;
void loop() {
long raw = myEnc.read();
if (raw % 4 == 0) {
long n0 = raw/multi;
if (n0 > 127) {
myEnc.write(127*multi);
n0 = 127;
}
if (n0 < 0) {
myEnc.write(0);
n0 = 0;
}
if (n0 != previous0) {
previous0 = n0;
}
// only check the analog inputs 50 times per second,
// to prevent a flood of MIDI messages
if (sent0 != previous0) {
if (msec >= 20) {
msec = 0;
// only transmit MIDI messages if analog input changed
usbMIDI.sendControlChange(controller0, n0, channel);
sent0 = previous0;
}
}
// MIDI Controllers should discard incoming MIDI messages.
// http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
while (usbMIDI.read()) {
// ignore incoming messages
}
}
}
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
#include <Bounce.h>
// the MIDI channel number to send messages
const int channel = 1;
const int controller0 = 21; // 10 = pan position
// Encoder
Encoder myEnc(0, 1);
// store current, previously read values and previously sent values, to detect change
long n0 = 0;
long previous0 = -1;
long sent0 = -1;
// MIDI overflow variable
elapsedMillis msec = 0;
// steps to MIDI value 127
float multi = 4; // 0.5 = 16 steps; 1 = 32 steps; 2 = 64 steps; 4 = 127 steps; 8 = 256 steps (2 steps for one increase)
void interrupt_rutine() {
long raw = myEnc.read();
if (raw % 4 == 0) {
n0 = raw/multi;
if (n0 > 127) {
myEnc.write(127*multi);
n0 = 127;
}
if (n0 < 0) {
myEnc.write(0);
n0 = 0;
}
if (n0 != previous0) {
previous0 = n0;
}
}
}
void setup() {
// to check teensy has power
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// interrupts
attachInterrupt(digitalPinToInterrupt(0), interrupt_rutine, CHANGE);
attachInterrupt(digitalPinToInterrupt(1), interrupt_rutine, CHANGE);
}
void loop() {
// only transmit MIDI messages if rotary encoder input changed
if (sent0 != previous0) {
// only check the analog inputs 50 times per second,
// to prevent a flood of MIDI messages
if (msec >= 20) {
msec = 0;
usbMIDI.sendControlChange(controller0, n0, channel);
sent0 = previous0;
}
}
// MIDI Controllers should discard incoming MIDI messages.
// http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
while (usbMIDI.read()) {
// ignore incoming messages
}
}
if (oneSecondStatusUpdateTimer>1000){
oneSecondStatusUpdateTimer=0;// reset the timer, otherwise will only fire once
Serial.println("encoder values are");
Serial.print(previous0);
Serial.print(" "); //add a space between numbers
Serial.print(previous1);
Serial.println(); //put a line feed at the end of encoder values
}
if (oneSecondStatusUpdateTimer>1000){
oneSecondStatusUpdateTimer=0;// reset the timer, otherwise will only fire once
Serial.println("Hello ");
Serial.print("World!");
Serial.print(" "); //add a space between numbers
Serial.print("more Text");
Serial.println(); //put a line feed at the end of encoder values
if (led == 1) {
digitalWrite(13, HIGH);
led = 0;
}
else {
digitalWrite(13, LOW);
led = 1;
}
}
The output only prints "Hello" and nothing else. But the LED is blinking every second. I guess this serial problem is a Linux problem.
(...)
Now everything is clear. Thank you for your help!
By surfing around in these forums, you might discover that there are in fact issues with some Linux distributions and serial communication. It's not the Teensy or your code to blame.