Hi all!
I want to use a reel to reel machine in a sound installation and I need it to be autonomous meaning that it should never run out of tape in the beginning and the end. At the moment I can remote control the machine with MIDI to an Arduino and relay card I built.
I recently saw this video where an IR sensor is used to count rotations Watch Video . I put the code into an Arduino and was able to get that to work.
The next step is to combine the two codes into one which is where I need some help.
I have made a list of events that need to happen but don't know the way to implement the different parts of it together.
Here are my requirements so far:
Global:
-
Predetermined counter number for beginning (0) and end (100).
-
Command to ignore End number when counter is going backwards. (So the machine will not stop at the end counter number if rewinding)
-
Whenever REW command is activated, counter changes to count backwards.
-
Whenever PLAY, REC, FF commands are activated counter counts forwards.
When end number is reached:
All incoming MIDI commands are ignored.
STOP command is sent.
REW command is sent.
When beginning number is reached:
Stop command is sent.
FF command is sent (Machine should stop again as it reaches 0)
MIDI commands can now control Arduino.
Below is my code so far. I have basically meshed the two codes but the additional functions still need to be implemented. Any help or advice on any part of this code would be great. Thanks!
#include <MIDI.h> // Add Midi Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define labels for the 12 pins
#define REW 2
#define FF 3
#define PLAY 4
#define STOP 5
#define REC 6
//LCD
long x = 0;
int input = A0;
int state = 0;
LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();
//Incoming midi messages to be output pin 2-6 when receiving noteON message with note = 60-65
// Buttons to output notes from Midi Out to be recorded in DAW sequencer.
// Midi Note 60 (Rewind), 61 (FF), 62 (Play), 63 (Stop), 64 (Rec)
void setup() {
pinMode (REW, OUTPUT);
pinMode (FF, OUTPUT);
pinMode (PLAY, OUTPUT);
pinMode (STOP, OUTPUT);
pinMode (REC, OUTPUT);
MIDI.begin(13); // Initialize the Midi Library. Respond to notes on channel 13 only.
MIDI.setHandleNoteOn(Press);
MIDI.setHandleNoteOff(Release);
//LCD
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Turn counter ");
lcd.setCursor(0, 1);
lcd.print(" =turn ");
}
void loop() { // Main loop
MIDI.read(); // Continuously check if Midi data has been received
int counter = digitalRead(A0); //Counter
if (state == 0)
{
switch (counter) {
case 1 : state = 1; lcd.setCursor (0, 1); x = x + 1; lcd.print(x); break;
case 0 : state = 0; break;
}
}
if (counter == LOW) {
state = 0;
}
}
// Press is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void Press (byte channel, byte pitch, byte velocity) {
if (pitch == 60 ){
digitalWrite(REW,HIGH); // Send on pin 2
}
if (pitch == 61){
digitalWrite(FF,HIGH); // Send on pin 3
}
if (pitch == 62){
digitalWrite(PLAY,HIGH);// Send on pin 4
}
if (pitch == 63){
digitalWrite(STOP,HIGH); // Send on pin 5
}
if (pitch == 64){
digitalWrite(REC,HIGH);
digitalWrite(PLAY,HIGH);// Send on pin 4, 6
}
}
// Release is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void Release (byte channel, byte pitch, byte velocity){
if (pitch== 60){
digitalWrite(REW,LOW); // Stop send on pin 2
}
if (pitch== 61){
digitalWrite(FF,LOW); // Stop send on pin 3
}
if (pitch == 62){
digitalWrite(PLAY,LOW);// Stop send on pin 4
}
if (pitch == 63){
digitalWrite(STOP,LOW); // Stop send on pin 5
}
if (pitch == 64){
digitalWrite(REC,LOW);
digitalWrite(PLAY,LOW);// Stop send on pin 4 AND 6
}
}