Go Down

Topic: [Solved] Adjusting shift register outputs to the signal of light barriers (Read 1 time) previous topic - next topic

PeterH


Code: [Select]
LEDpattern = (LEDpattern | maskCup4); // maskCup4 is B11000000

The code above does a bitwise OR to set bits in a variable, where the bits are defined by a bitmask.

The code to clear them is much less obvious to understand, but surprisingly simple:

Code: [Select]
LEDpattern = (LEDpattern & ~maskCup4); // maskCup4 is B11000000

The ~ (tilde) operator gives you the bitwise inverse of the mask ("ones' complement") i.e. it inverts each bit. In this example it would give you B00111111.

The & (bitwise AND) operator then clears the bits which are zero in ~maskCup4. The result in this case is that it clears the top two bits and leaves the other bits unchanged.

Other options open to you are to use the Arduino function bitWrite(), bitSet(), bitClear() which let you read and update individual bits in a value without having to think about bitmasks. In this case where you seem to be dealing with multiple bits at the same time, the bitmasking approach you're using would produce more compact code.
I only provide help via the forum - please do not contact me for private consultancy.

hlawan

Thank you very much :)

The bitClear() funktion was exactly what i needed. Now the program is running fine and everything works.

Heres the code:

Code: [Select]

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


byte LEDpattern1 = B00000000;
byte LEDpattern2 = B00000000;

byte maskCup1 = B00000011;
byte maskCup2 = B00001100;
byte maskCup3 = B00110000;
byte maskCup4 = B11000000;
byte maskCup5 = B00000011;
byte maskCup6 = B00001100;



int sCup1 ;
int sCup2 ;
int sCup3 ;
int sCup4 ;
int sCup5 ;
int sCup6 ;


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 cup 1
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  int sCup1 = analogRead (SensorPin);

  //read out sensor cup 2
  digitalWrite (S0, HIGH);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  int sCup2 = analogRead (SensorPin);

  //read out sensor cup 3
  digitalWrite (S0, LOW);
  digitalWrite (S1, HIGH);
  digitalWrite (S2, LOW);
  int sCup3 = analogRead (SensorPin);

  //read out sensor cup 4
  digitalWrite (S0, HIGH);
  digitalWrite (S1, HIGH);
  digitalWrite (S2, LOW);
  int sCup4 = analogRead (SensorPin);

  //read out sensor cup 5
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, HIGH);
  int sCup5 = analogRead (SensorPin);

  //read out sensor cup 6
  digitalWrite (S0, HIGH);
  digitalWrite (S1, LOW);
  digitalWrite (S2, HIGH);
  int sCup6 = analogRead (SensorPin);



  //LEDs Cup1

  if (sCup1 < 500)
  {
    LEDpattern1 = (LEDpattern1 | maskCup1);
  }

  else
  {
    bitClear(LEDpattern1 , 0);
    bitClear(LEDpattern1 , 1);
  }


  //LEDs Cup2

  if (sCup2 < 500)
  {
    LEDpattern1 = (LEDpattern1 | maskCup2);       
  }

  else
  {
    bitClear(LEDpattern1 , 2);
    bitClear(LEDpattern1 , 3);
  }


  //LEDs Cup3

    if (sCup3 < 500)
  {
    LEDpattern1 = (LEDpattern1 | maskCup3);         
  }

  else
  {
    bitClear(LEDpattern1 , 4);
    bitClear(LEDpattern1 , 5);
  }

  //LEDs Cup4

  if (sCup4 < 500)
  {
    LEDpattern1 = (LEDpattern1 | maskCup4); 
  }   

  else
  {
    bitClear(LEDpattern1 , 6);
    bitClear(LEDpattern1 , 7);
  }

  //LEDs Cup5

  if (sCup5 < 500)
  {
    LEDpattern2 = (LEDpattern2 | maskCup5);         
  }

  else
  {
    bitClear(LEDpattern2 , 0);
    bitClear(LEDpattern2 , 1);
  }

  //LEDs Cup6

  if (sCup6 < 500)
  {
    LEDpattern2 = (LEDpattern2 | maskCup6);       
  }

  else
  {
    bitClear(LEDpattern2 , 2);
    bitClear(LEDpattern2 , 3);
  }       


  ยด//shifting out the data to the shift register

  digitalWrite(latchPin, LOW);           
  shiftOut(dataPin, clockPin, MSBFIRST, LEDpattern2);         
  shiftOut(dataPin, clockPin, MSBFIRST, LEDpattern1);
  digitalWrite(latchPin, HIGH);

}

UKHeliBob

Well done on solving your problem.

