Splitting IR Break Beams into four different signals

Hey,

I have an array of four IR Break Beams that work perfectly in Arduino. I have the serial data going into a Max patch to trigger random audio samples. Currently in the Arduino serial monitor all of the IR Break Beams go down in one column instead of four. I have different numbers being output by each break beam to tell them apart. Does any one know how to split the different break beams into there own column? I tried doing this in max with the splice argument but it didnt work, I've been trying in arduino too but I cant figure it out either.

Any help would be great,
Thanks.

heres my code.

#define LEDPIN 13

#define SENSORPIN_A 4
#define SENSORPIN_B 5
#define SENSORPIN_C 6
#define SENSORPIN_D 7


// variables will change:
int sensorState_A = 0, lastState_A = 0;
int sensorState_B = 0, lastState_B = 0;
int sensorState_C = 0, lastState_C = 0;
int sensorState_D = 0, lastState_D = 0;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  // initialize the sensor pins as an input and enable internal pullup:
  pinMode(SENSORPIN_A, INPUT_PULLUP);
  pinMode(SENSORPIN_B, INPUT_PULLUP);
  pinMode(SENSORPIN_C, INPUT_PULLUP);
  pinMode(SENSORPIN_D, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop()
{
  // read the state of the sensor:
  sensorState_A = digitalRead(SENSORPIN_A);
  sensorState_B = digitalRead(SENSORPIN_B);
  sensorState_C = digitalRead(SENSORPIN_C);
  sensorState_D = digitalRead(SENSORPIN_D);
  

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState_A == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }
  if (sensorState_A && !lastState_A)
  {
    Serial.println("1");
  }
  if (!sensorState_A && lastState_A)
  {
    Serial.println("0");
  }
  lastState_A = sensorState_A;

  if (sensorState_B == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_B && !lastState_B)
  {
    Serial.println("3");
  }
  if (!sensorState_B && lastState_B)
  {
    Serial.println("2");
  }
  lastState_B = sensorState_B;

  if (sensorState_C == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_C && !lastState_C)
  {
    Serial.println("5");
  }
  if (!sensorState_C && lastState_C)
  {
    Serial.println("4");
  }
  lastState_C = sensorState_C;

if (sensorState_D == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_D && !lastState_D)
  {
    Serial.println("7");
  }
  if (!sensorState_D && lastState_D)
  {
    Serial.println("6");
  }
  lastState_D = sensorState_D;

  
  delay(250); // so we don't flood the serial port
}

Assuming you want a comma between each column and you want a space where the value didn't change, this code will do that:

#define LEDPIN 13

#define SENSORPIN_A 4
#define SENSORPIN_B 5
#define SENSORPIN_C 6
#define SENSORPIN_D 7

#define SEPARATOR ","


// variables will change:
int sensorState_A = 0, lastState_A = 0;
int sensorState_B = 0, lastState_B = 0;
int sensorState_C = 0, lastState_C = 0;
int sensorState_D = 0, lastState_D = 0;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  // initialize the sensor pins as an input and enable internal pullup:
  pinMode(SENSORPIN_A, INPUT_PULLUP);
  pinMode(SENSORPIN_B, INPUT_PULLUP);
  pinMode(SENSORPIN_C, INPUT_PULLUP);
  pinMode(SENSORPIN_D, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop()
{
  // read the state of the sensor:
  sensorState_A = digitalRead(SENSORPIN_A);
  sensorState_B = digitalRead(SENSORPIN_B);
  sensorState_C = digitalRead(SENSORPIN_C);
  sensorState_D = digitalRead(SENSORPIN_D);
  

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState_A == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }
  if (sensorState_A && !lastState_A)
  {
    Serial.print("1");
  } 
  else if (!sensorState_A && lastState_A)
  {
    Serial.print("0");
  }
  else 
  {
    Serial.print(" ");
  }
  Serial.print(SEPARATOR);
  lastState_A = sensorState_A;

  if (sensorState_B == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_B && !lastState_B)
  {
    Serial.print("3");
  }
  else if (!sensorState_B && lastState_B)
  {
    Serial.print("2");
  }
  else 
  {
    Serial.print(" ");
  }
  Serial.print(SEPARATOR);

  lastState_B = sensorState_B;

  if (sensorState_C == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_C && !lastState_C)
  {
    Serial.print("5");
  }
  else if (!sensorState_C && lastState_C)
  {
    Serial.print("4");
  }
  else 
  {
    Serial.print(" ");
  }
  Serial.print(SEPARATOR);
  lastState_C = sensorState_C;

if (sensorState_D == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_D && !lastState_D)
  {
    Serial.print("7");
  }
  else if (!sensorState_D && lastState_D)
  {
    Serial.print("6");
  }
  else 
  {
    Serial.print(" ");
  }
  lastState_D = sensorState_D;

  Serial.println(); //end of line
  
  delay(250); // so we don't flood the serial port
}

