Pages: 1 [2]   Go Down
Author Topic: [Solved] Adjusting shift register outputs to the signal of light barriers  (Read 999 times)
0 Members and 1 Guest are viewing this topic.
East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4090
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you please post your while code as it is now as a fresh place to start from ?

You can improve these sections
Code:
  //read out sensor cup 1
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[0] = analogRead (SensorPin);
by putting them in a for loop too and holding the HIGH/LOW values to be used in an array but could I ask that you change the comments about cup numbers
Code:
//read out sensor cup 1
so that the numbers match the corresponding index to sCup[] ?  It will make no difference to operation of the program but may mean that the operation of the for loop and array are more obvious.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright I changed the cup numbers, they started to confuse me as well.


Quote
You can improve these sections
Code:
  //read out sensor cup 1
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[0] = analogRead (SensorPin);
by putting them in a for loop too and holding the HIGH/LOW values to be used in an array

/done smiley

And I was able to fit the  "LEDpattern adjustment and shiftout process" for the two different ShiftRegisters into one for loop using a threshold again.

So this is what the program looks like right now:

Code:

//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:
        bitClear(LEDpattern[0] , 0);
        bitClear(LEDpattern[0] , 1);
        break;

      case 1:
        bitClear(LEDpattern[0] , 2);
        bitClear(LEDpattern[0] , 3);
        break;

      case 2:
        bitClear(LEDpattern[0] , 4);
        bitClear(LEDpattern[0] , 5);
        break;

      case 3:
        bitClear(LEDpattern[0] , 6);
        bitClear(LEDpattern[0] , 7);
        break;

      case 4:
        bitClear(LEDpattern[1] , 0);
        bitClear(LEDpattern[1] , 1);
        break;

      case 5:
        bitClear(LEDpattern[1] , 2);
        bitClear(LEDpattern[1] , 3);
        break;
      }

    }

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

}





what would you say is the next thing I should improve? I think it already looks amazing compared to the code I posted in the very beginning. Again thank you very much for your help. I'm learning quiet a bit step by step with your help.
« Last Edit: February 13, 2013, 01:04:12 pm by hlawan » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4090
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As a first step, how about replacing these
Code:
      case 0:
        bitClear(LEDpattern[0] , 0);
        bitClear(LEDpattern[0] , 1);
        break;
with
Code:
case 0:
  clearTwoBits(0, 0, 1);
  break;
and a new function
Code:
  void clearTwoBits(int patternNumber, int firstBit, int secondBit)
  {
    bitClear(LEDpattern[patternNumber] , firstBit);
    bitClear(LEDpattern[0] , secondBit); 
  }
Not a huge change but another step towards making the program easier to extend.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4090
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

These and the following ones
Code:
  //read out sensor cup 0
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[0] = analogRead (SensorPin);

  //read out sensor cup 1
  digitalWrite (S0, HIGH);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[1] = analogRead (SensorPin);
look like another candidate for a for loop based on the cup number.  Each of them writes a pattern to the same LEDs and reads the corresponding sensor value.  If you put the HIGH/LOW values to be written in an array with the cup number as its row index and the 3 HIGH/LOW values as columns then you could get the values to be written to S0, S1 and S2 from the array row for the cup number and the sCup[] index number will be the for loop variable.

One reason for suggesting changing the cup numbers in the comments was that, as you know, the array index starts at zero not one.  With the comments changed there is less chance of confusion I think.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
These and the following ones
Code:
  //read out sensor cup 0
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[0] = analogRead (SensorPin);

  //read out sensor cup 1
  digitalWrite (S0, HIGH);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[1] = analogRead (SensorPin);
look like another candidate for a for loop based on the cup number.  Each of them writes a pattern to the same LEDs and reads the corresponding sensor value.  If you put the HIGH/LOW values to be written in an array with the cup number as its row index and the 3 HIGH/LOW values as columns then you could get the values to be written to S0, S1 and S2 from the array row for the cup number and the sCup[] index number will be the for loop variable.

One reason for suggesting changing the cup numbers in the comments was that, as you know, the array index starts at zero not one.  With the comments changed there is less chance of confusion I think.

(I think), I did that. I modified my post right after posting it, maybe you still saw the old version?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4090
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin )
In case I forget something I'll always be able to look it up in this thread smiley-wink

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

Works perfectly.

Code:
//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); 
  }
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4090
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Have you got any pictures ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I knew this question would come up smiley-grin 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 ( http://en.wikipedia.org/wiki/Beer_pong ) 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)


* bpt1.8.jpg (300.25 KB, 1676x916 - viewed 14 times.)

* 2.2.jpg (655.87 KB, 1614x916 - viewed 13 times.)

* 2.2b.jpg (851.63 KB, 1614x916 - viewed 13 times.)
« Last Edit: February 13, 2013, 05:24:23 pm by hlawan » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: 1 [2]   Go Up
Jump to: