Logic AND problem help

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 have a sketch

Read Read this before posting a programming question ... - Programming Questions - Arduino Forum then post your code here for further advice.

I have a sketch ...

What sketch?

Please use code tags.

Read this before posting a programming question

How to use this forum

My bad, read that when I joined but forgot.

void trig(byte channel, byte pitch, byte velocity) {
  if (pitch == 36 && velocity > 0){ // kick trig yay working
for(int i=0; i<trigtime; i++){
  digitalWrite(kick,HIGH);}
  digitalWrite(kick,LOW);}
  
  if (pitch == 40 && velocity > 0){ // snare trig yay working
for(int j=0; j<trigtime; j++){
  digitalWrite(snare,HIGH);}
  digitalWrite(snare,LOW);}  
  
  if (pitch == 42 && velocity > 0){ // hihat trig yay working
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 BOO NOT WORKING!!!!
for(int j=0; j<trigtime; j++){
  digitalWrite(testAND,HIGH);}
  digitalWrite(testAND,LOW);} 
}
void loop() { 
  MIDI.read();
if (pitch == 42 && velocity > 0 && pitch == 40 && velocity > 0){

How can that possible work? It can only be true if pitch is equal to 42 and also equal to 40 at the same time. Impossible!

Perhaps you meant this:-

if ( (pitch == 42 && velocity > 0) || (pitch == 40 && velocity > 0) )

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).

Grumpy_Mike:

if (pitch == 42 && velocity > 0 && pitch == 40 && velocity > 0){

How can that possible work? It can only be true if pitch is equal to 42 and also equal to 40 at the same time. Impossible!

Perhaps you meant this:-

if ( (pitch == 42 && velocity > 0) || (pitch == 40 && velocity > 0) )

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.

what I'm trying to achieve is make the pin go high only if both the other 2 are high.

Other 2 what ?

Can you give some example values that should make the pin go high ?

what I'm trying to achieve is make the pin go high only if both the other 2 are high.

What does that have to do with pitch and velocity? Somehow, I think we are not communicating, here.

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();  
}

I'm trying to achieve is make the pin go high only if both the other 2 are high.

In the context of your code that comment makes no sense. As I said before that statement can NEVER be true.

for(int i=0; i<trigtime; i++){
  digitalWrite(kick,HIGH);}

Turning the pin on more than once accomplishes nothing. Trying to hide the { and } by not using spaces just makes your code hard to read. Don't do it;

  digitalWrite(kick,LOW);}

Turning the pin off a few nanoseconds after turning it on seems rather pointless. At least, in terms of having bothered turning it on.

    if ( (pitch = 42 && velocity > 0) && (pitch = 40 && velocity > 0) ){ // snare & hat trig NOT WORKING

pitch STILL can't be 40 and 42 at the same time, so this statement will NEVER be true. And, = is the assignment operator. == is the equality operator.

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 :blush:

I would like pin 9 to go high when pins 11 and 12 go high

But pins 11 and 12 never go high at the same time so what you are asking is impossible.

They do go high at the same time when the midi data (notes 42 and 40) are sent from the drum machine at the same time.

Damn you lol...

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:-

if( (velocity >0) && ( (lastPitch == 40 && pitch ==42) || (lastPitch == 42 && pitch == 40) ) ) { // set pin 9

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.

That is not correct.

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:

digitalWrite(9, digitalRead(11) && digitalRead(12));

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.

OldDumbButKeen:
They do go high at the same time when the midi data (notes 42 and 40) are sent from the drum machine at the same time.

No MIDI is serial, two messages are sent only one after the other NEVER at the same time. It seems you don't understand serial data.