Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <util/delay.h>
- #include <avr/io.h>
- //Pin connected to ST_CP of 74HC595
- char latchPin = 8;
- //Pin connected to SH_CP of 74HC595
- char clockPin = 12;
- ////Pin connected to DS of 74HC595
- char dataPin = 11;
- //holders for infromation you're going to pass to shifting function
- char dataRED;
- char dataGREEN;
- char dataArrayRED[11];
- char dataArrayGREEN[11];
- void setup() {
- //set pins to output because they are addressed in the main loop
- DDRD |= _BV(3);
- //Serial.begin(9600);
- //Arduino doesn't seem to have a way to write binary straight into the code
- //so these values are in HEX. Decimal would have been fine, too.
- dataArrayRED[0] = 126; //11111111
- dataArrayRED[1] = 72; //11111110
- dataArrayRED[2] = 61; //11111100
- dataArrayRED[3] = 109; //11111000
- dataArrayRED[4] = 75; //11110000
- dataArrayRED[5] = 103; //11100000
- dataArrayRED[6] = 119; //11000000
- dataArrayRED[7] = 76; //10000000
- dataArrayRED[8] = 127; //00000000
- dataArrayRED[9] = 111; //11100000
- dataArrayRED[10] = 0; //11100000
- //Arduino doesn't seem to have a way to write binary straight into the code
- //so these values are in HEX. Decimal would have been fine, too.
- dataArrayGREEN[0] = 126; //11111111
- dataArrayGREEN[1] = 72; //01111111
- dataArrayGREEN[2] = 61; //00111111
- dataArrayGREEN[3] = 109; //00011111
- dataArrayGREEN[4] = 75; //00001111
- dataArrayGREEN[5] = 103; //00000111
- dataArrayGREEN[6] = 119; //00000011
- dataArrayGREEN[7] = 76; //00000001
- dataArrayGREEN[8] = 127; //00000000
- dataArrayGREEN[9] = 111; //00000111
- dataArrayGREEN[10] = 0; //00000111
- }
- void loop() {
- int reading = 0;
- while (reading < someval){
- reading = analogRead(somepin);
- }
- for (char j = 30; j >= 0; j--) {
- //load the light sequence you want from array
- char ones = j%10;
- char tens = j/10;
- //ground latchPin and hold low for as long as you are transmitting
- digitalWrite(latchPin, 0);
- //move 'em out
- shiftOut(dataArrayGREEN[ones]);
- shiftOut(dataArrayRED[ones]);
- //return the latch pin high to signal chip that it
- //no longer needs to listen for information
- digitalWrite(latchPin, 1);
- _delay_ms(1000);
- }
- // Start tone and flash pins
- while (1){
- tone(10, 14000);
- shiftOut(dataPin, clockPin, dataArrayGREEN[8]);
- shiftOut(dataPin, clockPin, dataArrayRED[8]);
- _delay_ms(500);
- tone(10, 10000);
- shiftOut(dataPin, clockPin, dataArrayGREEN[10]);
- shiftOut(dataPin, clockPin, dataArrayRED[10]);
- _delay_ms(500);
- }
- // the heart of the program
- void shiftOut(char myDataOut) {
- // This shifts 8 bits out MSB first,
- //on the rising edge of the clock,
- //clock idles low
- //internal function setup
- char pinState;
- DDRD |= _BV(2);
- DDRD |= _BV(4);
- //clear everything out just in case to
- //prepare shift register for bit shifting
- PORTD &= ~(_BV(2));
- PORTD &= ~(_BV(4));
- //for each bit in the byte myDataOut?
- //NOTICE THAT WE ARE COUNTING DOWN in our for loop
- //This means that %00000001 or "1" will go through such
- //that it will be pin Q0 that lights.
- for (char i=7; i>=0; i--){
- PORTD &= ~(_BV(4));
- //if the value passed to myDataOut and a bitmask result
- // true then... so if we are at i=6 and our value is
- // %11010100 it would the code compares it to %01000000
- // and proceeds to set pinState to 1.
- if ( myDataOut & (1<<i) ) {
- pinState= 1;
- }
- else {
- pinState= 0;
- }
- //Sets the pin to HIGH or LOW depending on pinState
- digitalWrite(myDataPin, pinState);
- //register shifts bits on upstroke of clock pin
- digitalWrite(myClockPin, 1);
- //zero the data pin after shift to prevent bleed through
- digitalWrite(myDataPin, 0);
- }
- //stop shifting
- digitalWrite(myClockPin, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement