-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/*----------------------------------------------------------------------* | ||
* Arduino Button Library v1.0 * | ||
* Jack Christensen May 2011, published Mar 2012 * | ||
* * | ||
* Library for reading momentary contact switches like tactile button * | ||
* switches. Intended for use in state machine constructs. * | ||
* Use the read() function to read all buttons in the main loop, * | ||
* which should execute as fast as possible. * | ||
* * | ||
* This work is licensed under the Creative Commons Attribution- * | ||
* ShareAlike 3.0 Unported License. To view a copy of this license, * | ||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a * | ||
* letter to Creative Commons, 171 Second Street, Suite 300, * | ||
* San Francisco, California, 94105, USA. * | ||
*----------------------------------------------------------------------*/ | ||
|
||
#include "Button.h" | ||
|
||
/*----------------------------------------------------------------------* | ||
* Button(pin, puEnable, invert, dbTime) instantiates a button object. * | ||
* pin Is the Arduino pin the button is connected to. * | ||
* puEnable Enables the AVR internal pullup resistor if != 0 (can also * | ||
* use true or false). * | ||
* invert If invert == 0, interprets a high state as pressed, low as * | ||
* released. If invert != 0, interprets a high state as * | ||
* released, low as pressed (can also use true or false). * | ||
* dbTime Is the debounce time in milliseconds. * | ||
* * | ||
* (Note that invert cannot be implied from puEnable since an external * | ||
* pullup could be used.) * | ||
*----------------------------------------------------------------------*/ | ||
Button::Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime) | ||
{ | ||
_pin = pin; | ||
_puEnable = puEnable; | ||
_invert = invert; | ||
_dbTime = dbTime; | ||
pinMode(_pin, INPUT); | ||
if (_puEnable != 0) | ||
digitalWrite(_pin, HIGH); //enable pullup resistor | ||
_state = digitalRead(_pin); | ||
if (_invert != 0) _state = !_state; | ||
_time = millis(); | ||
_lastState = _state; | ||
_changed = 0; | ||
_lastChange = _time; | ||
} | ||
|
||
/*----------------------------------------------------------------------* | ||
* read() returns the state of the button, 1==pressed, 0==released, * | ||
* does debouncing, captures and maintains times, previous states, etc. * | ||
*----------------------------------------------------------------------*/ | ||
uint8_t Button::read(void) | ||
{ | ||
static uint32_t ms; | ||
static uint8_t pinVal; | ||
|
||
ms = millis(); | ||
pinVal = digitalRead(_pin); | ||
if (_invert != 0) pinVal = !pinVal; | ||
if (ms - _lastChange < _dbTime) { | ||
_time = ms; | ||
_changed = 0; | ||
return _state; | ||
} | ||
else { | ||
_lastState = _state; | ||
_state = pinVal; | ||
_time = ms; | ||
if (_state != _lastState) { | ||
_lastChange = ms; | ||
_changed = 1; | ||
} | ||
else { | ||
_changed = 0; | ||
} | ||
return _state; | ||
} | ||
} | ||
|
||
/*----------------------------------------------------------------------* | ||
* isPressed() and isReleased() check the button state when it was last * | ||
* read, and return false (0) or true (!=0) accordingly. * | ||
* These functions do not cause the button to be read. * | ||
*----------------------------------------------------------------------*/ | ||
uint8_t Button::isPressed(void) | ||
{ | ||
return _state == 0 ? 0 : 1; | ||
} | ||
|
||
uint8_t Button::isReleased(void) | ||
{ | ||
return _state == 0 ? 1 : 0; | ||
} | ||
|
||
/*----------------------------------------------------------------------* | ||
* wasPressed() and wasReleased() check the button state to see if it * | ||
* changed between the last two reads and return false (0) or * | ||
* true (!=0) accordingly. * | ||
* These functions do not cause the button to be read. * | ||
*----------------------------------------------------------------------*/ | ||
uint8_t Button::wasPressed(void) | ||
{ | ||
return _state && _changed; | ||
} | ||
|
||
uint8_t Button::wasReleased(void) | ||
{ | ||
return !_state && _changed; | ||
} | ||
|
||
/*----------------------------------------------------------------------* | ||
* pressedFor(ms) and releasedFor(ms) check to see if the button is * | ||
* pressed (or released), and has been in that state for the specified * | ||
* time in milliseconds. Returns false (0) or true (1) accordingly. * | ||
* These functions do not cause the button to be read. * | ||
*----------------------------------------------------------------------*/ | ||
uint8_t Button::pressedFor(uint32_t ms) | ||
{ | ||
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0; | ||
} | ||
|
||
uint8_t Button::releasedFor(uint32_t ms) | ||
{ | ||
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0; | ||
} | ||
|
||
/*----------------------------------------------------------------------* | ||
* lastChange() returns the time the button last changed state, * | ||
* in milliseconds. * | ||
*----------------------------------------------------------------------*/ | ||
uint32_t Button::lastChange(void) | ||
{ | ||
return _lastChange; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*----------------------------------------------------------------------* | ||
* Arduino Button Library v1.0 * | ||
* Jack Christensen Mar 2012 * | ||
* * | ||
* This work is licensed under the Creative Commons Attribution- * | ||
* ShareAlike 3.0 Unported License. To view a copy of this license, * | ||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a * | ||
* letter to Creative Commons, 171 Second Street, Suite 300, * | ||
* San Francisco, California, 94105, USA. * | ||
*----------------------------------------------------------------------*/ | ||
#ifndef Button_h | ||
#define Button_h | ||
#if ARDUINO >= 100 | ||
#include <Arduino.h> | ||
#else | ||
#include <WProgram.h> | ||
#endif | ||
class Button | ||
{ | ||
public: | ||
Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime); | ||
uint8_t read(); | ||
uint8_t isPressed(); | ||
uint8_t isReleased(); | ||
uint8_t wasPressed(); | ||
uint8_t wasReleased(); | ||
uint8_t pressedFor(uint32_t ms); | ||
uint8_t releasedFor(uint32_t ms); | ||
uint32_t lastChange(); | ||
|
||
private: | ||
uint8_t _pin; //arduino pin number | ||
uint8_t _puEnable; //internal pullup resistor enabled | ||
uint8_t _invert; //if 0, interpret high state as pressed, else interpret low state as pressed | ||
uint8_t _state; //current button state | ||
uint8_t _lastState; //previous button state | ||
uint8_t _changed; //state changed since last read | ||
uint32_t _time; //time of current state (all times are in ms) | ||
uint32_t _lastChange; //time of last state change | ||
uint32_t _dbTime; //debounce time | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/*------------------------- GENERAL INFORMATION ------------------------* | ||
* MidiSensors v1.0 * | ||
* Released 08/08/16 by LeMinaw * | ||
* Arduino Button Library v1.0 is requiered * | ||
*------------------------------- LICENSE ------------------------------* | ||
* This work is licensed under the Creative Commons Attribution * | ||
* NonCommercial 4.0 International License. To view a copy of this * | ||
* license, visit http://creativecommons.org/licenses/by-nc/4.0/. * | ||
*----------------------------------------------------------------------*/ | ||
|
||
/*--------------------------------------------MIDI PROTOCOL REMINDER--------------------------------* | ||
* MESSAGE | DATA1 | DATA2 * | ||
* 1000 cccc = note off => 128(10) | 0xxx xxxx: note pitch | 0xxx xxxx: velocity * | ||
* 1001 cccc = note on => 129(10) | 0xxx xxxx: note pitch | 0xxx xxxx: velocity * | ||
* 1110 cccc = pitch bend => 224(10) | 0000 0000: code | 0xxx xxxx: speed * | ||
* 1011 cccc = control change => 176(10) | 0xxx xxxx: number | 0xxx xxxx: value * | ||
*--------------------------------------------------------------------------------------------------*/ | ||
|
||
#include "Button.h" | ||
|
||
// PINS CONFIGURATION | ||
const int sensPins[4] = {0, 1, 2, 3}; | ||
const int ledActPins[4] = {22, 23, 24, 25}; | ||
const int ledSelPins[4] = {8, 9, 10, 11}; | ||
const int butSelPin = 5; | ||
const int butMinPin = 6; | ||
const int butMaxPin = 7; | ||
// CC CONFIGURATION | ||
const int sensCCs[4] = {75, 76, 77, 78}; | ||
// END CONFIGURATION | ||
|
||
int selectedSens = -1; | ||
int sensValues[4] = {0, 0, 0, 0}; | ||
int sensCalibs[8] = {0, 1023, 0, 1023, 0, 1023, 0, 1023}; | ||
int newValue;0 | ||
|
||
Button butSel(butSelPin, true, true, 10); // Pullup, inversion and 20ms debounce enabled | ||
Button butMin(butMinPin, true, true, 10); | ||
Button butMax(butMaxPin, true, true, 10); | ||
|
||
void setup() { | ||
// Serial.begin(57600); // For serial logs | ||
Serial1.begin(31250); | ||
for (int i; i < 4; i++) pinMode(sensPins[i], INPUT ); | ||
for (int i; i < 2; i++) pinMode(ledSelPins[i], OUTPUT); | ||
for (int i; i < 4; i++) pinMode(ledActPins[i], OUTPUT); | ||
selectSens(); | ||
} | ||
|
||
void loop() { | ||
butSel.read(); | ||
butMin.read(); | ||
butMax.read(); | ||
|
||
// Serial.println(analogRead(sensPins[1])); // For serial logs | ||
|
||
if(butSel.wasReleased()) selectSens(); | ||
if(butMin.wasReleased()) minCalib(selectedSens); | ||
if(butMax.wasReleased()) maxCalib(selectedSens); | ||
|
||
for (int i; i < 4; i++) { | ||
newValue = map(analogRead(sensPins[i]), sensCalibs[i*2], sensCalibs[i*2+1], 0, 127); | ||
if (newValue != sensValues[i]) { | ||
midiTx(176, sensCCs[i], newValue); | ||
sensValues[i] = newValue; | ||
digitalWrite(ledActPins[i], HIGH); | ||
delay(2); | ||
digitalWrite(ledActPins[i], LOW); | ||
} | ||
} | ||
// delay(1); // Can improve stability on some cards | ||
} | ||
|
||
void selectSens() { | ||
selectedSens++; | ||
if (selectedSens > 3) selectedSens = 0; | ||
for (int i; i < 4; i++) digitalWrite(ledSelPins[i], LOW); | ||
digitalWrite(ledSelPins[selectedSens], HIGH); | ||
} | ||
|
||
void minCalib(int sens) { | ||
sensCalibs[sens*2] = analogRead(sensPins[sens]); | ||
} | ||
void maxCalib(int sens) { | ||
sensCalibs[sens*2+1] = analogRead(sensPins[sens]); | ||
} | ||
|
||
void midiTx(unsigned char msg, unsigned char data1, unsigned char data2) { | ||
//Serial.write(msg); // For serial logs | ||
//Serial.write(data1); // For serial logs | ||
//Serial.write(data2); // For serial logs | ||
Serial1.write(msg); | ||
Serial1.write(data1); | ||
Serial1.write(data2); | ||
} | ||
|
||
//EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Button KEYWORD1 | ||
read KEYWORD2 | ||
isPressed KEYWORD2 | ||
isReleased KEYWORD2 | ||
wasPressed KEYWORD2 | ||
wasReleased KEYWORD2 | ||
pressedFor KEYWORD2 | ||
releasedFor KEYWORD2 | ||
lastChange KEYWORD2 |