Hey, I am fairly new and trying to combine two pieces of code. One sends photoresistor signals as MIDI format. Another is a wireless transmitter sending photoresistor signal between the NRF24's. I want to send the signal with the NRF24's as a MIDI signal or pick it up as a MIDI signal. I've got this far but struggling to get this to work. Any help is appreciated, thanks in advance.
Photoresistor to MIDI signal code:
int val = 0; //Our initial pot values. We need one for the first value and a second to test if there has been a change made. This needs to be done for all 3 pots.
int lastVal = 0;
void setup()
{
Serial.begin(9600); // Set the speed of the midi port to the same as we will be using in the Hairless Midi software
}
void loop()
{
val = analogRead(0)/8-40; // Divide by 8 to get range of 0-127 for midi
if (val != lastVal) // If the value does not = the last value the following command is made. This is because the pot has been turned. Otherwise the pot remains the same and no midi message is output.
if (val < 0) {
val = 0;
}
{
MIDImessage(176,1,val);} // 176 = CC command (channel 1 control change), 1 = Which Control, val = value read from Potentionmeter 1 NOTE THIS SAYS VAL not VA1 (lowercase of course)
lastVal = val;
delay(10); //here we add a short delay to help prevent slight fluctuations, knocks on the pots etc. Adding this helped to prevent my pots from jumpin up or down a value when slightly touched or knocked.
}
void MIDImessage(byte command, byte data1, byte data2) //pass values out through standard Midi Command
{
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}
Wireless Transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
#define Photoresistor A0
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
byte data[6]; // depending on the number of sensors used
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(Photoresistor,INPUT);
}
void loop()
{
data[0] = map(analogRead(Photoresistor), 0, 1023, 0, 255);
radio.write( data, sizeof(data) );
}
Wireless Reciever:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
int fValue[2];
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
byte data[6]; // depending on the number of sensors used
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
void setup()
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
resetData();
}
void loop()
{
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read( fValue, sizeof(fValue) );
lastReceiveTime = millis(); // At this moment we have received the data
Serial.println(data[0]);
delay(250);
}
}
else
{
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
}
}
}
void resetData()
{
// we are going to place our default code over here.
Serial.println("Connection Lost");
}