Hi,
I am trying to control a servo using MIDI output from a computer.
Before I got to this part of my project, I tested out the Sweep example in the PWM library, and my servo worked with that.
Then I tried the Knob example in the PWM library, where a potentiomenter controls a servo, and that worked fine.
Then I tried to change the potentiometer data to MIDI input and nothing happened.
If any one wants to look at my code I would really appreciate it. I can't really see anything wrong. The only thing I can think of that might be bad is that there is no MIDI library included, but I did declare the USB type as Serial + MIDI, so hopefully that is how the Teensy knows to look for midi data.
Thank you any suggestions are greatly appreciated.
Nick
I am trying to control a servo using MIDI output from a computer.
Before I got to this part of my project, I tested out the Sweep example in the PWM library, and my servo worked with that.
Then I tried the Knob example in the PWM library, where a potentiomenter controls a servo, and that worked fine.
Then I tried to change the potentiometer data to MIDI input and nothing happened.
If any one wants to look at my code I would really appreciate it. I can't really see anything wrong. The only thing I can think of that might be bad is that there is no MIDI library included, but I did declare the USB type as Serial + MIDI, so hopefully that is how the Teensy knows to look for midi data.
Thank you any suggestions are greatly appreciated.
Nick
Code:
#include <PWMServo.h>
#include <Bounce.h>
const int channel = 5;
const int MIDIchannel = 5; // this will only apply to midi channel 1
const int servo1 = 50; // midi control change 50
int pos1; // position of servo1
PWMServo myservo; // create servo object to control a servo
// variable to read the value from the analog pin
void setup() {
myservo.attach(SERVO_PIN_A); // attaches the servo on pin 9 to the servo object
//myservo.attach(SERVO_PIN_A, 1000, 2000); // some motors need min/max setting
// Serial.begin(115200);
delay(500);
Serial.println("!!! READY !!!");
usbMIDI.setHandleControlChange(myControlChange);
}
void loop() {
}
void myControlChange(byte channel, byte control, byte value) // for LEDs, MIDI output
{
if (channel == MIDIchannel
&& control == servo1);
{
value = map(value, 0, 1023, 0, 179);
value = pos1;
myservo.write(pos1); // sets the servo position according to the scaled value
delay(15);
}
}