PLEASE HELP WITH THIS CODE IT IS NOT TURN ON THE LIGHTS AS STATE BELOW NOTE SW3
ID: 3747859 • Letter: P
Question
PLEASE HELP WITH THIS CODE IT IS NOT TURN ON THE LIGHTS AS STATE BELOW NOTE SW3 IS A SWITCH
/* Created on September 12, 2018, 8:49 AM */
/* Insert any include files appropriate */
#include "xc.h"
#include "../EE3310_2188_header.h"
/* Insert your #define values here */
#define LED1 _RB15
#define SW3 _RA4
#define LED0Gn _RA0
#define LED1Gn _RA1
#define LED2Rd _RA2
#define LED0Rd _RA3
#define LED3Rd _RB0
#define LED1Bl _RB3
#define LED2Bl _RB2
/* Declare Global Variables here */
int i, j, state;
/* Insert Subroutines (Function Calls here */ void ConfigIO(void)
{ TRISA = 0b10000; ANSELA = 0;
LATA = 0b00000;
LATB = 0x0000;
TRISB = 0x0000;
ANSELB = 0;
_CNPDA4 = 1;
int main(void) {
/* Declare Local Variables here */
int k = 0;
int state;
/* Call the initialization functions */
ConfigIO();
/* Here's the while loop that executes all operations.*/
//HERE IS MY PROBLEM
1. Press and Release the switch (SW3) one time. LED0-Green à ON.
2. Press and Release SW3 again. LED0-Green à OFF.
3. Press and Release SW3 LED1-Green à ON.
4. Press and Release SW3 LED1-Green à OFF.
5. Press SW3 LED2-Red à ON.
6. Release SW3 LED2-Red à OFF and LED0-Red à ON.
7. Press SW3 LED3-Red à ON.
8. Release SW3 LED0-Red à OFF and LED1-Blue à ON.
9. Press SW3 LED2-Blue à ON and LED3-Red à OFF.
10. Release SW3 LED2-Blue à OFF and LED1-Blue à OFF.
11. Return to Step 1 and repeat
switch (state) {
case 0:
(SW3 == 1);
state = 1;
break;
case 1:
(SW3 == 0);
LED0Gn = 1;
state = 2;
break;
case 2:
(SW3 == 0)
state = 3;
break;
case 3:
(SW3 == 1)
LED0Gn = 0;
state = 4;
break;
case 4:
(SW3 == 0)
state = 5;
break;
case 5:
(SW3 == 1)
LED1Gn = 1;
state = 6;
break;
case 6:
(SW3 == 0)
state = 7;
break;
case 7:
(SW3 == 1)
LED1Gn = 0;
state = 8;
break;
case 8:
(SW3 == 0)
LED2Rd = 1;
state = 9;
break;
case 9:
(SW3 == 1)
LED2Rd = 0;
LED0Rd = 1;
state = 10;
break;
case 10:
(SW3 == 0)
LED3Rd = 1;
state = 11;
break;
case 11:
(SW3 == 1)
LED0Rd = 0;
LED1Bl = 1;
state = 12;
break;
case 12:
(SW3 == 0)
LED2Bl = 1;
LED3Rd = 0;
state = 13;
break;
case 13:
(SW3 == 1)
LED2Bl = 0;
LED1Bl = 0;
state = 0;
break;
}
return 0;
} // end main
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.