I have a sketch where I have 3 digital pins (lets call them 1,2,3 for simplicity) go hi or low depending on incoming serial data, so what I'd like to do is to have additional pins go high or low for various combinations of the other 3 pins states, so for example if pin1 and pin2 are high then pin 4 goes high, if pin1 and pin3 are high then pin 5 goes high etc. So in effect a logical AND type of thing.
So my question is would it be best to have the additional pins look at the first 3 pins and set their state from there, or would it be better to look at the serial data and base the states on that. As you can probably tell, I'm new to all this so a bit of example code with syntax, rather than technical talk would probably be best, thanks.
I think my answer would depend on whether the serial input provided all the data you need to determine the state of all the outputs, or whether the sketch also had some element of state in it (e.g. that a message received via the serial port caused one output pin to change and the knock-on effects depended on the state of the other pins).
Thanks, I tried it that way but that is giving me a logic OR type output, so if either is true then the pin is going high, what I'm trying to achieve is make the pin go high only if both the other 2 are high.
Ok, I have set up 3 midi notes to make digital pins go high, so now what I am trying to do is have combinations of the same midi notes make other digital pins go high. Here is the full sketch, so far I have only tried (and failed) to get one combination of the 2 notes working as described, individually they all work perfectly.
/*
LED drums - Light up LEDs from drum machine
*/
#include <MIDI.h>
#define kick 10 //set output pins
#define snare 11 // for each
#define hihat 12 // drum sound
#define testAND 9
int trigtime=100; // sets length of trigger experiment between 10 and 200
void setup() {
pinMode (kick, OUTPUT); // pin 10 kick output
pinMode (snare, OUTPUT); // pin 11 snare output
pinMode (hihat, OUTPUT); // pin 12 hihat output
pinMode (testAND, OUTPUT);
digitalWrite(kick,LOW); // initialise pins low
digitalWrite(snare,LOW); // for each
digitalWrite(hihat,LOW); // drum sound
MIDI.begin(10); // set to ch10
MIDI.setHandleNoteOn(trig); // call trig when note on recieved
}
void trig(byte channel, byte pitch, byte velocity) {
if (pitch == 36 && velocity > 0){ // kick trig // was && velocity > 0)
for(int i=0; i<trigtime; i++){
digitalWrite(kick,HIGH);}
digitalWrite(kick,LOW);}
if (pitch == 40 && velocity > 0){ // snare trig
for(int j=0; j<trigtime; j++){
digitalWrite(snare,HIGH);}
digitalWrite(snare,LOW);}
if (pitch == 42 && velocity > 0){ // hihat trig
for(int j=0; j<trigtime; j++){
digitalWrite(hihat,HIGH);}
digitalWrite(hihat,LOW);}
if ( (pitch = 42 && velocity > 0) && (pitch = 40 && velocity > 0) ){ // snare & hat trig NOT WORKING
for(int j=0; j<trigtime; j++){
digitalWrite(testAND,HIGH);}
digitalWrite(testAND,LOW);}
}
void loop() {
MIDI.read();
}
what I am trying to do is have combinations of the same midi notes make other digital pins go high.
Then it is not an AND function you want.
Your logic is screwed and I can't work out under what conditions you want to trigger pin 9.
Can you say in words when this pin should be triggered then maybe we can help.
I would like pin 9 to go high when pins 11 and 12 go high, in this example, but eventually add more pins so say pin 8 would go high when pins 10 and 11 go high. Basically I want 3 midi notes, each with its own associated digital pin that briefly pulses on, and then further digital pins that go high when a combination of the 3 midi notes go high, again just a brief pulse (from a drum machine)
Does that make it clearer what I'm trying to achieve? I appreciate I have totally gone the wrong way about it, hence my asking for help
I could not work out what you were asking so I re-read your posts like 4 times!
I think I understand.. now
Your data which comes in, is serial just imagine you're on the beach and the data represents beach balls, the person can only send 1 ball at a time by throwing it to you, since it is just 1 ball at a time you examin it, it's green! So play a hihat... next ball.. its blue! Play a snare. But you can never get "2 balls" eg if it's blue and green play snare and hi hat.
So simply remove the last if statement (in 1 second a computer could be passed a million balls). Comprende? Basically it's fast enough to make it appear as though 2 sounds were played at the same time, in reality... 1 sound triggers just after the 1st when 2 comnands are issued.
Do you mean that two successive triggerings of the call back function where on one the note is 40 or 42 and the next one where the previous note was 40 and the note is 42 OR the previous one was 42 and the note is 40, should trigger pin 9?
If so that is a lot more complex and involves using a static local variable to record the last note. So at the end of the trigger routine you make the variable lastPitch equal to pitch. Then you can say:-
cjdelphi:
So simply remove the last if statement (in 1 second a computer could be passed a million balls). Comprende? Basically it's fast enough to make it appear as though 2 sounds were played at the same time, in reality... 1 sound triggers just after the 1st when 2 comnands are issued.
OldDumbButKeen:
I would like pin 9 to go high when pins 11 and 12 go high
Ignoring the issue of how / when pins 11 and 12 are set high and low, and assuming you haven't remembered what state they're in and don't find it convenient to update pin 9 when you updated pins 11 and 12, what you're asking for could be implemented like this:
It's not pretty, and relies on the coincidence that HIGH and LOW correspond to boolean true/false values, but within the context of an Arduino sketch it is safe to assume that will always be the case.