Hey man, thanks for the code it works great in Arduino. However getting in to Max is alot harder, only the first IR sensor triggers signals. I think its just the comma in the serial window. Is there away that I can use the SEPARATOR argument with out the comma. Max can only really read numbers in the serial window, letters and special characters dont work. thanks again fro the code :slight_smile:

I have just deleted the comma out of the code and have now just got numbers in the arduino serial window. However in Max all of the numbers still come out of the first inlet, the other 3 inlets now have numbers which is good but they dont change when I tirp the sensor i think i have 'assigned' to it. it only trips the first one, is that an issue in Max in need to resolve or is that again in Arduino?

Without a lot more detail on the protocol the Max is expecting, I really can't give you any more advice.

I can upload the max patch if you want? I think the zl functions might work with the code you gave me, currently im using a floating number to get the serial data in to max.

heres the max patch

 ----------begin_max5_patcher---------- 3592.3oc6cszjiaaD97r+JXojSojmg.fO8MGuwNGhcNrkqbvkKUPhPZvtjDL Djyty3x6u8fGjTjRhPTRTb0FOqKqQDnIX2en6FciGT+9ata1R1mH7YVeq0uZ c2c+9at6NUQxBtq556lkf+zpXLWQ1rzxjkj7Yy0UslkVjhSHpp9tbJNttlLb wpGooaVjSVUne.dgN26I9Wf+bKGe381ys7AxOghua8aM2XtnAKH4KHo3kwpl 1tpNwCmlFSJTrBnEOvounHDHaUcwOVjSWwhY45Gt88xGq3Sn7SnybKPqG5tz 56NuymsoUvDrxhZt.tsE3qvUr68gUktbSWVPJ9Z1Xuu0gcHepnyMZOu0GsoT yIEOmQzjNa1bqYKwoal0PBu3YMaMqtugFotjs78eS3LYQ+wadi7i4Wec.WTn BPQPmW6+uA5+QmS++FLM8yl5jAHIe54GJ+CxScgykzGeXLeOgmS2jJU+rlQS KFHB..NmCFTv1rQznF.gPspSEJ.sq+brPAPenvvkcGv4H5IDNGuoQ1kJqpxi YrLq+J377KDFbu.r7sUNDbs2ysPG.AZ1zef3zPAImwDjrOK3wAgjviGP40DA kWbi.OvyBdVVVTvRMoPDXKERGmfdrdtDKjSvAIzcpGgTaI34f5wRXrFfbf9T OwATfdmEdQ9n3l2yXIWvMDqLVVYLtfbQvIvUBjA9iniEiP1PgqvwDtvoQrDK v44io1hyWOdcvT3Bd3CUgrGQfpv57FkxI.nUlT1l5PX6Gh.SMDcVilSdIBux Xzb1JKn.ak353V+oY8htRm8f3e3H1EmkSxHoQVLwmmm4fckiCUhpAtSQe8P6 nQS8XRZWCtZUfu5FSB4LhJV70Yw3m+rE7hzpbTNYcCGQmrnimUz1ucJnmacg b7SjnEhxDhyBbgHkWQXb5oO5tFPT.qXNcUQYJU9H9VQHbU7qTNhiYebSLaIN tfjjw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