How would I go about passing a void loop analog read value to a library .h file? I've tried it but it only reads the pot position as soon as I plug the arduino in and does not change once booted up.
arduino main sketch file code;
#include "Arduino.h"
#include "common.h"
#include "synth.h"
#include "serial-in.h"
#include "audio-out.h"
void setup() {
Synth<0>::initialize();
SerialIn<0>::open();
AudioOut<0>::open();
}
void loop() {
int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
while (true) {
if (SerialIn<0>::available()) {
uint8_t b = SerialIn<0>::read();
Synth<0>::receive_midi_byte(b);
}
int8_t level = Synth<0>::clock();
AudioOut<0>::write(level);
}
}
.h file code
#pragma once
// #define private public // for tests
#include "common.h"
// associations of units
#define IOsc Osc
#define IFilter Filter
#define IAmp Amp
#define IEG EG
#define IVoice Voice
#define ISynthCore SynthCore
#include "osc.h"
#include "filter.h"
#include "amp.h"
#include "eg.h"
#include "voice.h"
#include "synth-core.h"
template <uint8_t T>
class Synth {
public:
INLINE static void initialize() {
ISynthCore<0>::initialize();
int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
// Preset
ISynthCore<0>::control_change(OSC_MODE , 0 );
ISynthCore<0>::control_change(OSC_COLOR , val);
ISynthCore<0>::control_change(MOD_RATE , 8 );
ISynthCore<0>::control_change(MOD_DEPTH , val1);
ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2);
ISynthCore<0>::control_change(LPF_RESONANCE , val3);
ISynthCore<0>::control_change(ENV_A , val4 );
ISynthCore<0>::control_change(ENV_D_R , 1 );
}
INLINE static void receive_midi_byte(uint8_t b) {
ISynthCore<0>::receive_midi_byte(b);
}
INLINE static int8_t clock() {
return ISynthCore<0>::clock();
}
};
2 Answers 2
First of all, the while(true)
loop in the loop
function is wrong. You will need to put in the loop
only the code which needs to be repeatedly called. So
void loop() {
if (SerialIn<0>::available()) {
uint8_t b = SerialIn<0>::read();
Synth<0>::receive_midi_byte(b);
}
int8_t level = Synth<0>::clock();
AudioOut<0>::write(level);
}
Then, I haven't understood where the analog read values are used. Moreover the fact that in the initialization you read the analog values and in the loop you expect them to be passed is inconsistent. From what I read, the values should be passed to control_change
; you can do one of these things, in my opinion:
1) manage the analog reading from the main:
void setup() {
Synth<0>::initialize(analogRead(A0), analogRead(A1), analogRead(A2), analogRead(A3), analogRead(A4));
SerialIn<0>::open();
AudioOut<0>::open();
}
void loop() {
Synth<0>::change_OSC_COLOR(analogRead(A0));
Synth<0>::change_MOD_DEPTH(analogRead(A1));
...
if (SerialIn<0>::available()) {
uint8_t b = SerialIn<0>::read();
Synth<0>::receive_midi_byte(b);
}
int8_t level = Synth<0>::clock();
AudioOut<0>::write(level);
}
// in h file
INLINE static void initialize(int val, int val1, int val2, int val3, int val4) {
ISynthCore<0>::initialize();
change_OSC_MODE(0);
change_OSC_COLOR(val);
change_MOD_RATE(8);
change_MOD_DEPTH(val1);
change_LPF_CUTOFF_ENV(val2);
change_LPF_RESONANCE(val3);
change_ENV_A(val4);
change_ENV_D_R(1);
}
INLINE static void change_OSC_MODE(int value) {
ISynthCore<0>::control_change(OSC_MODE, value);
}
INLINE static void change_OSC_COLOR(int value) {
ISynthCore<0>::control_change(OSC_COLOR, value);
}
INLINE static void change_MOD_RATE(int value) {
ISynthCore<0>::control_change(MOD_RATE, value);
}
...
or, alternatively,
...
void loop() {
Synth<0>::change_param(OSC_COLOR, analogRead(A0));
Synth<0>::change_param(MOD_DEPTH, analogRead(A1));
...
}
// in h file
INLINE static void initialize(int val, int val1, int val2, int val3, int val4) {
ISynthCore<0>::initialize();
change_param(OSC_MODE, 0);
change_param(OSC_COLOR, val);
...
}
INLINE static void change_param(uint8_t param, int value) {
ISynthCore<0>::control_change(param, value);
}
(I prefer the firsti since you can prevent the user from using some parameters)
2) manage the analog reading from the library:
void setup() {
Synth<0>::initialize();
SerialIn<0>::open();
AudioOut<0>::open();
}
void loop() {
Synth<0>::updateAnalog();
if (SerialIn<0>::available()) {
uint8_t b = SerialIn<0>::read();
Synth<0>::receive_midi_byte(b);
}
int8_t level = Synth<0>::clock();
AudioOut<0>::write(level);
}
// in h file
INLINE static void initialize() {
ISynthCore<0>::initialize();
updateAnalog();
}
INLINE static void updateAnalog() {
int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
ISynthCore<0>::control_change(OSC_MODE , 0 );
ISynthCore<0>::control_change(OSC_COLOR , val);
ISynthCore<0>::control_change(MOD_RATE , 8 );
ISynthCore<0>::control_change(MOD_DEPTH , val1);
ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2);
ISynthCore<0>::control_change(LPF_RESONANCE , val3);
ISynthCore<0>::control_change(ENV_A , val4 );
ISynthCore<0>::control_change(ENV_D_R , 1 );
}
Each method has advantages or disadvantages... You should pick the one more suitable for you
-
On the first method I get this error: Expected primary-expression before 'int' ISynthCore<0>::initialize(int val, int val1, int val2, int val3, int val4);Casper Round– Casper Round2017年05月23日 13:21:43 +00:00Commented May 23, 2017 at 13:21
-
Sorry, that was a typo. The list of the parameters should have gone in the other
initialize
line... Now I should have fixed it (int val, int val1, int val2, int val3, int val4
goes in the line above the one where you saw it)frarugi87– frarugi872017年05月23日 13:28:06 +00:00Commented May 23, 2017 at 13:28 -
Tried that, another error again I'm afraid. Changed the code as you said 'change_MOD_DEPTH' is not a member of 'Synth<0u>'Casper Round– Casper Round2017年05月23日 13:39:32 +00:00Commented May 23, 2017 at 13:39
-
@CasperRound the
...
at the end means "do this for all the other members you need", so just add the remaining ones ;)frarugi87– frarugi872017年05月23日 13:56:29 +00:00Commented May 23, 2017 at 13:56 -
I've already done that. HahaCasper Round– Casper Round2017年05月23日 13:59:37 +00:00Commented May 23, 2017 at 13:59
How would I go about passing a void loop analog read value to a library .h file?
What you wanted to do makes little sense. But it is certainly doable. The simplest would be to use an "extern" variable as a bridge.
val
,val1
etc: one in yourSynth
class, one in theloop()
. They are different, separate. Writing to one will not change the other. Both sets are changed only at startup - firstsetup()
callsSynth::initialize()
which does a load ofanalogRead
calls and sets itsval
variables. Then inloop()
you do moreanalogRead
calls, and set the localval
variables.Synth
only does anything with those values when you callinitialize()
. Does that help?