added libraries and clean up

This commit is contained in:
Lukas 2013-02-14 02:59:15 +01:00
parent 52975c9dba
commit d94fed811b
16 changed files with 528 additions and 162 deletions

Binary file not shown.

View File

@ -0,0 +1,28 @@
#include <Wire.h>
#include <INPUT_MCP23017.h>
#include <INPUT_74HC4051.h>
INPUT_MCP23017 input_MCP23017;
INPUT_74HC4051 input_4051;
void setup() {
Serial.begin(9600);
input_MCP23017.begin(0,printValue);
input_4051.begin(A0,11,12,13,printValue);
}
void loop() {
input_MCP23017.loop();
input_4051.loop();
}
void printValue(int id, int pin, int value) {
Serial.print("id: ");
Serial.print(id);
Serial.print(" pin: ");
Serial.print(pin);
Serial.print(" value: ");
Serial.print(value);
Serial.println();
}

View File

@ -0,0 +1,99 @@
#include <Wire.h>
#define MCP23017_ADDRESS 0x20
// registers (from https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h)
#define MCP23017_IODIRA 0x00
#define MCP23017_IPOLA 0x02
#define MCP23017_GPINTENA 0x04
#define MCP23017_DEFVALA 0x06
#define MCP23017_INTCONA 0x08
#define MCP23017_IOCONA 0x0A
#define MCP23017_GPPUA 0x0C
#define MCP23017_INTFA 0x0E
#define MCP23017_INTCAPA 0x10
#define MCP23017_GPIOA 0x12
#define MCP23017_OLATA 0x14
#define MCP23017_IODIRB 0x01
#define MCP23017_IPOLB 0x03
#define MCP23017_GPINTENB 0x05
#define MCP23017_DEFVALB 0x07
#define MCP23017_INTCONB 0x09
#define MCP23017_IOCONB 0x0B
#define MCP23017_GPPUB 0x0D
#define MCP23017_INTFB 0x0F
#define MCP23017_INTCAPB 0x11
#define MCP23017_GPIOB 0x13
#define MCP23017_OLATB 0x15
int _addr = 0;
int _value[16];
void setup() {
Serial.begin(9600);
Wire.begin();
// set defaults!
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write((byte)MCP23017_IODIRA);
Wire.write(0xFF); // all inputs on port A
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_IODIRB);
Wire.write(0xFF); // all inputs on port B
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPPUA);
Wire.write(0xFF); // all pullup resistors on port A
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPPUB);
Wire.write(0xFF); // all pullup resistors on port B
Wire.endTransmission();
}
void loop() {
uint8_t pin,bank;
int value;
//read bank A
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPIOA);
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=0; pin<8; pin++) {
value = (bank >> pin) & 0x1;
if (_value[pin] != value) {
_value[pin] = value;
printValue(_addr,pin,value);
}
}
//read bank B
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPIOB);
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=8; pin<16; pin++) {
value = (bank >> (pin-8)) & 0x1;
if (_value[pin] != value) {
_value[pin] = value;
printValue(_addr,pin,value);
}
}
}
void printValue(int id, int pin, int value) {
Serial.print("id: ");
Serial.print(id);
Serial.print(" pin: ");
Serial.print(pin);
Serial.print(" value: ");
Serial.print(value);
Serial.println();
}

View File

@ -40,3 +40,4 @@ void loop() {
Serial.println();
}
}

View File

@ -0,0 +1,22 @@
#include <INPUT_4051.h>
INPUT_4051 input_4051(A1,10,9,8,printValue);
void setup() {
Serial.begin(9600);
input_4051.setPinCount(2);
}
void loop() {
input_4051.loop();
}
void printValue(int pin, int value) {
Serial.print("pin");
Serial.print(pin);
Serial.print(" : ");
Serial.print(value/8);
Serial.println();
}

View File

