MIDI problems and optic sensor problem

Hello people!

PROBLEM#1

I’m working on a musical Tesla Coil for fun and I have an issue with my coding. I am trying to catch a midi signal and transfert it into a PWM. For now everything works, but when I connect with teraTerm to make serial connection, I keep on receiving theses symbols, even though nothing is happenning: þøøøøøøøøøøøø…

Here is my code:
#include<MIDI.h>
#include<TimerOne.h>

int nnotes = 0;

bool allume1 = false;
bool allume2 = false;

float frequence1=100.0;
float tt1=0.0;
float df =0.0;
float anal = 1.0;
MIDI_CREATE_DEFAULT_INSTANCE();
void setup() {
pinMode(9, OUTPUT);//SIGNAL OUTPUT
pinMode(A5, INPUT);//POTENTIMETER

Timer1.initialize(2200);
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(31250);// Initialize the Midi Library.
// OMNI sets it to listen to all channels… MIDI.begin(2) would set it
// to respond to notes on channel 2 only.
MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
// tells the Midi Library which function you want to call when a NOTE ON command
MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library
// to call “MyHandleNoteOff” when a NOTE OFF command is received.

}
void loop() {
MIDI.read();

//CHANNEL 1 - 1 note
tt1 = (1.0/frequence1)*1000000;
anal =(analogRead(A5));
df = (anal/1024.0);
if (allume1 == true){
Timer1.setPeriod(tt1);
Timer1.pwm(9,anal);
}

}
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
nnotes++;
allume1 =true;
byte DATA =(pitch);
float allah = (DATA-69.00)/12.00;
double ok = pow(2.00,allah);
frequence1 = ok*440.00;
Serial.println(frequence1);
Serial.println(channel);}

void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
nnotes–;
if (nnotes==0){
allume1 = false;
Timer1.disablePwm(9);
tt1=0;
}
}

PROBLEM #2:

I am using a optical emitter and receiver to transmit data in order not to get interference in my signal. I simply send the PWM signal into the emitter. My PWM signal has a 20% duty-cycle, whatever the frequency of the signal is. The problem is, when I measure the signal at the output of the receiver, its duty-cycle increased to 70% and varies with the frequency I transmit…

I am using a SFH250V optical recevier which is rated for 100Mbps, when I only to to 10kHz signals approximately…

Anyone knows what is going on?

Problem #2 -- your optical system is inverting the PWM signal and so you get the inverse duty cycle. The quick fix is instead of analogWrite(pin,value) use analogWrite(pin, 255-value)

Problem #1 -- is nothing to do with your code it is interference to the hardware. That is why making these things is hard.

Okay, Thank you! but is there a way to prevent the duty cycle from varying with the frequency, giving that it is supposed to be constant. ?

And do you know we can fix the interference problem? I mean, the tesla coil isn't on, I'm really just plugging midi into a midi input into the arduino and I receive these...

Thank You :)

I am using a SFH250V optical recevier which is rated for 100Mbps, when I only to to 10kHz signals approximately...

It can only do 100Mbps if all the support circuitry is correct according to the datasheet. You may have a wiring error.

but is there a way to prevent the duty cycle from varying with the frequency,

When you play about with the prescale counters in order to achieve a change in frequency then in order to get the right frequency you also have to change the count threshold for the PWM. So no if you want to control the frequency then you throw away full control of the duty cycle.

but when I connect with teraTerm to make serial connection, I keep on receiving theses symbols, even though nothing is happenning: þøøøøøøøøøøøø....

Check that the baud rate is correct at both ends. Have you set your terminal to 31250 Baud? A lot of terminals can't.

Thank you all for your replies, I'll meditate on that.

But one last thing: yes my baud rate is 31250 I can control it with TeraTerm. Second last thing: I know that the time of the duty-cycle will vary, but as it is written in the code, it should always be the reading of my analog5pin, which gives a value of 200, which is, with the fonction Timer1.pwm(), giving a a value between 0 and 1023 for the duty cycle. 200 is therefore approx 20% and should remain 20% of the total PWM, no matter what the frequency is?

Hi, You are using an opticfibre connection.

Tom... :)