[Solved] Adjusting shift register outputs to the signal of light barriers

I got the earlier version, but can see what you have done in the latest one. Actually, using 3 arrays as you have is probably easier to read than using a 2 dimensional one as I suggested.

I take it that you are enjoying yourself !

Of course, your program is actually getting more difficult to understand as it gets more compact. You know what it does now but make sure you know when you come to extend it.

Comparing the latest version with the original will be quite a revelation I am sure.

You are right the code is getting more difficult to understand but I think since we kind of developed it "together" I truly understood every step (but the map() thing :smiley: )
In case I forget something I'll always be able to look it up in this thread :wink:

I included your clearTwoBits function. It will come handy as soon as I build my 20 hole final version.

Works perfectly.

//defining pins

int S0 = 8;
int S1 = 9;
int S2 = 10;

int SensorPin = A0;

int dataPin = 2;        
int latchPin = 3;
int clockPin = 4;


//defining variables

int sCup[6] ;

byte LEDpattern[] = {B00000000, B00000000};

byte maskCup[] = {B00000011, B00001100, B00110000, B11000000, B00000011, B00001100};

int SOvalue[] = {0,1,0,1,0,1};
int S1value[] = {0,0,1,1,0,0};
int S2value[] = {0,0,0,0,1,1};

void setup()
{

  //Configure each IO Pin
  pinMode(S0, OUTPUT);      
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  
  pinMode(SensorPin, INPUT);

  pinMode(dataPin, OUTPUT);       
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

}

void loop() {


  //read out sensor values and store them in sCup[]

  for (int i=0 ; i<6 ; i++)
  {
    digitalWrite (S0, SOvalue[i]);
    digitalWrite (S1, S1value[i]);
    digitalWrite (S2, S2value[i]);
    sCup[i] = analogRead (SensorPin);
  }


  //adjust LED pattern to SensorValues and shift out data
  for (int i = 0 ; i < 6 ; i++)
  {

    int value = (sCup[i] < 500) ? LOW : HIGH;  
    int patternValue = (i < 4) ? LOW : HIGH;

    switch (value) 
    {

    case  0:
      LEDpattern[patternValue] = (LEDpattern[patternValue] | maskCup[i]);
      break;

    case  1:

      switch (i) 
      {
      case 0:
        clearTwoBits(0, 0, 1);
        break;

      case 1:
        clearTwoBits(0, 2, 3);
        break;

      case 2:
        clearTwoBits(0, 4, 5);
        break;

      case 3:
        clearTwoBits(0, 6, 7);
        break;

      case 4:
        clearTwoBits(1, 0, 1);
        break;

      case 5:
        clearTwoBits(1, 2, 3);
        break;
      } 

    }

    digitalWrite(latchPin, LOW);            
    shiftOut(dataPin, clockPin, MSBFIRST, LEDpattern[1]);         
    shiftOut(dataPin, clockPin, MSBFIRST, LEDpattern[0]);
    digitalWrite(latchPin, HIGH); 
  }

}

//defining Function to clear two bits
void clearTwoBits(int patternNumber, int firstBit, int secondBit)
  {
    bitClear(LEDpattern[patternNumber] , firstBit);
    bitClear(LEDpattern[patternNumber] , secondBit);  
  }

As a matter of interest what is the purpose of this 'thing' that you have built ?

Have you got any pictures ?

I knew this question would come up :smiley: Well, I'm building a table...a beer-pong table. Beer pong is a drinking game (throw ping pong balls at cups filled with beer) famous in the USA ( Beer pong - Wikipedia ) and somehow that crossed my way and i decided to build a beer-pong table. I made a 3d model (see attachment) and somehow my plans for the table became more and more complex. At some point I decided that the table needs some kind of intelligence and that's how I got in touch with micro controllers and programming...well and during the project I noticed that I really like spending my time learning the basics of programming...

the white surfaces in the drawing is milky acrylic glass and all the stripes are LED stripes under the acrylic glass.
The holes are of course the holes I was taking about in which the cups are placed and the LEDs we are taking about will be placed under the milky acrylic glass.
The small red pyramids will show which cups are still in the game on the other side.
The blue holes are automatic ping pong ball washers. (If you search for "beer pong table" on youtube you can see how they work)

This may or may not be the cause of your problem but using map() like you are feels wrong somehow

If feels wrong because it is. map is an integer function. It will map the input value to 0 OR 1, not values between 0 and 1. In fact, the only value that will be mapped to 1 is a value at the upper end of the from range (1023). All other values will be mapped to 0.

As you sensed, not a useful thing to do.