Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in assembly to modify the state of LED1 and LED2 depending upon

ID: 2079508 • Letter: W

Question

Write a program in assembly to modify the state of LED1 and LED2 depending upon whether SW1 or SW2 is pressed. At program startup, both LED1 and LED2 should be turned off. When SW1 is pressed, both LEDs should begin blinking at approximately 5 Hz. When SW1 is released, both LEDs should turn off. When SW2 is pressed, both LEDs should turn on. When SW2 is released, both LEDs should turn off. If both switches are pressed at the same time, LED1 should turn on and LED2 should begin blinking at approximately 5 Hz. When both switches are released, both LED1 and LED2 should turn off.

Explanation / Answer

/* sketch 1

turn on a LED when the button is pressed

turn it off when the button is not pressed (or released)

*/

int pinButton = 8; //the pin where we connect the button

int LED = 2; //the pin we connect the LED

void setup() {

pinMode(pinButton, INPUT); //set the button pin as INPUT

pinMode(LED, OUTPUT); //set the LED pin as OUTPUT

}

void loop() {

int stateButton = digitalRead(pinButton); //read the state of the button

if(stateButton == 1) { //if is pressed

     digitalWrite(LED, HIGH); //write 1 or HIGH to led pin

} else { //if not pressed

     digitalWrite(LED, LOW); //write 0 or low to led pin

}

/* sketch 2

turn on a LED when the button is pressed and let it on when the button is released

*/

int pinButton = 8; //the pin where we connect the button

int LED = 2; //the pin we connect the LED

void setup() {

pinMode(pinButton, INPUT); //set the button pin as INPUT

pinMode(LED, OUTPUT); //set the LED pin as OUTPUT

}

void loop() {

int stateButton = digitalRead(pinButton); //read the state of the button

if(stateButton == 1) { //if is pressed

     digitalWrite(LED, HIGH); //write 1 or HIGH to led pin

}

}

/* sketch 3

turn on a LED when the button is pressed and let it on

until the button is pressed again

*/

int pinButton = 8;

int LED = 2;

int stateLED = LOW;

int stateButton;

int previous = LOW;

long time = 0;

long debounce = 200;

void setup() {

pinMode(pinButton, INPUT);

pinMode(LED, OUTPUT);

}

void loop() {

stateButton = digitalRead(pinButton);

if(stateButton == HIGH && previous == LOW && millis() - time >debounce) {

if(stateLED == HIGH){

      stateLED = LOW;

    } else {

       stateLED = HIGH;

    }

    time = millis();

}

digitalWrite(LED, stateLED);

previous == stateButton;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote