NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Need help with multiple switched inputs for a desired output.

June 15, 2012
by Bamftoy
Bamftoy's Avatar

Ok, so I'm trying to make it so that a pushbutton will light up 2 LEDs sequentially and turn off the LEDs on the third button press. While, if at ANY point in the program 2 switches are engaged, and the button is pressed, 3 LEDs will light and stay on until the button is released. After the button is released the first 2 LEDs should stay on until the button is pressed to turn them off again.

Also while compiling I get these warnings test.c: In function 'main': test.c:56: warning: 'sc' is used uninitialized in this function test.c:31: warning: 'sb' may be used uninitialized in this function

Thanks for any help, Josh

here's my code:

#define F_CPU 14745600

#include <avr/io.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

int main() {

    // Set outputs for leds
    DDRC |= (1<<PC5); 
    DDRC |= (1<<PC4);
    DDRC |= (1<<PC3);

    // set PD4 as pushbutton input
    DDRD &= ~(1<<PD4); 
    PORTD |= (1<<PD4); // turn on pull up resistor

    // variable to represent pushbutton input
    uint8_t PB;
    uint8_t j;

    // set PB2 as switch1 input
    DDRB &= ~(1<<PB2);
    PORTB |= (1<<PB2);

    // variable to represent switch1 input
    uint8_t B;
    uint8_t sb;

    // set PB3 as switch2 input
    DDRB &= ~(1<<PB3);
    PORTB |= (1<<PB3);

    // variable to represent switch2 input
    uint8_t C;
    uint8_t sc;

    for(j = 0; j <= 1; j++){
    // Turn off relays
    PORTC &= ~(1<<PC5);
    PORTC &= ~(1<<PC4);
    PORTC &= ~(1<<PC3);
    }

    j=0; // begin with relays OFF

    while(1) {

    PB = (PIND & (1<<PD4)) >> PD4;
    B = (PINB & (1<<PB2)) >> PB2; 
    C = (PINB & (1<<PB3)) >> PB3;

    uint8_t ALL = j + sb + sc;

    if (PB == 0){

    j = j + 1;     //if button pressed, increase count by one
    if (j >= 3 )   //set 2 as highest allowed count, and if i is equal or greater than 3,
    j = 0;         //count loops back to zero
    delay_ms(300); // switch "debounce"

    }

    if (B == 0) {
    sb = sb + 1;
    if (sb >= 2 )
    sb = 0;
    }

    if (sc == 0) {
    sc = sc + 1;    
    if (sc >= 1) 
    sc = 0;
    }

    if ( j == 0 ){
    // ALL LEDs OFF 
    PORTC |= (1<<PC5); 
    PORTC |= (1<<PC4);
    PORTC |= (1<<PC3);
    }

    if ( j == 1 ){
    // LED1 on
    PORTC &= ~(1<<PC5);

    }

    if ( j == 2 ){
    // LED1&2 on
    PORTC &= ~(1<<PC5);
    PORTC &= ~(1<<PC4);
    }

    if ( ALL == 3 ){
        // All LEDs on
        PORTC &= ~(1<<PC5);
        PORTC &= ~(1<<PC4);
        PORTC &= ~(1<<PC3);
        j = 2;
    }

    }

    return 0;

}
June 15, 2012
by pcbolt
pcbolt's Avatar

Josh -

I've read contrasting information on this subject. Some say all variables will get initiallized to 0 automatically, some say it is best to leave them uninitialized, some say you should explicitly initialize all variables. By initializing, I mean setting them to some value when you declare them.

To stop the compiler warning, just change line 30 to:

uint8_t sp = 0;

Same with line 38. The compiler won't warn about the other variables since they get assigned a value before they are used in any equations. "sp" and "sb" are used before they are assigned any value.

June 15, 2012
by BobaMosfet
BobaMosfet's Avatar

It is a fact, that in any given c compiler, it does not initialize variables to a clean state for you- it simply allocates memory to them, and whatever happened to be in that memory prior to the allocation to the variable, will still be there.

You should always initialize your variables to a known state. You should always set pointer variables to NULL when done with them. Same with any deallocations you make- they should be NULLed. This way, if anything attempts to use them, you hit zero-page and it crashes fairly quickly and definitively. Most of the time.

Good practice, following these two things will save you loads of problems.

BM

June 15, 2012
by pcbolt
pcbolt's Avatar

Boba -

I agree. Despite what I have read, I still don't trust the compiler or platform to initialize variables automatically to 0.

The Win-AVR manual says otherwise...see This FAQ for a discussion on this subject.

June 15, 2012
by Bamftoy
Bamftoy's Avatar

Thanks for the help! Warnings are now gone.

Anyone have any suggestions for it to read the 2 switches and 1 pushbutton to goto:

if ( ALL == 3 ){
        // All LEDs on
        PORTC &= ~(1<<PC5);
        PORTC &= ~(1<<PC4);
        PORTC &= ~(1<<PC3);
        j = 2;

Thanks, josh

Post a Reply

Please log in to post a reply.

Did you know that you can follow NerdKits on Facebook, YouTube, and Twitter? Learn more...