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

write the code below please, same Structure and same steps please C It should be

ID: 3586121 • Letter: W

Question

write the code below please, same Structure and same steps please C

It should be noted that inputs, especially those driven by devices, can change very quickly and therefore need to be serviced quickly. Microprocessor systems have developed hardware known as interrupts for handling such inputs. From our standpoint an interrupt is actually a function that runs anytime an input or signal changes or is in a given state. The application that we will be employing interrupts for is the input from the knob of our encoder. A shaft encoder uses two signals, also known as channels, to indicate the motion of a shaft as well as its direction. The signals from an encoder are shown in Figure 6-1. In this graph it can be seen that only one of the signals changes at a time. Also when moving in a ClockWise (CW) or CounterClockWise (CCW) direction, the order in which the signals change is different. Note we will arbitrarily think of CW as the positive direction and CCW as the negative direction. CW Input B Change Input A Change Figure 6-1. Encoder Signal with Transitions Marked. The encoder signals A and B are connected to pins 2 and 3 of the Arduino Nano, respectively. Pins 2 and 3 were chosen since they are attached to interrupts 0 and 1 respectively. In this way a function known as an Interrupt Service Routine (ISR) can be established that will be run cach time one of the signals changes. The question is, What should the routine do in order to keep track of the motion of the shaft? Note that if we move CW and examine the transition events only on channel A, channel A always transitions into the opposite value of channel B. However, if we move CCW channel A transitions into the same value as channel B. Examining the transitions of B, we see that the opposite is true. Thus once a transition occurs in one of the signals, an interrupt routine will run, and depending upon the state of the two inputs (immediately after the transition), it will increments or decrements a variable indicating the shafts position.

Explanation / Answer

#include <Arduino.h>

unsigned int encoderPosition = 0; // variable to track encoder position
//
unsigned int pinA = 2; // define the IO pins
//
unsigned int pinB = 3;
//
unsigned int stateA = 0; // this will hold the state of A
//
unsigned int stateB = 0;
//
void MonitorA()
{
    //
    stateA = !stateA; // this interrupt was triggered because there was a change in signalA, so complement state of pinA
    //
    if(stateA == stateB)
        encoderPosition++; // increment encoder pos
    else
        encoderPosition--;
}
//
void MonitorB()
{
    //
    stateB = !stateB;
    //
    if(stateB == stateA)
        encoderPosition--;
    //
    else
        encoderPosition++;

}
//
void setup()
{
//
pinMode(pinA, INPUT); // configure as input
//
pinMode(pinB, INPUT);
//
attachInterrupt(INT0, MonitorA, CHANGE); // attach an ISR with interrupt 0, which is associated with pin 2, mode is CHANGE
//
attachInterrupt(INT1, MonitorB, CHANGE);
//
// read initial state of both inputs at the start
stateA = digitalRead(pinA);
//
stateB = digitalRead(PINB);
//
}
//
void displayLCD(unsigned int encoderPosition)
{
    // to be implemented
}
//
void loop()
{
//
while(1)
    {
        //
        displayLCD(encoderPosition); // show
        //
        delay(100);
    }
}

//