Browse Source

MIDI-out and MCP23017 sketch

master
Lukas 11 years ago
parent
commit
4fabe518b0
  1. BIN
      design_files/compents.ods
  2. BIN
      design_files/design.odg
  3. 42
      source/library/input_4051/input_4051.cpp
  4. 28
      source/library/input_4051/input_4061.h
  5. 42
      source/sketches/testing/_4051_test.ino
  6. 92
      source/sketches/testing/midi_in_out_softwareserial.ino

BIN
design_files/compents.ods

BIN
design_files/design.odg

42
source/library/input_4051/input_4051.cpp

@ -0,0 +1,42 @@
/*
Morse.cpp - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#include "Arduino.h"
#include "input_4051.h"
const int tolerance = 4;
input_4051::input_4051(int analogPin, int s0Pin, int s1Pin, int s2Pin, void (*valueChangeCallback(int,int)))
{
_analog = analogPin;
_s0 = s0Pin;
_s1 = s1Pin;
_s2 = s2Pin;
pinMode(_analog,INPUT);
pinMode(_s0,OUTPUT);
pinMode(_s1,OUTPUT);
pinMode(_s2,OUTPUT);
_valueChangeCallback = valueChangeCallback;
_value = {-1,-1,-1,-1,-1,-1,-1,-1};
}
void input_4051::loop()
{
for (count=0; count<=7; count++) {
r0 = bitRead(count,0);
r1 = bitRead(count,1);
r2 = bitRead(count,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
int read = analogRead(_analog);
if (value[count] < read - tolerance || value[count] > read + tolerance) {
value[count] = read;
_valueChangeCallback(count, read);
}
}
}

28
source/library/input_4051/input_4061.h

@ -0,0 +1,28 @@
/*
input_4051.h - Library for reading inputs from 4051 multiplexer
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#ifndef input_4051_h
#define input_4051_h
#include "Arduino.h"
class input_4051
{
public:
Morse(int analogPin, int s0Pin, int s1Pin, int s2Pin, void (*valueChangeCallback(int,int)));
void loop(void);
int getSpecificValue(int pin);
private:
int _analog;
int _s0;
int _s1;
int _s2;
void (*_valueChangeCallback(int,int);
int _value[8];
};
#endif

42
source/sketches/testing/_4051_test.ino

@ -0,0 +1,42 @@
int tolerance = 4;
const int s0 = 10;
const int s1 = 9;
const int s2 = 8;
int pots[] = {-1,-1};
void setup() {
Serial.begin(9600);
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
}
void loop() {
//get Y0 of 4051
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s1, LOW);
int read = analogRead(A1);
//only print value on change (tolerance of 4 out of 1024)
if (pots[0] < read - tolerance || pots[0] > read + tolerance) {
pots[0] = read;
Serial.print("pot 0: ");
//print value between 0 & 127 like MIDI velocity ;)
Serial.print(pots[0]/8);
Serial.println();
}
//get Y1 of 4051, analog to Y0
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s1, LOW);
read = analogRead(A1);
if (pots[1] < read - tolerance || pots[1] > read + tolerance) {
pots[1] = read;
Serial.print("pot 1: ");
Serial.print(pots[1]/8);
Serial.println();
}
}

92
source/sketches/testing/midi_in_out_softwareserial.ino

@ -0,0 +1,92 @@
#include <SoftwareSerial.h>
// RX, TX for MIDI out
SoftwareSerial MIDI(10, 11);
//button pin
const int switchPin = 6;
//rotary encoder pins
const int encAPin = 4;
const int encBPin = 5;
//for sending note-off once after button is released
boolean btnOff = false;
//old rotary encoder value
int encA = LOW;
//read rotary encoder value
int enc = HIGH;
void setup()
{
//printing baudrate
Serial.begin(9600);
//MIDI baudrate for software serial (pin 10 & 11)
MIDI.begin(31250);
//button and encoder pins as input
pinMode(switchPin, INPUT);
pinMode(encAPin, INPUT);
pinMode(encBPin, INPUT);
//activate pullup-resistors (read value is inverted, so LOW is active)
digitalWrite(switchPin, HIGH);
digitalWrite(encAPin, HIGH);
digitalWrite(encBPin, HIGH);
}
void loop()
{
//print incoming bytes on softwareserial, just for checking MIDI-in, worked
if (MIDI.available())
Serial.println(MIDI.read());
//check if button is pressed
if (digitalRead(switchPin) == LOW)
{
if (!btnOff) {
//send note on
midiSignal(144,60,100);
btnOff = true;
}
}
if (digitalRead(switchPin) == HIGH)
{
//send note off
if (btnOff) {
midiSignal(128,60,0);
btnOff = false;
}
}
//read encoder pin A
enc = digitalRead(encAPin);
//check if rotary encoder is turned
if ((encA == HIGH) && (enc == LOW)) {
//check direction of turning
if (digitalRead(encBPin) == HIGH) {
//send note on and note off directly, so signal is send on every turn
midiSignal(144,62,100);
midiSignal(128,62,100);
} else {
//other direction, other note value
midiSignal(144,61,100);
midiSignal(128,61,100);
}
}
//save "old" encoder value
encA = enc;
}
//send MIDI signal through softwareserial
void midiSignal(byte b1, byte b2, byte b3) {
//debug printing
Serial.print("send: ");
Serial.print(b1);
Serial.print(" | ");
Serial.print(b2);
Serial.print(" | ");
Serial.print(b3);
Serial.println("");
MIDI.write(b1);
MIDI.write(b2);
MIDI.write(b2);
}
Loading…
Cancel
Save