Embedded Systems Activity 2 PDF - Digital I/O - Fall 2024

Summary

This document presents an activity on digital I/O for embedded systems, focusing on using a pushbutton to control an LED. The activity includes code examples for Arduino programming.

Full Transcript

Activity 2: Digital I/O Embedded Systems - Julian Barreiro-Gomez Fall - 2024 Brief Description: Use a digital input to read a pushbutton, and then use a digital output to make an LED light only when the button is pressed....

Activity 2: Digital I/O Embedded Systems - Julian Barreiro-Gomez Fall - 2024 Brief Description: Use a digital input to read a pushbutton, and then use a digital output to make an LED light only when the button is pressed. Figure 1: Circuit scheme 1 // Embedded Systems // September 2024 3 // Toy Problem Example 5 const int pin_o = 13; // we define a integer constant for the digital output pin // We create "pin" to easily change the output pin if needed 7 const int pin_i = 1; // we define a integer constant for the digital output pin // We create "pin" to easily change the output pin if needed 9 int State = 0; // this variable will store the "state" of the input (pushbutton) 11 void setup() { 13 // This is executed only once, this function is used for setup only 15 pinMode(pin_o,OUTPUT);// this is to set "pin" (13) as an output pinMode(pin_i,INPUT);// this is to set "pin" (1) as an input 17 } 19 void loop() 21 { // This is executed, and it is within a loop 23 State = digitalRead(pin_i); // we read the pin (1) and assign the value to "State" 25 if(State==1){ // if "State=1" (pressed button), then (13) light on 27 digitalWrite(pin_o,HIGH); // this is to set "pin" (13) to high level } 29 else{ // otherwise (released button), then light (13) off digitalWrite(pin_o,LOW); // this is to set "pin" (13) to low level 31 } 33 } Listing 1: First simple code Khalifa University - Embedded Systems Page 1

Use Quizgecko on...
Browser
Browser