0% found this document useful (0 votes)
22 views

Embedded Systems Report Lab 5

Embedded systems report

Uploaded by

SOPHIA JERARI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Embedded Systems Report Lab 5

Embedded systems report

Uploaded by

SOPHIA JERARI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Embedded Systems Report Lab 5

Made by: Essoufia EL JERARI Grp A1


Supervisored by: Adnane ADDAIM

Table of Contents
Introduction......................................................................................................................3

1
Part that we will need.......................................................................................................3
Arduino sketch & explanation in the comments.................................................................3
BreadBoard Layout............................................................................................................5
Proteus Simulation with 2 push buttons.............................................................................5
Controlling a LED Brigthtness using one Momentary Switch..............................................6
Proteus 8 Simulation..................................................................................................................6
Arduino Sketch Code..................................................................................................................6
Momentary switch explanation..................................................................................................7
LED Brigthtness variation without any button....................................................................7
Arduino Sketch code..................................................................................................................7

Introduction
This experiment is, actually, an extension of the last experiment. Instead of the usual
potentiometer setup, we wrote a sketch that controls LED brightness using two buttons. With this

2
project you will be able to control the brightness of the lamp but, this time, pressing the buttons.
Each of the buttons corresponds to a different intensity.

Part that we will need

Arduino sketch & explanation in the comments


Turn on LED when the button is pressed
// and keep it on after it is released
// including simple de-bouncing.
// If the button is held, brightness changes.
int LedPin=3; // the pin for the led
int ButtonUpPin=12; // the pin for increasing brightness button
int ButtonDownPin=13; // the pin for decreasing brightness button
int BuzzerPin=11; // pin for the buzzer
int ButtonUpState; // store the brightness state up
int ButtonDownState; // store the brightness state down
int LedBrightness=0; // state the initial value of the pin
int WaitTime=200;
int BuzzerTime=2000;

void setup () {
pinMode (LedPin, OUTPUT); // tell Arduino that led is output
pinMode(ButtonUpPin, INPUT); // tell Arduino that buttonup is input

3
pinMode(ButtonDownPin, INPUT); // tell Arduino that buttondown is input
pinMode(BuzzerPin, OUTPUT); } // tell Arduino that the buzzer is output
void loop () {
ButtonUpState=digitalRead(ButtonUpPin); // read the input value and store it
ButtonDownState=digitalRead(ButtonDownPin); // read the input value and store it
if (ButtonUpState==1) { // check if there is a transition
LedBrightness=LedBrightness + 10; // increment of the input by 10 if the up button is pressed
Serial.println(LedBrightness); // for debugging
if (LedBrightness>255) { // set the maximum brightness value
LedBrightness=255; // if we reach the max we set the value to the max
digitalWrite(BuzzerPin, HIGH); // send the information to the buzzer pin
delay(BuzzerTime); // use the delay to avoid buzzing going so fast
digitalWrite(BuzzerPin,LOW); // turn off the buzzer after the buzzer time
}
analogWrite(LedPin,LedBrightness);// turn on the led ON at the current brightness level
delay(WaitTime); // delay to avoid brightness going so fast
}
if (ButtonDownState==1) {
LedBrightness=LedBrightness - 10; // decrement of 10 if the down button is pressed
Serial.println(LedBrightness); // for debugging
if (LedBrightness<0) { // setting minimum value of brightness
LedBrightness=0; // if it exceeds the min value return 0
digitalWrite(BuzzerPin, HIGH); // send the information to the buzzer pin
delay(BuzzerTime); // use the delay to avoid buzzing going so fast
digitalWrite(BuzzerPin,LOW); // turn off the buzzer after the buzzer time
}
analogWrite(LedPin, LedBrightness); // turn ON the led at the current brightness level generally 0
delay(WaitTime); // delay to avoid brightness going so fast
}

BreadBoard Layout

4
Proteus Simulation with 2 push buttons

Result after keeping pressing the up button =


increment by 10*5 times of pressing

Result after keeping the down button = decrement


brightness by 5 times of pressing till it reach value of
0.

Controlling a LED Brigthtness using one Momentary Switch

5
Proteus 8 Simulation

Arduino Sketch Code


int LedPin=3;
int ButtonPin=12;
int ButtonState;
int Increment=20;
int LedBrightness=0;
int WaitTime=200;

void setup () {
pinMode(LedPin, OUTPUT);
pinMode(ButtonPin, INPUT);
}

void loop () {

ButtonState=digitalRead(ButtonPin);
if (ButtonState==1) {

LedBrightness=LedBrightness + Increment;
if ( LedBrightness >=255 || LedBrightness<=0) {
Increment= -Increment; }
analogWrite(LedPin, LedBrightness);
delay(WaitTime);
}
}

6
Momentary switch explanation
Momentary switch: the program tracks the transition of the input
 if ( InputState==1);
ButtonState= digitalRead(Button);
Toggle switch: the program tracks both transition of the input and the states of the input.
 intButtonPreviousState=0;
intButtonCurrentState;
int LedState=0;
Output state= HIGH;
if (( ButtonPreviousState=0)&& (ButtonCurrentState==1))
if (LedState==1)
LedState=0;
else
LedState=1;
digitalWrite(Led,LedState);
ButtonPreviousState=ButtonCurrentState;

LED Brigthtness variation without any button

Arduino Sketch code


int LedPin=3;
int Increment=20;
int LedBrightness=0;
int WaitTime=200;

void setup () {
pinMode(LedPin, OUTPUT);
}

void loop () {

LedBrightness=LedBrightness + Increment;
if ( LedBrightness >=255 || LedBrightness<=0) {
Increment= -Increment; }
analogWrite(LedPin, LedBrightness);
delay(WaitTime);
}

7
Max brightness =255 Medium brightness = brightness + old brightness value

Min Brightness = 0

You might also like