Skip to main content
added 160 characters in body
Source Link
Roger Rowland
  • 2k
  • 4
  • 21
  • 36

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put these lines outside your loop
uint8_t uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc. 
// use one variable for each input, or if memory is tight, you could
// alternatively use one byte per 8 pins, but this more "wasteful"
// version is easier to understand the principle

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uLastRA6State)
    {
        // save current state
        uLastRA6State = uLastRA6State ? 0 : 1;

        // output change
        if (uLastRA6State)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs, e.g.
    if (PORTAbits.RA7 != uLastRA7State)
    {
        // ... similar to above, for RA7
    }
}

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put these lines outside your loop
uint8_t uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc. 
// use one variable for each input

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uLastRA6State)
    {
        // save current state
        uLastRA6State = uLastRA6State ? 0 : 1;

        // output change
        if (uLastRA6State)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs, e.g.
    if (PORTAbits.RA7 != uLastRA7State)
    {
        // ... similar to above, for RA7
    }
}

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put these lines outside your loop
uint8_t uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc. 
// use one variable for each input, or if memory is tight, you could
// alternatively use one byte per 8 pins, but this more "wasteful"
// version is easier to understand the principle

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uLastRA6State)
    {
        // save current state
        uLastRA6State = uLastRA6State ? 0 : 1;

        // output change
        if (uLastRA6State)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs, e.g.
    if (PORTAbits.RA7 != uLastRA7State)
    {
        // ... similar to above, for RA7
    }
}
added 310 characters in body
Source Link
Roger Rowland
  • 2k
  • 4
  • 21
  • 36

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put thisthese linelines outside your loop
uint8_t uInput1uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc. 
// use one variable for each input

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uInput1uLastRA6State)
    {
        // save current state
        uInput1uLastRA6State = uInput1uLastRA6State ? 0 : 1;

        // output change
        if (uInput1uLastRA6State)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs, e.g.
    if (PORTAbits.RA7 != uLastRA7State)
    {
        // ... similar to above, for RA7
    }
}

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put this line outside your loop
uint8_t uInput1 = 0;               

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uInput1)
    {
        // save current state
        uInput1 = uInput1 ? 0 : 1;

        // output change
        if (uInput1)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs
}

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put these lines outside your loop
uint8_t uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc. 
// use one variable for each input

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uLastRA6State)
    {
        // save current state
        uLastRA6State = uLastRA6State ? 0 : 1;

        // output change
        if (uLastRA6State)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs, e.g.
    if (PORTAbits.RA7 != uLastRA7State)
    {
        // ... similar to above, for RA7
    }
}
added 98 characters in body
Source Link
Roger Rowland
  • 2k
  • 4
  • 21
  • 36

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put this line outside your loop
uint8_t uInput1 = 0;               

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uInput1)
    {
        // save current state
        uInput1 = uInput1 ? 0 : 1;

        // output change
        if (uInput1)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs
}

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

uint8_t uInput1 = 0;

if (PORTAbits.RA6 != uInput1)
{
    // save current state
    uInput1 = uInput1 ? 0 : 1;

    // output change
    if (uInput1)
        putsUART2("Input: 1 ON\n");
    else
        putsUART2("Input: 1 OFF\n");
}

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put this line outside your loop
uint8_t uInput1 = 0;               

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uInput1)
    {
        // save current state
        uInput1 = uInput1 ? 0 : 1;

        // output change
        if (uInput1)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs
}
Source Link
Roger Rowland
  • 2k
  • 4
  • 21
  • 36
Loading