Guys no need to get angry over this
Grumpy Mike, I'm having a bit of trouble with my previous code and the new one. Basically I'm trying to call a specific function using MIDI.
What's the best way to deal with it, knowing the fact that I have about 30 functions.
- One different note by function? E.g note 60 for function 1, note 48 for function 2 and so forth?
- The same note but with different velocity?
I'd go for option 1 which is more convenient and more distinctive, can you confirm?
Here's the code that is successfully moving one or two servos if I type note 60, the angle change with the velocity. (Ignore the solenoid part)
#include <VarSpeedServo.h>
VarSpeedServo myServo[2];
int servoPin[]={7,8};
int sol1Pin = 9;
byte incomingByte;
byte note;
byte velocity;
int chan = 1; //specify what MIDI channel we're listing to
int action=2; //0 =note off ; 1=note on ; 2= null
const int lowNote = 60; //what's the first note? 36 is C1 in Ableton
//setup: declaring inputs and outputs and begin serial
void setup() {
Serial.begin(38400);
//setup our outputs
for(int i= 0; i<2; i++){
myServo[i].attach(servoPin[i]);
myServo[i].write(90);
}
pinMode(sol1Pin, OUTPUT);
}
//loop: wait for serial data, and interpret the message
void loop () {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// wait for as status-byte, channel 1, note on or off
if (incomingByte== 143+chan){ // note on message starting starting
action=1;
}else if (incomingByte== 127+chan){ // note off message starting
action=0;
}else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
note=incomingByte;
triggerSolenoid(note, velocity);
note=0;
velocity=0;
action=2;
}else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
note=incomingByte;
}else if ( (action==1)&&(note!=0) ){ // ...and then the velocity
velocity=incomingByte;
triggerSolenoid(note, velocity);
moveServo(note, velocity);
note=0;
velocity=0;
action=0;
}else{
//nada
}
}
}
void moveServo(byte note, byte velocity){
//write an angle based on velocity to our servo
switch(note){
case lowNote: //if it's note 36
{int angle1 = int(map(velocity, 0,127, 0,180)); //map velocity to angle
myServo[0].write(angle1); //write our angle to the servo
break;
}
case lowNote+1:{ //if it's note 37
int angle2 = int(map(velocity, 0,127, 0,180)); //map velocity to angle
myServo[1].write(angle2); //write our angle to the servo
break;
}
}
}
void triggerSolenoid(byte note, byte velocity){
if(note == lowNote+2){ //if it's note 38
int value=LOW;
if (velocity >10){ //if the velocity is over 10 turn the solenoid on
value=HIGH;
}else{ //other wise turn it off
value=LOW;
}
digitalWrite(sol1Pin, value); //write our value to the solenoid
}
}
Right now, by typing note 60, the servo in pin 7 is moving. If I change the velocity it changes the angle. GREAT.
However I'm gonna try to mix this piece of code with my old one.
Here's a piece of the beginning of my actual piece of code controlling 8 servos and 1 solenoid with a PS2 remote controller.
#include <VarSpeedServo.h>
#include <servo.h>
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
byte boucle= 0; // for boucle
VarSpeedServo servoI1; // create servo object to control a servo that inflate 1
VarSpeedServo servoD1; // create servo object to control a servo that deflate 1
VarSpeedServo servoI2; // create servo object to control a servo that Inflate 2
VarSpeedServo servoD2; // create servo object to control a servo that Deflate 2
VarSpeedServo servoI3; // create servo object to control a servo that Inflate 3
VarSpeedServo servoD3; // create servo object to control a servo that Deflate 3
VarSpeedServo servoI4; // create servo object to control a servo that Inflate 4
VarSpeedServo servoD4; // create servo object to control a servo that Deflate 4
int Rollfast = 0; //Value read off the PS2 Right Stick up/down.
int Pitchfast = 0; //Value read off the PS2 Right Stick left/right
int Rollslow = 0; //Value read off the PS2 Left Stick up/down
int Pitchslow = 0; // Value read off the PS2 Left Stick left/right
// the loop function runs over and over again forever
void setup(){
Serial.begin(57600);
// initialize digital pin 13 as an output.
pinMode(31, OUTPUT);
digitalWrite (31, LOW);
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
error = ps2x.config_gamepad(13,11,10,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
servoI1.attach(22); // Set I1 servo to digital pin 22
servoD1.attach(23); // Set D1 servo to digital pin 23
servoI2.attach(24); // Set I2 servo to digital pin 24
servoD2.attach(25); // Set D2 servo to digital pin 25
servoI3.attach(26); // Set I1 servo to digital pin 26
servoD3.attach(27); // Set D2 servo to digital pin 27
servoI4.attach(28); // Set I2 servo to digital pin 28
servoD4.attach(29); // Set I2 servo to digital pin 29
}
void infslow1() { // Monte 1 lentement
servoI1.write(45, 5, true);
servoI1.write(32, 0, true);
}
void infslow2() { // Monte 2 lentement
servoI2.write(45, 5, true);
servoI2.write(32, 0, true);
}
void infslow3() { // Monte 3 lentement
servoI3.write(45, 5, true);
servoI3.write(32, 0, true);
}
void infslow4() { // Monte 4 lentement
servoI4.write(45, 5, true);
servoI4.write(32, 0, true);
}
void infslow12() { // Monte 1 et 2 lentement
servoI1.write(45, 5);
servoI2.write(45, 5, true);
servoI1.write(32, 0);
servoI2.write(32, 0, true);
}
void infslow43() { // Monte 4 et 3 lentement
servoI4.write(45, 5);
servoI3.write(45, 5, true);
servoI4.write(32, 0);
servoI3.write(32, 0, true);
}
void infslow14() { // Monte 1 et 4 lentement
servoI1.write(45, 5);
servoI4.write(45, 5, true);
servoI1.write(32, 0);
servoI4.write(32, 0, true);
}
void infslow23() { // Monte 2 et 3 lentement
servoI2.write(45, 5);
servoI3.write(45, 5, true);
servoI2.write(32, 0);
servoI3.write(32, 0, true);
}
void infslow1234 () { // Monte 1 2 3 4 lentement
servoI1.write(47, 3);
servoI2.write(47, 3);
servoI3.write(47, 3);
servoI4.write(47, 3, true);
delay (4500);
servoI1.write(32, 3);
servoI2.write(32, 3);
servoI3.write(32, 3);
servoI4.write(32, 3, true);
}
void vibrationslow () {
digitalWrite (31, LOW);
servoI1.write(60, 0);
servoI2.write(60, 0);
servoI3.write(60, 0);
servoI4.write(60, 0, true);
servoD1.write(50, 0);
servoD2.write(50, 0);
servoD3.write(50, 0);
servoD4.write(50, 0, true);
delay (100);
for (boucle=0;boucle<10;boucle++)
{
boucle= 2;
digitalWrite (31, HIGH);
delay(100);
digitalWrite (31, LOW);
delay(100);
digitalWrite (31, HIGH);
delay(100);
digitalWrite (31, LOW);
delay(100);
digitalWrite (31, HIGH);
delay(100);
digitalWrite (31, LOW);
delay(100);
ps2x.read_gamepad(); //read controller
if(ps2x.ButtonReleased(PSB_L3))
boucle=11;
}
delay (100);
servoI1.write(32, 0);
servoI2.write(32, 0);
servoI3.write(32, 0);
servoI4.write(32, 0, true);
delay (700);
digitalWrite (31, LOW);
servoD1.write(32, 0);
servoD2.write(32, 0);
servoD3.write(32, 0);
servoD4.write(32, 0, true);
}
//and it goes on and on till the void loop.