Advertisement
jmyean

Programming the Shift Register and LED Circuit

Apr 30th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Jung Min Yean
  3.  * Learning How to Use a Shift Register
  4.  * 04/30/2019
  5.  */
  6.  
  7.  const int Data = 8; // The output
  8.  const int Latch = 9;
  9.  const int Clock = 10; // Goes around the Shift Register
  10.  int sequence[] = {0,1,2,4,8,16,32,64,128};
  11.  int sequence2[] = {0,1,3,7,15,31,63,127,255};
  12.  int sequence3[] = {255,127,63,31,15,7,3,1,0};
  13.  
  14. void setup()
  15. {
  16.   pinMode(Data, OUTPUT);
  17.   pinMode(Latch, OUTPUT);
  18.   pinMode(Clock, OUTPUT);
  19.  
  20.    digitalWrite(Latch, LOW);
  21.   shiftOut(Data, Clock, MSBFIRST, B11111111); // B means Binary - 1 is on and 0 is off
  22.   digitalWrite(Latch, HIGH);
  23.   delay(200);
  24.  
  25.   digitalWrite(Latch, LOW);
  26.   shiftOut(Data, Clock, MSBFIRST, B00000000); // B means Binary - 1 is on and 0 is off
  27.   digitalWrite(Latch, HIGH);
  28.   delay(200);
  29. }
  30. void lightUp()
  31. {
  32.   for(int i = 0; i < 8; i++)
  33.     {
  34.     digitalWrite(Latch, LOW);
  35.     shiftOut(Data, Clock, MSBFIRST, sequence[i]);
  36.     digitalWrite(Latch, HIGH);
  37.     delay(200);
  38.   }
  39. }
  40.  
  41. void addingUp()
  42. {
  43.   for (int i = 0; i < 9; i++)
  44.   {
  45.     digitalWrite(Latch, LOW);
  46.     shiftOut(Data, Clock, MSBFIRST, sequence2[i]);
  47.     digitalWrite(Latch, HIGH);
  48.     delay(200);
  49.   }
  50. }
  51.  
  52. void subtractingDown()
  53. {
  54.   for (int i = 0; i < 9; i++)
  55.   {
  56.     digitalWrite(Latch, LOW);
  57.     shiftOut(Data, Clock, MSBFIRST, sequence3[i]);
  58.     digitalWrite(Latch, HIGH);
  59.     delay(200);
  60.   }
  61. }
  62.  
  63. void loop()
  64. {
  65. lightUp();
  66. addingUp();
  67. subtractingDown();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement