ayanfeoluwaadekanye1
Published © GPL3+

LED Decimal to Binary Converter

Use the shift register and serial monitor to convert from decimal to binary and also control brightness of led

IntermediateFull instructions provided6,407
LED Decimal to Binary Converter

Things used in this project

Story

Read more

Schematics

The Circuit

The Circuit

Code

The Code

C/C++
//Personal project
//Controlling LED with serial and shift register

//Setting up shift register Pins
int latch=10;
int clockPin=11;
int dataPin=12;
int OE=9;

//Creating a buffer to hold values
char buffer[11];

void setup() {
  // put your setup code here, to run once:

 //Setting up shift register Pins
  pinMode(latch,OUTPUT);
  pinMode(clockPin,OUTPUT);
  pinMode(dataPin,OUTPUT);
  pinMode(OE,OUTPUT);
  
  //starting Serial monitor
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
 //check if user typed something
  if (Serial.available() > 0) {    
    int index=0;   
    delay(100); // let the buffer fill up   
    int numChar = Serial.available();   
  if (numChar>11) {      
    numChar=11;   
  }    
  while (numChar--) {     
    buffer[index++] = Serial.read();    
   }    
   splitString(buffer);  
 } 
}

void splitString(char* data){
  Serial.print("Data entered: "); 
  Serial.println(data);  
  char* parameter;  
  parameter = strtok (data, " ,"); 
  while (parameter != NULL) {    
    setLED(parameter);   
    parameter = strtok (NULL, " ,"); 
    } 
    
    // Clear the text and serial buffers  
  for (int x=0; x<11; x++) {   
     buffer[x]='\0'; 
   }  
  }
 void setLED(char* data) {  
  Serial.println(data);
  if ((data[0] == 'b') || (data[0] == 'B')) {  
    int Ans = strtol(data+1, NULL, 10);  
    Ans = constrain(Ans,0,255);    
    analogWrite(OE, 255-Ans);   
    Serial.print("Brightness is set to: ");  
    Serial.println(Ans);  
    }  
   else{
     int num = strtol(data, NULL, 10);  
     num = constrain(Ans,0,255);
     digitalWrite(latch, LOW);
     shiftOut(dataPin, clockPin, LSBFIRST, num);
     digitalWrite(latch, HIGH);
     Serial.print("number is set to: ");  
     Serial.println(num); 
    }
   }

Credits

ayanfeoluwaadekanye1

ayanfeoluwaadekanye1

0 projects • 7 followers

Comments