Showing posts with label Led brightness control. Show all posts
Showing posts with label Led brightness control. Show all posts

Saturday, 21 May 2016

Raspberry Pi PWM - Led Brightness Control

Controlling Brightness Of Led Using PWM with Raspberry Pi
Code :

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(21,GPIO.OUT)

p = GPIO.PWM(21,50)
p.start(0)

while True :
for i in range(100) :
p.ChangeDutyCycle(i)
time.sleep(0.05)
for i in range(100) :
p.ChangeDutyCycle(100-i)
time.sleep(0.05)


Output :



Saturday, 4 April 2015

LED Brightness Control Via Serial.....


//Author : Kunchala Anil
//Led Brightness control Via Serial
#define ledstep 5
int ledbrightness = 0;
int led[] = {3,5,6,9,10,11};
void setup(){
Serial.begin(9600);
for(int i=0;i<6;i++){
pinMode(led[i],OUTPUT);
}//end of for loop
}//end of setup

void loop(){
Serial.println("enter y to increse the led brightness and n to decrease it");
serial_check();
}//end of loop


//SERIAL_CHECK() FUNCTION
void serial_check(){
while(!Serial.available()){
//wait until user enters the data
}

while(Serial.available()){
char input = Serial.read();
if(input == 'y'){
increase_brightness();
}//end of if
else if(input == 'n')
{
decrease_brightness();
}//end of else if
else {
error();
}//end of else
}//end of while
}//end of serial_check() function

//INCREASE_BRIGHTNESS FUNCTION
void increase_brightness(){
if(ledbrightness < 250){
ledbrightness = ledbrightness + ledstep;
}//end of if
else
{
Serial.println("brightness is maximum resetting it to 0");
ledbrightness = 0;
}//end of else
for(int j=0;j");
Serial.print(ledbrightness);
Serial.println("");
}//end of increase_brightness() function

//MAINTAIN_BRIGHTNESS FUCNTION
void decrease_brightness(){
if(ledbrightness > 0){
ledbrightness = ledbrightness - ledstep;
}//end of if
else
{
Serial.println("brightness is zero resetting it to 250");
ledbrightness = 250;
}//end of else
for(int j=0;j");
Serial.print(ledbrightness);
Serial.println("");
}//end of maintain_brightness() function

//ERROR FUNCTION
void error(){
Serial.println("you entered a wrong character");
}//end of error function