Looking at your code I see variables like this
Code: [Select]
byte maskCup1 = B00000011;
byte maskCup2 = B00001100;
byte maskCup3 = B00110000;
byte maskCup4 = B11000000;
byte maskCup5 = B00000011;
byte maskCup6 = B00001100;

and
Code: [Select]
int sCup1 ;
int sCup2 ;
int sCup3 ;
int sCup4 ;
int sCup5 ;
int sCup6 ;

To me that looks like it is a prime candidate for the use of arrays which would mean that you could shorten the code that reads the values  into sCup1, sCup2, sCup3 etc and creates the LEDpattern values.  Changing your code may seem like a bit of a chore but it will pay off when you expand the size of your from 6 holes to 20 holes.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

hlawan

Alright, I didn't have too much time today to read about arrays but I think I understood what they are about.

Here is the new code:

Code: [Select]
//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};




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

  //initialize serial communication
  Serial.begin(9600);       
}

void loop() {

  //read out sensor cup 1
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[0] = analogRead (SensorPin);

  //read out sensor cup 2
  digitalWrite (S0, HIGH);
  digitalWrite (S1, LOW);
  digitalWrite (S2, LOW);
  sCup[1] = analogRead (SensorPin);

  //read out sensor cup 3
  digitalWrite (S0, LOW);
  digitalWrite (S1, HIGH);
  digitalWrite (S2, LOW);
  sCup[2] = analogRead (SensorPin);

  //read out sensor cup 4
  digitalWrite (S0, HIGH);
  digitalWrite (S1, HIGH);
  digitalWrite (S2, LOW);
  sCup[3] = analogRead (SensorPin);

  //read out sensor cup 5
  digitalWrite (S0, LOW);
  digitalWrite (S1, LOW);
  digitalWrite (S2, HIGH);
  sCup[4] = analogRead (SensorPin);

  //read out sensor cup 6
  digitalWrite (S0, HIGH);
  digitalWrite (S1, LOW);
  digitalWrite (S2, HIGH);
  sCup[5] = analogRead (SensorPin);



  //LEDs Cup1
 
  if (sCup[0] < 500)
  {
    LEDpattern[0] = (LEDpattern[0] | maskCup[0]);
  }

  else
  {
    bitClear(LEDpattern[0] , 0);
    bitClear(LEDpattern[0] , 1);
  }


  //LEDs Cup2

  if (sCup[1] < 500)
  {
    LEDpattern[0] = (LEDpattern[0] | maskCup[1]);       
  }

  else
  {
    bitClear(LEDpattern[0] , 2);
    bitClear(LEDpattern[0] , 3);
  }


  //LEDs Cup3

    if (sCup[2] < 500)
  {
    LEDpattern[0] = (LEDpattern[0] | maskCup[2]);         
  }

  else
  {
    bitClear(LEDpattern[0] , 4);
    bitClear(LEDpattern[0] , 5);
  }

  //LEDs Cup4

  if (sCup[3] < 500)
  {
    LEDpattern[0] = (LEDpattern[0] | maskCup[3]); 
  }   

  else
  {
    bitClear(LEDpattern[0] , 6);
    bitClear(LEDpattern[0] , 7);
  }


  //LEDs Cup5

  if (sCup[4] < 500)
  {
    LEDpattern[1] = (LEDpattern[1] | maskCup[4]);         
  }

  else
  {
    bitClear(LEDpattern[1] , 0);
    bitClear(LEDpattern[1] , 1);
  }


  //LEDs Cup6

  if (sCup[5] < 500)
  {
    LEDpattern[1] = (LEDpattern[1] | maskCup[5]);       
  }

  else
  {
    bitClear(LEDpattern[1] , 2);
    bitClear(LEDpattern[1] , 3);
  }       


  //shifting out the data to the shift registers

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


}



Is this what you meant?
Are there more things about the code that work but are not really nice or could be better?

UKHeliBob

Well, you now have some data in arrays but are not really making use of them.  Arrays are particularly useful when you can put code in a for loop and use the variable of the for loop as an index to the data in the array.  It is a bit complicated in your case because the actions you take are different depending on the value of the array index. 

I would consider trying a for loop to go through the cup numbers so that the sCup
  • < 500 comparison is in the for loop and the actions to be taken are dealt with using  switch/case based on the array index. 

    To avoid more duplication of code how about creating a function that does the actions and passing a parameter to it for the LEDpattern to be used and the array index.  The index needed for maskCup[] is already the same as the array index.

    Using arrays and a function will considerably reduce the amount of code you have to enter and will make the program easier to extend.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up