Pages: [1]   Go Down
Author Topic: photocells and shift registers  (Read 770 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at
Arduino for Teens available at

Pages: [1]   Go Up
Jump to: