Home TUTORIALSArduino Tutorials for Beginners Sinhala Arduino Tutorial 11 | Controlling Thousands of LEDs | Shift Registers | 74HC595

Sinhala Arduino Tutorial 11 | Controlling Thousands of LEDs | Shift Registers | 74HC595

by Dileepa Jayawardena
1 comment 27660 views

Arduino බෝඩ් එකක ඇති සීමිත පින් ප්‍රමානයෙන් ඔබ්බට ගොස් විශාල බාහිර උපකරණ සවි කිරීම පිලිබඳ මෙයින් කතා කිරීමට බලාපොරොත්තු වේ.
Shift Registers කුමක්ද? කොහොමද ඒව වැඩ කරන්නෙ? කියන එක ගැන මේ වීඩියෝවේදී කතා කරනවා.

/*
by Dileepa Jayawardena
myhub.lk
http://myhub.lk/a/Arduino_Tutorials_for_Beginners/Sinhala_Arduino_Tutorial_11_%7C_Controlling_Thousands_of_LEDs_%7C_Shift_Registers_%7C_74HC595_
*/

int SER_Pin = 8;   //pin 14 on the 75HC595
int RCLK_Pin = 9;  //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595

//How many of the shift registers - change this ***
#define number_of_74hc595s 1

//numOfRegisterPins Counting number of pins
#define numOfRegisterPins number_of_74hc595s * 8

boolean registers[numOfRegisterPins];

void setup() {
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);


  //reset all register pins
  clearRegisters();
  writeRegisters();
}


//set all register pins to LOW
void clearRegisters() {
  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    registers[i] = LOW;
  }
}

void writeRegisters() {

  digitalWrite(RCLK_Pin, LOW);

  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    digitalWrite(SRCLK_Pin, LOW);

    int val = registers[i];

    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);

  }
  digitalWrite(RCLK_Pin, HIGH);

}

void setRegisterPin(int index, int value) {
  registers[index] = value;
}


void loop() {
for(int i=0;i< numOfRegisterPins;i++){
  setRegisterPin(i, HIGH);
  writeRegisters();
  setRegisterPin(i, LOW);
  delay(100);
  }

for(int i=numOfRegisterPins-2;i>0;i--){
  setRegisterPin(i, HIGH);
  writeRegisters();
  setRegisterPin(i, LOW);
  delay(100);
  }
  
}

EX1_myhub_lk_74hc595_40LED_Arduino_multi_shift_reg.inoDownload (1.5 KB)

/*
by Dileepa Jayawardena
myhub.lk
http://myhub.lk/a/Arduino_Tutorials_for_Beginners/Sinhala_Arduino_Tutorial_11_%7C_Controlling_Thousands_of_LEDs_%7C_Shift_Registers_%7C_74HC595_
*/

int SER_Pin = 8;   //pin 14 on the 75HC595
int RCLK_Pin = 9;  //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595

//How many of the shift registers - change this ***
#define number_of_74hc595s 5

//numOfRegisterPins Counting number of pins
#define numOfRegisterPins number_of_74hc595s * 8

boolean registers[numOfRegisterPins];

void setup() {
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);


  //reset all register pins
  clearRegisters();
  writeRegisters();
}


//set all register pins to LOW
void clearRegisters() {
  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    registers[i] = LOW;
  }
}

void writeRegisters() {

  digitalWrite(RCLK_Pin, LOW);

  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    digitalWrite(SRCLK_Pin, LOW);

    int val = registers[i];

    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);

  }
  digitalWrite(RCLK_Pin, HIGH);

}

void setRegisterPin(int index, int value) {
  registers[index] = value;
}


void loop() {

  setPattern(4);
  /*
    1-one led left to right right to left
    2-2 Leds runnig two ways
    3-Fill and empty LED list
    4-Fire Fill
  */
}

void setPattern(int pt) {
  if (pt == 1) {
    runLeftRight();
  } else if (pt == 2) {
    runTwoWay();
  } else if (pt == 3) {
    setFillAndEmpty();
  } else if (pt == 4) {
    runFireFill();
  }
}

void runLeftRight() {
  for (int i = 0; i < numOfRegisterPins; i++) {
    setRegisterPin(i, HIGH);
    writeRegisters();
    setRegisterPin(i, LOW);
    delay(5);
  }
  for (int i = numOfRegisterPins - 2; i > 0; i--) {
    setRegisterPin(i, HIGH);
    writeRegisters();
    setRegisterPin(i, LOW);
    delay(5);
  }
}


void runTwoWay() {

  for (int i = 0; i < numOfRegisterPins; i++) {
    setRegisterPin(i, HIGH);//LEft Side LED
    setRegisterPin(numOfRegisterPins - i, HIGH); //Right Side LED
    writeRegisters();
    setRegisterPin(i, LOW);
    setRegisterPin(numOfRegisterPins - i, LOW);
    delay(15);
  }
}

void setFillAndEmpty() {
  for (int i = 0; i < numOfRegisterPins/2; i++) {
    setRegisterPin(i, HIGH);//LEft Side LED
    setRegisterPin(numOfRegisterPins - i, HIGH); //Right Side LED
    writeRegisters();
    delay(15);
  }
  for (int i = 0; i < numOfRegisterPins/2; i++) {
    setRegisterPin(i, LOW);//LEft Side LED
    setRegisterPin(numOfRegisterPins - i,LOW); //Right Side LED
    writeRegisters();
    delay(15);
  }

  for (int i = ( numOfRegisterPins/2)-1; i >-1; i--) {
    setRegisterPin(i, HIGH);//LEft Side LED
    setRegisterPin(numOfRegisterPins - i, HIGH); //Right Side LED
    writeRegisters();
    delay(15);
  }
  for (int i = ( numOfRegisterPins/2)-1; i >-1; i--) {
    setRegisterPin(i, LOW);//LEft Side LED
    setRegisterPin(numOfRegisterPins - i,LOW); //Right Side LED
    writeRegisters();
    delay(15);
  }
  
}

int firecount=0;
void runFireFill(){

  if(firecount==0){
     firecount=numOfRegisterPins;
    }
     firecount--;
  
   for (int i = 0; i < firecount; i++) {
    setRegisterPin(i, HIGH);

     for (int ii = numOfRegisterPins-1; ii > firecount; ii--) {
      setRegisterPin(ii, HIGH);
      }
    
    writeRegisters();
    setRegisterPin(i, LOW);
    
    delay(20);
  }
 
  }
EX2_myhub_lk_74hc595_40LED_Arduino_multi_shift_reg_with_4pattern.inoDownload (3.3 KB)

Related Posts

Leave a Comment