LED Decimal to Binary Converter
Use the shift register and serial monitor to convert from decimal to binary and also control brightness of led
Components and supplies
8
LED (generic)
1
Arduino UNO
1
Breadboard (generic)
1
Shift Register- Serial to Parallel
20
Jumper wires (generic)
8
Resistor 221 ohm
Apps and platforms
1
Arduino IDE
Project description
Code
The Code
c_cpp
1//Personal project 2//Controlling LED with serial and shift register 3 4//Setting up shift register Pins 5int latch=10; 6int clockPin=11; 7int dataPin=12; 8int OE=9; 9 10//Creating a buffer to hold values 11char buffer[11]; 12 13void setup() { 14 // put your setup code here, to run once: 15 16 //Setting up shift register Pins 17 pinMode(latch,OUTPUT); 18 pinMode(clockPin,OUTPUT); 19 pinMode(dataPin,OUTPUT); 20 pinMode(OE,OUTPUT); 21 22 //starting Serial monitor 23 Serial.begin(9600); 24} 25 26void loop() { 27 // put your main code here, to run repeatedly: 28 //check if user typed something 29 if (Serial.available() > 0) { 30 int index=0; 31 delay(100); // let the buffer fill up 32 int numChar = Serial.available(); 33 if (numChar>11) { 34 numChar=11; 35 } 36 while (numChar--) { 37 buffer[index++] = Serial.read(); 38 } 39 splitString(buffer); 40 } 41} 42 43void splitString(char* data){ 44 Serial.print("Data entered: "); 45 Serial.println(data); 46 char* parameter; 47 parameter = strtok (data, " ,"); 48 while (parameter != NULL) { 49 setLED(parameter); 50 parameter = strtok (NULL, " ,"); 51 } 52 53 // Clear the text and serial buffers 54 for (int x=0; x<11; x++) { 55 buffer[x]='\\0'; 56 } 57 } 58 void setLED(char* data) { 59 Serial.println(data); 60 if ((data[0] == 'b') || (data[0] == 'B')) { 61 int Ans = strtol(data+1, NULL, 10); 62 Ans = constrain(Ans,0,255); 63 analogWrite(OE, 255-Ans); 64 Serial.print("Brightness is set to: "); 65 Serial.println(Ans); 66 } 67 else{ 68 int num = strtol(data, NULL, 10); 69 num = constrain(Ans,0,255); 70 digitalWrite(latch, LOW); 71 shiftOut(dataPin, clockPin, LSBFIRST, num); 72 digitalWrite(latch, HIGH); 73 Serial.print("number is set to: "); 74 Serial.println(num); 75 } 76 } 77
The Code
c_cpp
1//Personal project 2//Controlling LED with serial and shift register 3 4//Setting up shift register Pins 5int latch=10; 6int clockPin=11; 7int dataPin=12; 8int OE=9; 9 10//Creating a buffer to hold values 11char buffer[11]; 12 13void setup() { 14 // put your setup code here, to run once: 15 16 //Setting up shift register Pins 17 pinMode(latch,OUTPUT); 18 pinMode(clockPin,OUTPUT); 19 pinMode(dataPin,OUTPUT); 20 pinMode(OE,OUTPUT); 21 22 //starting Serial monitor 23 Serial.begin(9600); 24} 25 26void loop() { 27 // put your main code here, to run repeatedly: 28 //check if user typed something 29 if (Serial.available() > 0) { 30 int index=0; 31 delay(100); // let the buffer fill up 32 int numChar = Serial.available(); 33 if (numChar>11) { 34 numChar=11; 35 } 36 while (numChar--) { 37 buffer[index++] = Serial.read(); 38 } 39 splitString(buffer); 40 } 41} 42 43void splitString(char* data){ 44 Serial.print("Data entered: "); 45 Serial.println(data); 46 char* parameter; 47 parameter = strtok (data, " ,"); 48 while (parameter != NULL) { 49 setLED(parameter); 50 parameter = strtok (NULL, " ,"); 51 } 52 53 // Clear the text and serial buffers 54 for (int x=0; x<11; x++) { 55 buffer[x]='\\0'; 56 } 57 } 58 void setLED(char* data) { 59 Serial.println(data); 60 if ((data[0] == 'b') || (data[0] == 'B')) { 61 int Ans = strtol(data+1, NULL, 10); 62 Ans = constrain(Ans,0,255); 63 analogWrite(OE, 255-Ans); 64 Serial.print("Brightness is set to: "); 65 Serial.println(Ans); 66 } 67 else{ 68 int num = strtol(data, NULL, 10); 69 num = constrain(Ans,0,255); 70 digitalWrite(latch, LOW); 71 shiftOut(dataPin, clockPin, LSBFIRST, num); 72 digitalWrite(latch, HIGH); 73 Serial.print("number is set to: "); 74 Serial.println(num); 75 } 76 } 77
Downloadable files
The Circuit
The Circuit
The Circuit
The Circuit

The Circuit
The Circuit
Comments
Only logged in users can leave comments