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

I need help with my code. Here is the assignment and I already wrote the code in

ID: 3794850 • Letter: I

Question

I need help with my code. Here is the assignment and I already wrote the code in C. I just need someone to go over it and try to rewrite all the if and loop statements

Here is the code:

#include "EE3310_header.h"
#include <p24FJ64GA102.h>
#include "xc.h"
#define ON 1
#define OFF 0

#define LED0 _RB3 // LED 0 POUR RESISTANCE RB3 AND SO ON
#define LED1 _RB4
#define LED2 _RB5
#define LED3 _RB6
#define LED4 _RB7
#define LED5 _RB8
#define LED6 _RB9
#define SW3 _RA3


void config_io(void){
TRISA = 0b01000; //set RA3 as input
TRISB = 0x0000; //set all port b as outputs
PORTA = 0b01000; //RA3 set to high
PORTB = 0x0000; //port b set to low
AD1PCFG = 0x1FFF; //set analog pins to digital
_CN29PUE = 1;
}


void DelayLoop (void) { // This loop debounces SW3
int i, j;
for (i = 0; i <= 10; i++) {
for (j = 0; j <= 100; j++){
// do nothing
} // end inner loop
} // end outer loop
} // end DelayLoop


int main(void){
int i = 0;
config_io();
while(1){
if(i == 0){
//PORTB = 0x0000;
if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
if(SW3 == 1)
{
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
LED6 = ON ;
}
  
i = 1;
  
}
}
else if(i == 1){
if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
if(SW3 == 1){
LED6 = OFF;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 2;
}
}
}
else if(i == 2){
if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
if(SW3 == 1){
LED5 = ON;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 3;
}
}
}
else if(i == 3){
if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
if(SW3 == 1){
LED5 = OFF;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 4;
}
}
}
else if(i == 4){
if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 5;
LED4 = ON;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();

}
}
else if(i == 5){
if(SW3 == 1){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 6;
LED4 = OFF;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
LED3 = ON;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
  
}
}
else if(i == 6){
if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 7;
LED2 = ON;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
  
}
}
else if(i == 7){
if(SW3 == 1){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 8;
LED3 = OFF;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
LED1 = ON;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
  
}
}
else if(i == 8){

if(SW3 == 0){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 9;
LED1 = OFF;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
LED0 = ON;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();

}
}
else if(i == 9){
  
if(SW3 == 1){
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
LED2 = OFF;
DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
LED0 = OFF;

DelayLoop();
DelayLoop();
DelayLoop();
DelayLoop();
i = 0;
}
}
} return 0 ;
}

Start with all the LEDs off 1. Press and Release the switch (SW3) one time. LED6 ON 2. Press and Release SW3 again. 3. Press and Release SW3 4. Press and Release SW3 5. Press SW3 6. Release SW3 7. Press SW3 8. Release SW3 9. Press SW3 10. Release SW3 11. Return to Step 1 and repeat LED6 OFF LED5 ON LED5 OFF LED4 ON LED4 OFF and LED3 ON LED2 ON LED3 OFF and LED1 ON LED0 ON and LED1 OFF LED2 OFF and LED0 OFF.

Explanation / Answer

/*Here are some modifications to if and loop statements. This program will run more efficiently*/

#include "EE3310_header.h"
#include <p24FJ64GA102.h>
#include "xc.h"
#define ON 1
#define OFF 0

#define LED0 _RB3 // LED 0 POUR RESISTANCE RB3 AND SO ON
#define LED1 _RB4
#define LED2 _RB5
#define LED3 _RB6
#define LED4 _RB7
#define LED5 _RB8
#define LED6 _RB9
#define SW3 _RA3


void config_io(void){
TRISA = 0b01000; //set RA3 as input
TRISB = 0x0000; //set all port b as outputs
PORTA = 0b01000; //RA3 set to high
PORTB = 0x0000; //port b set to low
AD1PCFG = 0x1FFF; //set analog pins to digital
_CN29PUE = 1;
}


void DelayLoop (void) { // This loop debounces SW3
int i, j;
for (i = 0; i <= 10; i++) {
for (j = 0; j <= 100; j++){
// do nothing
} // end inner loop
} // end outer loop
} // end DelayLoop

int main(void)
{
int i=0;
config_io();
while(1)
{
if(SW==0)
{
DelayLoop();
DelayLoop();
if(i>=0&&i<=3)
{
while(SW==0){}
DelayLoop();
if(i==0)
{
LED6=ON;
i=1;
}
else if(i==1)
{
LED6=OFF;
i=2;
}
else if(i==2)
{
LED5=ON;
i=3;
}
  
else if(i==3)
{
LED5=OFF;
i=4;
}
}
else
{
if(i==4)
{
LED4=ON;
}
else if(i==5)
{
LED2=ON;
}
else if(i==6)
{
LED0=ON;
LED1=OFF;
}
while(SW==0){}
DelayLoop();
if(i==4)
{
LED4=OFF;
LED3=ON;
i=5;
}
else if(i==5)
{
LED3=OFF;
LED1=ON;
i=6;
}
else if(i==6)
{
LED2=OFF;
LED0=OFF;
i=0;
}
}
}
}

return 0;
}

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