Pages: [1]   Go Down
Author Topic: photocells and shift registers  (Read 738 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

Hello,

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
  Serial.begin(9600);
}

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(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);   

 
  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)));
                        delay(500);
                }
                else {                       
                        digitalWrite(dataPin, !!(val & (1 << (7 - i))));
                        delay(500);
                }
                digitalWrite(clockPin, HIGH);
                delay(500);
                digitalWrite(clockPin, LOW);
                delay(500);
        }
}
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27089
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: