Homework Solution: Need to create state machine for this project given these constraints. T…

    Need to create state machine for this project given these constraints. The micro-controller is the ATmega2560. Overall am must compile. This can be tested using the ch eckmark icon. 1. The pragr 2. The project must be pushed to GitLab and an appropriate response from the server must be received: Got your subnission. Thanks! 3. Arduino libraries are not allowed at all for this lab for the exception of debug functionality using Serial.printin. switch.cpp 1. A function implementaticn that returns void and has no parameters called initswitchPB3 must be present and is used in the main function to initialize the switch on the pin named P83 2. P83 must be initial ized with an input pull-up resistor 3. Fin-Channtpls mustbe eabdfor piri PB3 led.cpp 1. A functio impentaticn thal rtuns void and has n pataels called inilEDmst be present and is used in the main function to initialize all LED pins as outputs. 2. Fins named PAO, PA1, PAZ, and PA3 must be used to contral the LEDs 3. A function implementation called turnOnLEDWithChar that returns void and has a parometer called num of type unsigned unsigned char must be present. 4. The turnOnl EDWithChar function must be one line of code, see LED ㏄ntral section for more details. switch.cpp 1. A function implementation that returns void and has no parameters called initSwitch must be present and is usedl in the main function to initialize pin 32 on the development board as an input. timer.cpp 1. A function implementation that rcturns void and has no parameters called initTimerO must be present and must initialize timer 0. 2. A function implementation that rts id and has no pas called delayMs must bpresnt and must implement a precise millisecond delay and can work at least up to 100 millisecondls. For loops are allowec. main.cpp 1. A call to initLEDO and initswitchPB3) must be present in the main function. 2. An infinite while loop must be prescnt. 3. A state machine for the entire project must be implemented using a typedef enum for states. 4. An ISR to handle the switch being pressed must be present and be used to change states appropriately 5. The switch press must be debounced using an appropriately dlesigned state machine. 6. When the switch is pressed and released, the LEDs change the pace at which they link (either half or twice depending on the state) .LEDs must blink either every 100 ms or 200 ms depending on the state.
    This program must compile. This can be tested using the checkmark icon. The project must be pushed to GitLab and an appropriate response from the server must be received: "Got your submission. Thanks!" Arduino libraries are not allowed at all for this lab for the exception of debug functionality using Serial.println. A function implementation that returns void and has no parameters called initswitchPB3 must be present and is used in the main function to initialize the switch on the pin named PB3 PB3 must be initialized with an input pull-up resistor Pin-Change interrupts must be enabled for pin PB3 A function implementation that returns void and has no parameters called initLED must be present and is used in the main function to initialize all LED pins as outputs. Fins named PA0, PA1, PA2, and PA3 must be used to control the LEDs A function implementation called turnOnLEDWithChar that returns void and has a parameter called num of type unsigned unsigned char must be present. The turnOnLEDWithChar function must be one line of code, see LED control section for more details. A function implementation that returns void and has no parameters called initSwitch must be present and is used in the main function to initialize pin 32 on the development board as an input. A function implementation that returns void and has no parameters called initTimer0 must be present and must initialize timer 0. A function implementation that returns void and has no pas called delayMs must be present and must implement a precise millisecond delay and can work at least up to 100 milliseconds. For loops are allowed. A call to initLED0 and initswitchPB3() must be present in the main function. An infinite while loop must be present. A state machine for the entire project must be implemented using a typedef enum for states. An ISR to handle the switch being pressed must be present and be used to change states appropriately. The switch press must be debounced using an appropriately designed state machine. When the switch is pressed and released, the LEDs change the pace at which they link (either half or twice depending on the state). LEDs must blink either every 100 ms or 200 ms depending on the state.

    Expert Answer

     
    main.cpp ------------------------------------------- #include <Arduino.h>

    Insufficiency to fashion say tool restraint this scheme loving these constraints. The micro-controller is the ATmega2560.

    Overwhole am must determinedtle. This can be tested using the ch eckmark icon. 1. The pragr 2. The scheme must be pushed to GitLab and an embezzle retort from the server must be received: Got your subnission. Thanks! 3. Arduino libraries are referable attributable attributable attributable attributable attributable attributable known at whole restraint this lab restraint the qualification of debug dutyality using Serial.printin. switch.cpp 1. A duty toolaticn that avail lacking and has no parameters designated initswitchPB3 must be give and is authenticationd in the deep duty to primalize the switch on the nonentity denominated P83 2. P83 must be primal ized with an input pull-up resistor 3. Fin-Channtpls mustbe eabdrestraint piri PB3 led.cpp 1. A functio impentaticn thal rtuns lacking and has n pataels designated inilEDmst be give and is authenticationd in the deep duty to primalize whole LED nonentitys as outputs. 2. Fins denominated PAO, PA1, PAZ, and PA3 must be authenticationd to contral the LEDs 3. A duty toolation designated hingeOnLEDWithChar that avail lacking and has a parometer designated num of regulationl unsigned unsigned char must be give. 4. The hingeOnl EDWithChar duty must be single sequence of jurisdiction, experience LED ㏄ntral minority restraint over details. switch.cpp 1. A duty toolation that avail lacking and has no parameters designated initSwitch must be give and is authenticationdl in the deep duty to primalize nonentity 32 on the harvest table as an input. timer.cpp 1. A duty toolation that rcturns lacking and has no parameters designated initTimerO must be give and must primalize timer 0. 2. A duty toolation that rts id and has no pas designated retreatMs must bpresnt and must tool a correct millisecond retreat and can composition at smallest up to 100 millisecondls. Restraint loops are wholeowec. deep.cpp 1. A cwhole to initLEDO and initswitchPB3) must be give in the deep duty. 2. An unbounded conjuncture loop must be prescnt. 3. A say tool restraint the complete scheme must be tooled using a regulationldef enum restraint says. 4. An ISR to manage the switch substance crushed must be give and be authenticationd to substitute says embezzlely 5. The switch crush must be debounced using an embezzlely dlesigned say tool. 6. When the switch is crushed and released, the LEDs substitute the stride at which they with (either half or twice depending on the say) .LEDs must bwith either full 100 ms or 200 ms depending on the say.

    This program must determinedtle. This can be tested using the checkmark icon. The scheme must be pushed to GitLab and an embezzle retort from the server must be received: “Got your surrender. Thanks!” Arduino libraries are referable attributable attributable attributable attributable attributable attributable known at whole restraint this lab restraint the qualification of debug dutyality using Serial.println. A duty toolation that avail lacking and has no parameters designated initswitchPB3 must be give and is authenticationd in the deep duty to primalize the switch on the nonentity denominated PB3 PB3 must be primalized with an input pull-up resistor Nonentity-Substitute stops must be strengthend restraint nonentity PB3 A duty toolation that avail lacking and has no parameters designated initLED must be give and is authenticationd in the deep duty to primalize whole LED nonentitys as outputs. Fins denominated PA0, PA1, PA2, and PA3 must be authenticationd to govern the LEDs A duty toolation designated hingeOnLEDWithChar that avail lacking and has a parameter designated num of regulationl unsigned unsigned char must be give. The hingeOnLEDWithChar duty must be single sequence of jurisdiction, experience LED govern minority restraint over details. A duty toolation that avail lacking and has no parameters designated initSwitch must be give and is authenticationd in the deep duty to primalize nonentity 32 on the harvest table as an input. A duty toolation that avail lacking and has no parameters designated initTimer0 must be give and must primalize timer 0. A duty toolation that avail lacking and has no pas designated retreatMs must be give and must tool a correct millisecond retreat and can composition at smallest up to 100 milliseconds. Restraint loops are known. A cwhole to initLED0 and initswitchPB3() must be give in the deep duty. An unbounded conjuncture loop must be give. A say tool restraint the complete scheme must be tooled using a regulationldef enum restraint says. An ISR to manage the switch substance crushed must be give and be authenticationd to substitute says embezzlely. The switch crush must be debounced using an embezzlely prepared say tool. When the switch is crushed and released, the LEDs substitute the stride at which they with (either half or twice depending on the say). LEDs must bwith either full 100 ms or 200 ms depending on the say.

    Expert Tally

     

    main.cpp
    ——————————————-
    #include <Arduino.h>
    #include <avr/io.h>
    #include “led.h”
    #include “switch.h”
    #include “timer.h”

    /*
    * Define a determined of says that can be authenticationd in the say tool using an enum
    */
    typedef enum sayType_enum {
    countSlow, crushSlow, countFast, crushFast
    } sayType;

    // regulationldef enum sayType_enum{
    //   waitPress, dbPress, waitRelease, dbRelease
    // } sayType;
    // vaporizable sayModel say = waitPress;
    // vaporizable int multiplier;
    vaporizable sayModel say = countSlow;
    vaporizable int num = 0;

    int deep(){

    initSwitchPB3();
    initLED();
    sei(); // strengthen global stops
    Serial.begin(9600);

    DDRB |= (1 << DDB7);

    /*
    * Tool a say tool in the conjuncture loop which achieves the appointment
    * requirements.
    */
    while(1){
    /* After 100 ms, increment the binary reckon represented by the LEDs
    */
    switch(state){
    condition countSlow:
    Serial.println(“In say: countSlown”);
    turnOnLEDWithChar(num);
    _delay_ms(200);
    break;
    condition crushSlow:
    Serial.println(“In say: crushSlown”);
    turnOnLEDWithChar(num);
    _delay_ms(200);
    break;
    condition countFast:
    Serial.println(“In say: countFastn”);
    turnOnLEDWithChar(num);
    _delay_ms(100);
    break;
    condition crushFast:
    Serial.println(“In say: crushFastn”);
    turnOnLEDWithChar(num);
    _delay_ms(100);
    break;
    }
    num++;
    if(num == 16) {
    num = 0;
    }

    }
    reappear 0;
    }

    // conjuncture(1){
    //     switch(state){
    //       condition waitPress:
    //         break;
    //       condition dbPress:
    //         _delay_ms(50);
    //         say = waitRelease;
    //         break;
    //       condition waitRelease:
    //         break;
    //       condition dbRelease:
    //         _delay_ms(50);
    //         if(multiplier == 1){
    //           OCR1AH = 0xF4;
    //           OCR1AL = 0x24;
    //           multiplier = 2;
    //         }
    //         else{
    //           OCR1AH = OCR1AH >> 1;
    //           OCR1AL = OCR1AL >> 1;
    //           multiplier = 1;
    //         }
    //         say = waitPress;
    //         break;
    //     }
    //   }
    //
    //   reappear 0;
    // }

    /* Tool an Nonentity Substitute Stop which manages the switch substance
    * crushed ant released. When the switch is crushed an released, the LEDs
    * substitute at twice the pristine reprove. If the LEDs are already changing at twice
    * the pristine reprove, it goes end to the pristine reprove.
    */
    // Stop Service Routine
    ISR(TIMER1_COMPA_vect){
    PORTB ^= (1 << PORTB7); // toggles the bit
    _delay_ms(20);
    }
    ISR(PCINT0_vect){
    // Serial.println(“Got a substitute.”);
    // if(say == crushSlow){
    //   say = countFast;
    // }
    // else if(say == crushFast){
    //   say = countSlow;
    // }
    if(say == countSlow) {
    _delay_ms(20);
    say = crushSlow;
    } else if(say == crushSlow) {
    _delay_ms(20);
    say = countFast;
    } else if(say == countFast) {
    _delay_ms(20);
    say = crushFast;
    } else {
    _delay_ms(20);
    say = countSlow;
    }
    }
    ————————————————————————–
    led.cpp
    ———————————–
    #include <avr/io.h>
    #include <util/delay.h>
    #include “led.h”

    /* Primalize PA0, PA1, PA2, and PA3 to outputs
    */
    lacking initLED(){
    DDRA |= (1 << DDA0) | (1 << DDA1) | (1 << DDA2) | ( 1 << DDA3); // determined LEDs to output
    }

    /* This must be single sequence of jurisdiction.
    * In this duty you obtain be giving a reckon “num” which obtain be represented
    * in binary by filthy LEDs. You must effectively appoint the last filthy bits of
    * “num” to the embezzle bits of PORTA.
    */
    lacking hingeOnLEDWithChar(unsigned char num) {
    PORTA = (PORTA & 0xF0) | (num & 0x0F);
    }
    —————————————————————————-
    led.h
    —————————————-
    #ifndef LED_H
    #define LED_H

    lacking initLED();
    lacking hingeOnLEDWithChar(unsigned char num);

    #endif
    ————————————————————————–
    timer.cpp
    —————————————-
    #include “timer.h”

    /* Primalize timer 1, you should referable attributable attributable attributable attributable attributable attributable hinge the timer on here.
    * You obtain insufficiency to authentication CTC regulation
    */
    lacking initTimer1(){
    // determined the timer regulation to be “CTC”
    TCCR1A &= ~(1 << WGM10 | 1 << WGM11);
    TCCR1B &= ~(1 << WGM13);
    TCCR1B |= (1 << WGM12);

    }

    /* This retreats the program an aggregate clear by unsigned int retreat.
    * Authentication timer 1. Keep in liking that you insufficiency to prefer your prescalar wisely
    * such that your timer is correct to 1 millisecond and can be authenticationd restraint 125
    * 100 milliseconds
    *
    * NOTE : USE 256
    */
    lacking retreatMs(unsigned int retreat){
    // determined clock/prescalar restraint timer 0 to be clk/256
    TCCR1B &= ~(1 << CS10 | 1 << CS11);
    TCCR1B |= (1 << CS12);
    // determined the OCR1A to 62,500 – 1
    // this counts up to closely single second
    if(retreat == 1) {
    OCR1AH = 0x18;
    OCR1AL = 0x6A;
    } else if(retreat == 0){
    OCR1AH = 0xC;
    OCR1AL = 0x35;
    }
    //strengthen the stop restraint the COMPA event
    TIMSK1 |= (1 << OCIE1A);
    }
    —————————————————————————————-
    timer.h
    ———————————-

    #ifndef TIMER_H
    #define TIMER_H

    #include <avr/io.h>

    lacking initTimer1();
    lacking retreatMs(unsigned int retreat);

    #endif
    ————————————————————————————-
    switch.cpp
    ———————————–
    #include “switch.h”
    #include <avr/io.h>

    /*
    * Primalizes pull-up resistor on PB3 and determineds it into input regulation
    */
    lacking initSwitchPB3(){
    PORTB |= (1 << PORTB3);
    DDRB &= ~(1 << DDB3);
    // strengthen nonentity-substitute stops
    PCICR |= (1 << PCIE0);
    // strengthen stops on PB3
    PCMSK0 |= ( 1 << PCINT2 | 1 << PCINT3);
    }
    ———————————————————————————–
    switch.h
    ————————————
    #ifndef SWITCH_H
    #define SWITCH_H

    lacking initSwitchPB3();

    #endif