EE 391 Lab1 Done by Anas Bamaqa
EE 391 Lab1 Done by Anas Bamaqa
Lab Report 1
Comment:
It is function, is to use the push button to turn off and on the LED. When
we press the button, it will turn on and when we stop pressing it will
turn off.
Part II: Draw the circuit and attach the code, explain the
functionality of the code on the circuit.
int switchState = 0;
void setup(){
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);
}
void loop(){
switchState = digitalRead(2);
if(switchState == LOW) {
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
}
else{
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
delay(250);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
delay(250);
}
}
Explanation:
To enhance clarity in our explanation, I've assigned numbers to the LED
colors and designated pin2 as the button input. When the button isn't
pressed, the initial state illuminates the first two LEDs: green and red.
Yet, when the button is pressed, the first and second LEDs will turn off.
Subsequently, the third LED will light up for a 250-millisecond duration.
After this interval, the third LED will switch off, and the second LED will
illuminate for another 250 milliseconds. This sequence will repeat in a
continuous loop as long as the button remains pressed.
Part III: Draw the circuit and attach the code, explain the
functionality of the code on the circuit.
int switchState = 0;
void setup(){
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);
}
void loop(){
switchState = digitalRead(2);
if(switchState == LOW) {
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
delay(200);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
delay(200);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
delay(200);
}
else{
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
}
Explanation:
To enhance clarity in our explanation, I've assigned numbers to the LED
colors and designated pin2 as the button input. In the absence of a
button press, the system operates as follows: the first LED lights up
exclusively for 200 milliseconds with all other LEDs turned off, followed
by the second LED with all other LEDs turned off for the same duration.
Subsequently, the third LED illuminates while all others remain off for
another 200 milliseconds. This cycle repeats continuously as long as the
button remains unpressed. However, pressing the button will instantly
turn off all the lights.
Part IV: Comment on how defining the variable “switchst” can
be useful.
We can easily change the state of the LEDs and button by modifying
only one variable instead of changing each LED status one by one. And
for that we could achieve a code that is more readable and will help us
to quickly change the behavior of the LEDs status and button.
Part V: Draw the circuit and attach the code, explain the
functionality of the code on the circuit.
int switchState = 0;
int switchst = LOW;
void setup(){
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);
}
void loop(){
switchState = digitalRead(2);
if(switchState == switchst) {
digitalWrite(3,~switchst);
digitalWrite(4,switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,~switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,switchst);
digitalWrite(5,~switchst);
delay(200);
}
else{
digitalWrite(3,switchst);
digitalWrite(4,switchst);
digitalWrite(5,~switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,~switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,~switchst);
digitalWrite(4,switchst);
digitalWrite(5,switchst);
delay(200);
}
}
Explanation:
To enhance clarity in our explanation, I've assigned numbers to the LED
colors and designated pin2 as the button input. In the absence of a
button press, the system operates as follows: the first LED lights up
exclusively for 200 milliseconds with all other LEDs turned off, followed
by the second LED with all other LEDs turned off for the same duration.
Subsequently, the third LED illuminates while all others remain off for
another 200 milliseconds. This cycle repeats continuously as long as the
button remains unpressed. However, pressing the button it will flip how
the LEDs turn on. Which will be the third LED lights up exclusively for
200 milliseconds with all other LEDs turned off, followed by the second
LED with all other LEDs turned off for the same duration. Subsequently,
the first LED illuminates while all others remain off for another 200
milliseconds. This cycle repeats continuously as long as the button
remains pressed.
Challenges: Write down any challenges that you have
encountered during the experiment.
I encountered a couple of difficulties during the project. Firstly, I had
trouble activating the button because its pins were quite small, making
it challenging to establish a proper electrical connection. Secondly, I
faced issues with the LEDs not turning on, even though I was confident
that they were correctly connected to the breadboard. It turned out
that the problem was related to the resistor's value, as it was drawing
too much current, leaving insufficient current to power up the LEDs.
Conclusion:
We learned how to use Arduino and how to program it using the C
language. We also learned how to program using C language. We used
the Arduino to write some simple programs. We also learned how to
simulate the Arduino using tinkercad website and we use it to write
some simple programs.