Question

Write the code for the following. Write your code on your iPad and submit to this assessment. You may use your notes and a laptop if you wish in order to make sure your syntax is correct. Do not wire this up on a proto board.

1. You are given an LED output called LED11 on pin 11 of the Arduino. Configure it as an OUTPUT

2. You have a pushbutton input switch called SW12 on pin 12 of the Arduino. When the switch is read, its value is to be stored in an integer called sw_in. Configure the input pin 12 as an input with a pullup

3. Show the definitions of these items above the setup() routine. (25 pts)

4. Configure the appropriate inputs/outputs in the setup routine, and initialize the serial console. (25 pts)

5. In the loop() area, (25 pts)

Read the pushbutton switch.

If the value that is read is a 0, then turn on the LED.

If the value that is read is a 1, turn off the LED

6. Throw the values of the pushbutton switch and the LED to the serial console. (25 pts)



NOTE: No partial credit will be given for each portion. So make sure your code and syntax are correct.

Wire your code in Arduino way

Answers

GPT-4o mini
Below is the Arduino code that fulfills the requirements specified in your assessment. The code configures the LED and pushbutton switch, reads the button's state, controls the LED based on that state, and sends the values to the serial console.

```cpp
// Definitions
const int LED11 = 11; // Pin for LED output
const int SW12 = 12; // Pin for pushbutton input
int sw_in; // Variable to store the state of the switch

void setup() {
// Configure the LED as an OUTPUT
pinMode(LED11, OUTPUT);

// Configure the switch as an INPUT with pull-up resistor
pinMode(SW12, INPUT_PULLUP);

// Initialize serial communication
Serial.begin(9600);
}

void loop() {
// Read the pushbutton switch
sw_in = digitalRead(SW12);

// Control the LED based on switch state
if (sw_in == LOW) { // Button pressed (LOW state)
digitalWrite(LED11, HIGH); // Turn on the LED
} else { // Button not pressed (HIGH state)
digitalWrite(LED11, LOW); // Turn off the LED
}

// Print the values to the serial console
Serial.print("Switch State: ");
Serial.print(sw_in);
Serial.print(" | LED State: ");
Serial.println(digitalRead(LED11));

// Small delay for stability
delay(100);
}
```

### Code Explanation:
1. **Definitions**: Constants for the LED and switch pin numbers are set, and an integer variable `sw_in` is declared to store the state of the switch.

2. **Setup Routine**:
- The LED is configured as an output using `pinMode(LED11, OUTPUT)`.
- The pushbutton switch is configured as an input with an internal pull-up resistor using `pinMode(SW12, INPUT_PULLUP)`.
- Serial communication is initialized at a baud rate of 9600 using `Serial.begin(9600)`.

3. **Loop Routine**:
- The state of the pushbutton switch is read into `sw_in`.
- If the button is pressed (`LOW`), the LED is turned on. If it is not pressed (`HIGH`), the LED is turned off.
- The states of the switch and LED are printed to the serial console for monitoring.
- A small delay is included for stability and to avoid flooding the serial output.

Upload this code to your Arduino, and it should function as required in your assessment. Make sure to have the necessary components connected correctly to test the functionality.