Go Down

Topic: photocells and shift registers (Read 930 times) previous topic - next topic



I'm trying to write a program that will enable users to shift bits through a register by moving their hands in front of a photocell. I want to make it so that the closer the user moves his hands towards the photocell, the more LEDs light up. The shift register is correctly wired and it work fine. This is the code I have so far:

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int data = 3;
const int clock = 2;

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup()
  pinMode(clock, OUTPUT); // make the clock pin an output
  pinMode(data , OUTPUT); // make the data pin an output

void loop(){
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(data, outputValue);   

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print("\t output = ");     

  shiftOut2(data, clock, LSBFIRST, B10101010); // send this binary value to the shift register
} // empty loop for now

void shiftOut2(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val)
        int i;

        for (i = 0; i < 8; i++)  {
                if (bitOrder == LSBFIRST){
                        digitalWrite(dataPin, !!(val & (1 << i)));
                else {                       
                        digitalWrite(dataPin, !!(val & (1 << (7 - i))));
                digitalWrite(clockPin, HIGH);
                digitalWrite(clockPin, LOW);


Why'd you write your own shift out function?

What do 2 !!s do here?
digitalWrite(dataPin, !!(val & (1 << i)));

and why the 500ms delays?
That's going to be a sloooow shift out.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131