Components and supplies
74HC595 shift register
7 Segment LED Display, InfoVue
Jumper wires (generic)
Arduino UNO
Resistor 220 ohm
Apps and platforms
Arduino IDE
Arduino Web Editor
Project description
Code
The code
c_cpp
1const int latchPin = 13; //latch pin of the 74HC595 2const int clockPin = 12; //clock pin of the 74HC595 3const int dataPin = 8; //data pin of the 74HC595 4byte leds = 0; //byte variable that holds which LED is turned on at a time 5int repeatNum = 300; //number of times to repeat each number 6int counter = 0; //counter variable used in the for loops 7// Be warned: some numbers take longer to do because they use more LEDS than others, for instance, 1 (uses 2) and 8 (uses 7.) I have tried to make them about equal in terms of time 8/* The lines of code: 9* leds = 0; 10* updateShiftRegister(); 11* turn off all LEDs 12*/ 13/* 14* LED-[ U ]-VCC 15* LED-[ ]-LED 16* LED-[ ]-Pin 8 - Data Pin 17* LED-[ ]-GND 18* LED-[ ]-Pin 13 - Latch Pin 19* LED-[ ]-Pin 12 - Clock Pin 20* LED-[ ]-VCC 21* GND-[ ]-None (Q7') 22* _3_ 23*5 - [ ] - 2 24* [_6_] 25*4 - [ ] - 8 26* [_0_](.) - 7 27*/ 28void setup() { 29 pinMode(latchPin, OUTPUT); 30 pinMode(dataPin, OUTPUT); 31 pinMode(clockPin, OUTPUT); 32} 33void loop() { 34 for(counter = 0; counter <= repeatNum; counter++){ 35 Zero(); 36 } 37 for(counter = 0; counter <= repeatNum + 400; counter++){ 38 One(); 39 } 40 for(counter = 0; counter <= repeatNum; counter++){ 41 Two(); 42 } 43 for(counter = 0; counter <= repeatNum; counter++){ 44 Three(); 45 } 46 for(counter = 0; counter <= repeatNum; counter++){ 47 Four(); 48 } 49 for(counter = 0; counter <= repeatNum; counter++){ 50 Five(); 51 } 52 for(counter = 0; counter <= repeatNum; counter++){ 53 Six(); 54 } 55 for(counter = 0; counter <= repeatNum + 250; counter++){ 56 Seven(); 57 } 58 for(counter = 0; counter <= repeatNum; counter++){ 59 Eight(); 60 } 61 for(counter = 0; counter <= repeatNum; counter++){ 62 Nine(); 63 } 64} 65void updateShiftRegister() 66{ 67 digitalWrite(latchPin, LOW); 68 // set the latch pin to low so that the 74HC595 can recieve data 69 shiftOut(dataPin, clockPin, MSBFIRST, leds); 70 //write the byte leds to the register 71 digitalWrite(latchPin, HIGH); 72 //set latch pin back to high 73} 74void One(){ 75 leds = 0; 76 updateShiftRegister(); 77 leds = 0; 78 bitSet(leds, 6); 79 updateShiftRegister(); 80 delay(1); 81 leds = 0; 82 updateShiftRegister(); 83 leds = 0; 84 bitSet(leds, 1); 85 updateShiftRegister(); 86 delay(1); 87} 88void Two(){ 89 leds = 0; 90 updateShiftRegister(); 91 leds = 0; 92 bitSet(leds, 2); 93 updateShiftRegister(); 94 delay(1); 95 leds = 0; 96 updateShiftRegister(); 97 leds = 0; 98 bitSet(leds, 1); 99 updateShiftRegister(); 100 delay(1); 101 leds = 0; 102 updateShiftRegister(); 103 leds = 0; 104 bitSet(leds, 5); 105 updateShiftRegister(); 106 delay(1); 107 leds = 0; 108 updateShiftRegister(); 109 leds = 0; 110 bitSet(leds, 3); 111 updateShiftRegister(); 112 delay(1); 113 leds = 0; 114 updateShiftRegister(); 115 leds = 0; 116 bitSet(leds, 0); 117 updateShiftRegister(); 118 delay(1); 119} 120void Three(){ 121 leds = 0; 122 updateShiftRegister(); 123 leds = 0; 124 bitSet(leds, 2); 125 updateShiftRegister(); 126 delay(1); 127 leds = 0; 128 updateShiftRegister(); 129 leds = 0; 130 bitSet(leds, 1); 131 updateShiftRegister(); 132 delay(1); 133 leds = 0; 134 updateShiftRegister(); 135 leds = 0; 136 bitSet(leds, 5); 137 updateShiftRegister(); 138 delay(1); 139 leds = 0; 140 updateShiftRegister(); 141 leds = 0; 142 bitSet(leds, 6); 143 updateShiftRegister(); 144 delay(1); 145 leds = 0; 146 updateShiftRegister(); 147 leds = 0; 148 bitSet(leds, 0); 149 updateShiftRegister(); 150 delay(1); 151} 152void Four(){ 153 leds = 0; 154 updateShiftRegister(); 155 leds = 0; 156 bitSet(leds, 4); 157 updateShiftRegister(); 158 delay(1); 159 leds = 0; 160 updateShiftRegister(); 161 leds = 0; 162 bitSet(leds, 5); 163 updateShiftRegister(); 164 delay(1); 165 leds = 0; 166 updateShiftRegister(); 167 leds = 0; 168 bitSet(leds, 1); 169 updateShiftRegister(); 170 delay(1); 171 leds = 0; 172 updateShiftRegister(); 173 leds = 0; 174 bitSet(leds, 6); 175 updateShiftRegister(); 176 delay(1); 177} 178void Five(){ 179 leds = 0; 180 updateShiftRegister(); 181 leds = 0; 182 bitSet(leds, 2); 183 updateShiftRegister(); 184 delay(1); 185 leds = 0; 186 updateShiftRegister(); 187 leds = 0; 188 bitSet(leds, 4); 189 updateShiftRegister(); 190 delay(1); 191 leds = 0; 192 updateShiftRegister(); 193 leds = 0; 194 bitSet(leds, 5); 195 updateShiftRegister(); 196 delay(1); 197 leds = 0; 198 updateShiftRegister(); 199 leds = 0; 200 bitSet(leds, 6); 201 updateShiftRegister(); 202 delay(1); 203 leds = 0; 204 updateShiftRegister(); 205 leds = 0; 206 bitSet(leds, 0); 207 updateShiftRegister(); 208 delay(1); 209} 210void Six(){ 211 leds = 0; 212 updateShiftRegister(); 213 leds = 0; 214 bitSet(leds, 2); 215 updateShiftRegister(); 216 delay(1); 217 leds = 0; 218 updateShiftRegister(); 219 leds = 0; 220 bitSet(leds, 4); 221 updateShiftRegister(); 222 delay(1); 223 leds = 0; 224 updateShiftRegister(); 225 leds = 0; 226 bitSet(leds, 5); 227 updateShiftRegister(); 228 delay(1); 229 leds = 0; 230 updateShiftRegister(); 231 leds = 0; 232 bitSet(leds, 6); 233 updateShiftRegister(); 234 delay(1); 235 leds = 0; 236 updateShiftRegister(); 237 leds = 0; 238 bitSet(leds, 0); 239 updateShiftRegister(); 240 delay(1); 241 leds = 0; 242 updateShiftRegister(); 243 leds = 0; 244 bitSet(leds, 3); 245 updateShiftRegister(); 246 delay(1); 247} 248void Seven(){ 249 leds = 0; 250 updateShiftRegister(); 251 leds = 0; 252 bitSet(leds, 6); 253 updateShiftRegister(); 254 delay(1); 255 leds = 0; 256 updateShiftRegister(); 257 leds = 0; 258 bitSet(leds, 1); 259 updateShiftRegister(); 260 delay(1); 261 leds = 0; 262 updateShiftRegister(); 263 leds = 0; 264 bitSet(leds, 2); 265 updateShiftRegister(); 266 delay(1); 267} 268void Eight(){ 269 leds = 0; 270 updateShiftRegister(); 271 leds = 0; 272 bitSet(leds, 2); 273 updateShiftRegister(); 274 delay(1); 275 leds = 0; 276 updateShiftRegister(); 277 leds = 0; 278 bitSet(leds, 4); 279 updateShiftRegister(); 280 delay(1); 281 leds = 0; 282 updateShiftRegister(); 283 leds = 0; 284 bitSet(leds, 5); 285 updateShiftRegister(); 286 delay(1); 287 leds = 0; 288 updateShiftRegister(); 289 leds = 0; 290 bitSet(leds, 6); 291 updateShiftRegister(); 292 delay(1); 293 leds = 0; 294 updateShiftRegister(); 295 leds = 0; 296 bitSet(leds, 0); 297 updateShiftRegister(); 298 delay(1); 299 leds = 0; 300 updateShiftRegister(); 301 leds = 0; 302 bitSet(leds, 3); 303 updateShiftRegister(); 304 delay(1); 305 leds = 0; 306 updateShiftRegister(); 307 leds = 0; 308 bitSet(leds, 1); 309 updateShiftRegister(); 310 delay(1); 311} 312void Nine(){ 313 leds = 0; 314 updateShiftRegister(); 315 leds = 0; 316 bitSet(leds, 2); 317 updateShiftRegister(); 318 delay(1); 319 leds = 0; 320 updateShiftRegister(); 321 leds = 0; 322 bitSet(leds, 4); 323 updateShiftRegister(); 324 delay(1); 325 leds = 0; 326 updateShiftRegister(); 327 leds = 0; 328 bitSet(leds, 5); 329 updateShiftRegister(); 330 delay(1); 331 leds = 0; 332 updateShiftRegister(); 333 leds = 0; 334 bitSet(leds, 6); 335 updateShiftRegister(); 336 delay(1); 337 leds = 0; 338 updateShiftRegister(); 339 leds = 0; 340 bitSet(leds, 0); 341 updateShiftRegister(); 342 delay(1); 343 leds = 0; 344 updateShiftRegister(); 345 leds = 0; 346 bitSet(leds, 1); 347 updateShiftRegister(); 348 delay(1); 349} 350void Zero(){ 351 leds = 0; 352 updateShiftRegister(); 353 leds = 0; 354 bitSet(leds, 2); 355 updateShiftRegister(); 356 delay(1); 357 leds = 0; 358 updateShiftRegister(); 359 leds = 0; 360 bitSet(leds, 4); 361 updateShiftRegister(); 362 delay(1); 363 leds = 0; 364 updateShiftRegister(); 365 leds = 0; 366 bitSet(leds, 1); 367 updateShiftRegister(); 368 delay(1); 369 leds = 0; 370 updateShiftRegister(); 371 leds = 0; 372 bitSet(leds, 6); 373 updateShiftRegister(); 374 delay(1); 375 leds = 0; 376 updateShiftRegister(); 377 leds = 0; 378 bitSet(leds, 0); 379 updateShiftRegister(); 380 delay(1); 381 leds = 0; 382 updateShiftRegister(); 383 leds = 0; 384 bitSet(leds, 3); 385 updateShiftRegister(); 386 delay(1); 387}
The code
c_cpp
1const int latchPin = 13; //latch pin of the 74HC595 2const int clockPin 3 = 12; //clock pin of the 74HC595 4const int dataPin = 8; //data pin of the 74HC595 5byte 6 leds = 0; //byte variable that holds which LED is turned on at a time 7int repeatNum 8 = 300; //number of times to repeat each number 9int counter = 0; //counter variable 10 used in the for loops 11// Be warned: some numbers take longer to do because they 12 use more LEDS than others, for instance, 1 (uses 2) and 8 (uses 7.) I have tried 13 to make them about equal in terms of time 14/* The lines of code: 15* leds = 0; 16* 17 updateShiftRegister(); 18* turn off all LEDs 19*/ 20/* 21* LED-[ U ]-VCC 22* 23 LED-[ ]-LED 24* LED-[ ]-Pin 8 - Data Pin 25* LED-[ ]-GND 26* LED-[ 27 ]-Pin 13 - Latch Pin 28* LED-[ ]-Pin 12 - Clock Pin 29* LED-[ ]-VCC 30* 31 GND-[ ]-None (Q7') 32* _3_ 33*5 - [ ] - 2 34* [_6_] 35*4 36 - [ ] - 8 37* [_0_](.) - 7 38*/ 39void setup() { 40 pinMode(latchPin, OUTPUT); 41 42 pinMode(dataPin, OUTPUT); 43 pinMode(clockPin, OUTPUT); 44} 45void loop() { 46 47 for(counter = 0; counter <= repeatNum; counter++){ 48 Zero(); 49 } 50 for(counter 51 = 0; counter <= repeatNum + 400; counter++){ 52 One(); 53 } 54 for(counter 55 = 0; counter <= repeatNum; counter++){ 56 Two(); 57 } 58 for(counter = 0; 59 counter <= repeatNum; counter++){ 60 Three(); 61 } 62 for(counter = 0; counter 63 <= repeatNum; counter++){ 64 Four(); 65 } 66 for(counter = 0; counter <= 67 repeatNum; counter++){ 68 Five(); 69 } 70 for(counter = 0; counter <= repeatNum; 71 counter++){ 72 Six(); 73 } 74 for(counter = 0; counter <= repeatNum + 250; 75 counter++){ 76 Seven(); 77 } 78 for(counter = 0; counter <= repeatNum; counter++){ 79 80 Eight(); 81 } 82 for(counter = 0; counter <= repeatNum; counter++){ 83 Nine(); 84 85 } 86} 87void updateShiftRegister() 88{ 89 digitalWrite(latchPin, LOW); 90 91 // set the latch pin to low so that the 74HC595 can recieve data 92 shiftOut(dataPin, 93 clockPin, MSBFIRST, leds); 94 //write the byte leds to the register 95 digitalWrite(latchPin, 96 HIGH); 97 //set latch pin back to high 98} 99void One(){ 100 leds = 0; 101 updateShiftRegister(); 102 103 leds = 0; 104 bitSet(leds, 6); 105 updateShiftRegister(); 106 delay(1); 107 leds 108 = 0; 109 updateShiftRegister(); 110 leds = 0; 111 bitSet(leds, 1); 112 updateShiftRegister(); 113 114 delay(1); 115} 116void Two(){ 117 leds = 0; 118 updateShiftRegister(); 119 leds 120 = 0; 121 bitSet(leds, 2); 122 updateShiftRegister(); 123 delay(1); 124 leds = 0; 125 126 updateShiftRegister(); 127 leds = 0; 128 bitSet(leds, 1); 129 updateShiftRegister(); 130 131 delay(1); 132 leds = 0; 133 updateShiftRegister(); 134 leds = 0; 135 bitSet(leds, 136 5); 137 updateShiftRegister(); 138 delay(1); 139 leds = 0; 140 updateShiftRegister(); 141 142 leds = 0; 143 bitSet(leds, 3); 144 updateShiftRegister(); 145 delay(1); 146 leds 147 = 0; 148 updateShiftRegister(); 149 leds = 0; 150 bitSet(leds, 0); 151 updateShiftRegister(); 152 153 delay(1); 154} 155void Three(){ 156 leds = 0; 157 updateShiftRegister(); 158 leds 159 = 0; 160 bitSet(leds, 2); 161 updateShiftRegister(); 162 delay(1); 163 leds = 0; 164 165 updateShiftRegister(); 166 leds = 0; 167 bitSet(leds, 1); 168 updateShiftRegister(); 169 170 delay(1); 171 leds = 0; 172 updateShiftRegister(); 173 leds = 0; 174 bitSet(leds, 175 5); 176 updateShiftRegister(); 177 delay(1); 178 leds = 0; 179 updateShiftRegister(); 180 181 leds = 0; 182 bitSet(leds, 6); 183 updateShiftRegister(); 184 delay(1); 185 leds 186 = 0; 187 updateShiftRegister(); 188 leds = 0; 189 bitSet(leds, 0); 190 updateShiftRegister(); 191 192 delay(1); 193} 194void Four(){ 195 leds = 0; 196 updateShiftRegister(); 197 leds 198 = 0; 199 bitSet(leds, 4); 200 updateShiftRegister(); 201 delay(1); 202 leds = 0; 203 204 updateShiftRegister(); 205 leds = 0; 206 bitSet(leds, 5); 207 updateShiftRegister(); 208 209 delay(1); 210 leds = 0; 211 updateShiftRegister(); 212 leds = 0; 213 bitSet(leds, 214 1); 215 updateShiftRegister(); 216 delay(1); 217 leds = 0; 218 updateShiftRegister(); 219 220 leds = 0; 221 bitSet(leds, 6); 222 updateShiftRegister(); 223 delay(1); 224} 225void 226 Five(){ 227 leds = 0; 228 updateShiftRegister(); 229 leds = 0; 230 bitSet(leds, 231 2); 232 updateShiftRegister(); 233 delay(1); 234 leds = 0; 235 updateShiftRegister(); 236 237 leds = 0; 238 bitSet(leds, 4); 239 updateShiftRegister(); 240 delay(1); 241 leds 242 = 0; 243 updateShiftRegister(); 244 leds = 0; 245 bitSet(leds, 5); 246 updateShiftRegister(); 247 248 delay(1); 249 leds = 0; 250 updateShiftRegister(); 251 leds = 0; 252 bitSet(leds, 253 6); 254 updateShiftRegister(); 255 delay(1); 256 leds = 0; 257 updateShiftRegister(); 258 259 leds = 0; 260 bitSet(leds, 0); 261 updateShiftRegister(); 262 delay(1); 263} 264void 265 Six(){ 266 leds = 0; 267 updateShiftRegister(); 268 leds = 0; 269 bitSet(leds, 2); 270 271 updateShiftRegister(); 272 delay(1); 273 leds = 0; 274 updateShiftRegister(); 275 276 leds = 0; 277 bitSet(leds, 4); 278 updateShiftRegister(); 279 delay(1); 280 leds 281 = 0; 282 updateShiftRegister(); 283 leds = 0; 284 bitSet(leds, 5); 285 updateShiftRegister(); 286 287 delay(1); 288 leds = 0; 289 updateShiftRegister(); 290 leds = 0; 291 bitSet(leds, 292 6); 293 updateShiftRegister(); 294 delay(1); 295 leds = 0; 296 updateShiftRegister(); 297 298 leds = 0; 299 bitSet(leds, 0); 300 updateShiftRegister(); 301 delay(1); 302 leds 303 = 0; 304 updateShiftRegister(); 305 leds = 0; 306 bitSet(leds, 3); 307 updateShiftRegister(); 308 309 delay(1); 310} 311void Seven(){ 312 leds = 0; 313 updateShiftRegister(); 314 leds 315 = 0; 316 bitSet(leds, 6); 317 updateShiftRegister(); 318 delay(1); 319 leds = 0; 320 321 updateShiftRegister(); 322 leds = 0; 323 bitSet(leds, 1); 324 updateShiftRegister(); 325 326 delay(1); 327 leds = 0; 328 updateShiftRegister(); 329 leds = 0; 330 bitSet(leds, 331 2); 332 updateShiftRegister(); 333 delay(1); 334} 335void Eight(){ 336 leds = 0; 337 338 updateShiftRegister(); 339 leds = 0; 340 bitSet(leds, 2); 341 updateShiftRegister(); 342 343 delay(1); 344 leds = 0; 345 updateShiftRegister(); 346 leds = 0; 347 bitSet(leds, 348 4); 349 updateShiftRegister(); 350 delay(1); 351 leds = 0; 352 updateShiftRegister(); 353 354 leds = 0; 355 bitSet(leds, 5); 356 updateShiftRegister(); 357 delay(1); 358 leds 359 = 0; 360 updateShiftRegister(); 361 leds = 0; 362 bitSet(leds, 6); 363 updateShiftRegister(); 364 365 delay(1); 366 leds = 0; 367 updateShiftRegister(); 368 leds = 0; 369 bitSet(leds, 370 0); 371 updateShiftRegister(); 372 delay(1); 373 leds = 0; 374 updateShiftRegister(); 375 376 leds = 0; 377 bitSet(leds, 3); 378 updateShiftRegister(); 379 delay(1); 380 leds 381 = 0; 382 updateShiftRegister(); 383 leds = 0; 384 bitSet(leds, 1); 385 updateShiftRegister(); 386 387 delay(1); 388} 389void Nine(){ 390 leds = 0; 391 updateShiftRegister(); 392 leds 393 = 0; 394 bitSet(leds, 2); 395 updateShiftRegister(); 396 delay(1); 397 leds = 0; 398 399 updateShiftRegister(); 400 leds = 0; 401 bitSet(leds, 4); 402 updateShiftRegister(); 403 404 delay(1); 405 leds = 0; 406 updateShiftRegister(); 407 leds = 0; 408 bitSet(leds, 409 5); 410 updateShiftRegister(); 411 delay(1); 412 leds = 0; 413 updateShiftRegister(); 414 415 leds = 0; 416 bitSet(leds, 6); 417 updateShiftRegister(); 418 delay(1); 419 leds 420 = 0; 421 updateShiftRegister(); 422 leds = 0; 423 bitSet(leds, 0); 424 updateShiftRegister(); 425 426 delay(1); 427 leds = 0; 428 updateShiftRegister(); 429 leds = 0; 430 bitSet(leds, 431 1); 432 updateShiftRegister(); 433 delay(1); 434} 435void Zero(){ 436 leds = 0; 437 438 updateShiftRegister(); 439 leds = 0; 440 bitSet(leds, 2); 441 updateShiftRegister(); 442 443 delay(1); 444 leds = 0; 445 updateShiftRegister(); 446 leds = 0; 447 bitSet(leds, 448 4); 449 updateShiftRegister(); 450 delay(1); 451 leds = 0; 452 updateShiftRegister(); 453 454 leds = 0; 455 bitSet(leds, 1); 456 updateShiftRegister(); 457 delay(1); 458 leds 459 = 0; 460 updateShiftRegister(); 461 leds = 0; 462 bitSet(leds, 6); 463 updateShiftRegister(); 464 465 delay(1); 466 leds = 0; 467 updateShiftRegister(); 468 leds = 0; 469 bitSet(leds, 470 0); 471 updateShiftRegister(); 472 delay(1); 473 leds = 0; 474 updateShiftRegister(); 475 476 leds = 0; 477 bitSet(leds, 3); 478 updateShiftRegister(); 479 delay(1); 480}
Downloadable files
arduino_uno_74hc595_7segment_bb_0PQsprMvxy.jpg
arduino_uno_74hc595_7segment_bb_0PQsprMvxy.jpg
Comments
Only logged in users can leave comments
Anonymous user
2 years ago
great job ... I was going crazy with a wrong scheme and code that I found on the ELEGOO manual for this topic. Thanks to you I was able to solve the gap. I give you just one piece of advice: improve your scheme a little because yours is a good job and deserves to be accompanied by a good drawing. Well done! :)