@ -0,0 +1,67 @@
#include "Arduino.h"
#include "INPUT_74HC4051.h"
void INPUT_74HC4051::begin(uint8_t analog, uint8_t s0, uint8_t s1, uint8_t s2,CallbackFunction cbF) {
_analog = analog;
_s0 = s0;
_s1 = s1;
_s2 = s2;
pinMode(_analog,INPUT);
pinMode(_s0,OUTPUT);
pinMode(_s1,OUTPUT);
pinMode(_s2,OUTPUT);
_callbackFunction = cbF;
//init start values
uint8_t pin,r0,r1,r2;
for (pin=0; pin<8; pin++) {
r0 = bitRead(pin,0);
r1 = bitRead(pin,1);
r2 = bitRead(pin,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
//delayMicroseconds(10);
_value[pin] = analogRead(_analog);
}
}
void INPUT_74HC4051::loop() {
uint8_t pin,r0,r1,r2;
int value;
for (pin=0; pin<8; pin++) {
r0 = bitRead(pin,0);
r1 = bitRead(pin,1);
r2 = bitRead(pin,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
//delayMicroseconds(10);
value = analogRead(_analog);
if (_value[pin] < value - INPUT_74HC4051_TOLERANCE || _value[pin] > value + INPUT_74HC4051_TOLERANCE) {
_value[pin] = value;
(*_callbackFunction)(_analog,pin,value);
}
}
}
int INPUT_74HC4051::getSpecificValue(uint8_t pin) {
if (pin >= 8)
return -1;
uint8_t r0,r1,r2;
r0 = bitRead(pin,0);
r1 = bitRead(pin,1);
r2 = bitRead(pin,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
//delayMicroseconds(10);
int value = analogRead(_analog);
_value[pin] = value;
return value;
}

View File

@ -0,0 +1,29 @@
/*
INPUT_4051.h - Library for reading inputs from 4051 multiplexer
*/
#ifndef INPUT_74HC4051_h
#define INPUT_74HC4051_h
#define INPUT_74HC4051_TOLERANCE 1
#if !defined(CallbackFunction)
typedef void (*CallbackFunction)(int,int,int);
#endif
class INPUT_74HC4051
{
public:
void begin(uint8_t analog, uint8_t s0, uint8_t s1, uint8_t s2,CallbackFunction cbF);
void loop(void);
int getSpecificValue(uint8_t pin);
private:
uint8_t _analog;
uint8_t _s0;
uint8_t _s1;
uint8_t _s2;
int _value[8];
CallbackFunction _callbackFunction;
};
#endif

View File

@ -0,0 +1,115 @@
#include <Wire.h>
#include "Arduino.h"
#include "INPUT_MCP23017.h"
void INPUT_MCP23017::begin(uint8_t addr,CallbackFunction cbF) {
Wire.begin();
if (addr > 7)
_addr = 7;
else _addr = addr;
_callbackFunction = cbF;
// set defaults!
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write((byte)MCP23017_IODIRA);
Wire.write(0xFF); // all inputs on port A
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_IODIRB);
Wire.write(0xFF); // all inputs on port B
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPPUA);
Wire.write(0xFF); // all pullup resistors on port A
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPPUB);
Wire.write(0xFF); // all pullup resistors on port B
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write((byte)MCP23017_IPOLA);
Wire.write(0xFF); // inverse all inputs
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_IPOLB);
Wire.write(0xFF); // inverse all inputs
Wire.endTransmission();
//init start values
uint8_t pin,bank;
//read bank A
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPIOA);
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=0; pin<8; pin++)
_value[pin] = (bank >> pin) & 0x1;
//read bank B
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPIOB);
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=8; pin<16; pin++)
_value[pin] = (bank >> (pin-8)) & 0x1;
}
void INPUT_MCP23017::loop() {
uint8_t pin,bank;
int value;
//read bank A
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPIOA);
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=0; pin<8; pin++) {
value = (bank >> pin) & 0x1;
if (_value[pin] != value) {
_value[pin] = value;
(*_callbackFunction)(_addr,pin,value);
}
}
//read bank B
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
Wire.write(MCP23017_GPIOB);
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=8; pin<16; pin++) {
value = (bank >> (pin-8)) & 0x1;
if (_value[pin] != value) {
_value[pin] = value;
(*_callbackFunction)(_addr,pin,value);
}
}
}
int INPUT_MCP23017::getSpecificValue(uint8_t pin) {
if (pin > 16)
return LOW;
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
uint8_t p = pin;
if (pin > 8) {
Wire.write(MCP23017_GPIOB);
p -= 8;
} else
Wire.write(MCP23017_GPIOA);
Wire.endTransmission();
uint8_t bank = Wire.read();
int value = (bank >> p) & 0x1;
_value[pin] = value;
return value;
}

View File

@ -0,0 +1,51 @@
/*
INPUT_MCP23017.h - Library for reading inputs from MCP23017 port Expander
*/
#ifndef INPUT_MCP23017_h
#define INPUT_MCP23017_h
#if !defined(CallbackFunction)
typedef void (*CallbackFunction)(int,int,int);
#endif
class INPUT_MCP23017
{
public:
void begin(uint8_t addr,CallbackFunction cbF);
void loop(void);
int getSpecificValue(uint8_t pin);
private:
uint8_t _addr;
int _value[16];
CallbackFunction _callbackFunction;
};
#define MCP23017_ADDRESS 0x20
// registers (from https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h)
#define MCP23017_IODIRA 0x00
#define MCP23017_IPOLA 0x02
#define MCP23017_GPINTENA 0x04
#define MCP23017_DEFVALA 0x06
#define MCP23017_INTCONA 0x08
#define MCP23017_IOCONA 0x0A
#define MCP23017_GPPUA 0x0C
#define MCP23017_INTFA 0x0E
#define MCP23017_INTCAPA 0x10
#define MCP23017_GPIOA 0x12
#define MCP23017_OLATA 0x14
#define MCP23017_IODIRB 0x01
#define MCP23017_IPOLB 0x03
#define MCP23017_GPINTENB 0x05
#define MCP23017_DEFVALB 0x07
#define MCP23017_INTCONB 0x09
#define MCP23017_IOCONB 0x0B
#define MCP23017_GPPUB 0x0D
#define MCP23017_INTFB 0x0F
#define MCP23017_INTCAPB 0x11
#define MCP23017_GPIOB 0x13
#define MCP23017_OLATB 0x15
#endif

View File

@ -1,42 +0,0 @@
/*
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);
}
}
}

View File

@ -1,28 +0,0 @@
/*
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

View File

@ -0,0 +1,62 @@
#include <Wire.h>
#include <SoftwareSerial.h>
#define IOPOLA 0x02
#define GPPUA 0x12
#define BIT_IS_SET(i, bits) (1 << i & bits)
SoftwareSerial MIDI(10,11);
boolean button[8];
void setup()
{
//printing baudrate
Serial.begin(9600);
MIDI.begin(31250);
Wire.begin();
Wire.beginTransmission(0x20);
Wire.write(0x12); // set MCP23017 memory pointer to GPIOB address
Wire.write(0xFF); //PULLUP
Wire.endTransmission();
}
void loop()
{
Wire.beginTransmission(0x20);
Wire.write(0x12); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
int inputs = Wire.read();
delayMicroseconds(10);
//check if button is pressed
int i;
for(i=0;i<8;i++) {
if (BIT_IS_SET(i,inputs)) {
if(!button[i]) {
button[i] = true;
midiSignal(144,60+i,115);
}
} else {
if(button[i]) {
button[i] = false;
midiSignal(128,60+i,0);
}
}
}
}
//send MIDI signal through softwareserial
void midiSignal(byte b1, byte b2, byte b3) {
Serial.print(b1);
Serial.print(" ");
Serial.print(b2);
Serial.print(" ");
Serial.print(b3);
Serial.print(" ");
Serial.println();
MIDI.write(b1);
MIDI.write(b2);
MIDI.write(b2);
}

View File

@ -0,0 +1,45 @@
/*
MIDI note player
This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
If this circuit is connected to a MIDI synth, it will play
the notes F#-0 (0x1E) to F#-5 (0x5A) in sequence.
The circuit:
* digital in 1 connected to MIDI jack pin 5
* MIDI jack pin 2 connected to ground
* MIDI jack pin 4 connected to +5V through 220-ohm resistor
Attach a MIDI cable to the jack, then to a MIDI synth, and play music.
created 13 Jun 2006
modified 13 Aug 2012
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Midi
*/
void setup() {
// Set MIDI baud rate:
Serial.begin(9600);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
pinMode(3, OUTPUT);
}
void loop() {
button();
}
void button() {
int sensorVal = digitalRead(2);
if (sensorVal == HIGH) {
digitalWrite(3, LOW);
}
else {
digitalWrite(3, HIGH);
}
}

View File

@ -0,0 +1,9 @@
void setup() {
Serial.begin(9600);
}
void loop() {
int val = analogRead(5);
Serial.println(val);
delay(100); // we have to make a delay to avoid overloading the serial port
}

View File

@ -1,92 +0,0 @@
#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);
}