Embedded Systems Report Lab 5
Embedded Systems Report Lab 5
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.
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
5
Proteus 8 Simulation
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;
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