While Loop Problem

Discussion in 'Programmer's Corner' started by DancinNancy, Feb 3, 2009.

  1. DancinNancy

    Thread Starter Member

    Oct 1, 2008
    24
    0
    I am trying to write a program to light LEDs on a guitar neck. With the way i have it wired i need turn on and off LEDs fast so that you can't tell they are blinking. I wrote a loop for one section to light up an A chord on the neck(the S1-S6 and F0s and F2s are defined in the program earlier) but it seems like the loop only goes through once and then waits for the first switch(RD7) to be pressed. When hooked up to a multimeter it only shows whatever the last declarations on that port was (PORTA=0 and PORTF=0 in this case gives zero volts output.) Can anyone tell me where the error is in my code? Thanks

    Also RD6 and RD7 are the switches and i put the delay in so that it wouldn't scroll through the sections too fast.




    while((section==1)&&(scroll==0))
    {
    mPORTDClearBits(BIT_2);
    mPORTDSetBits(BIT_0); // Switch ON LED1

    if(!_RD7){
    TMR1=0;
    while(TMR1<DELAY){
    } //delay loop
    scroll=1;
    } // check switch 2 loop

    if(!_RD6){
    TMR1=0;
    while(TMR1<DELAY){
    } //delay loop
    section=2;
    scroll=0;
    } // check switch 1 loop
    } //Chords Section Loop
    while((section==1)&&(scroll==1)) //CHORD A
    {

    PORTF=S1;
    PORTA=F0;
    PORTF=0;
    PORTA=0;
    PORTF=S2;
    PORTA=F0;
    PORTF=0;
    PORTA=0;
    PORTF=S3;
    PORTA=F2;
    PORTF=0;
    PORTA=0;
    PORTF=S4;
    PORTA=F2;
    PORTF=0;
    PORTA=0;
    PORTF=S5;
    PORTA=F2;
    PORTF=0;
    PORTA=0;
    PORTF=S6;
    PORTA=F0;
    PORTF=0;
    PORTA=0;



    if(!_RD7){
    TMR1=0;
    while(TMR1<DELAY){
    } //delay loop
    scroll=2;
    } //check switch 2 loop

    if(!_RD6){
    TMR1=0;
    while(TMR1<DELAY){
    } //delay loop
    section=2;
    scroll=0;
    } //check switch 1 loop
    } //Chord Section A Chord Loop
     
    Last edited: Feb 4, 2009
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Do the LEDs light and then turn off?

    while(TMR1<DELAY){
    } //delay loop

    When TMR1<DELAY then the program will enter the while but since there is not code inside the program will stack there, is it?
     
  3. DancinNancy

    Thread Starter Member

    Oct 1, 2008
    24
    0
    I am pretty sure it lights but it happens to fast for me to see. I don't actually have it hooked up to LEDs yet just to a multimeter to check the voltages. It seems to just go through the loop once and then wait until the next loop is activated when i press a button. But if i move the if(!_RD6) loops up above the setting of the ports it still works but only once.

    And the delay while loop was working for me with the switches, it slowed down the three indicator leds i have that let me know which section im in. But i just tried putting that delay loop between the declarations like this

    PORTA=S2;
    TMR1=0;
    while(TMR1<DELAY){
    }
    PORTA=0;

    and the right port lights up for S2 but the program seems to stop there instead now. I assume this all has to do with my delay loop. Any recomendations on how to rework that?
     
  4. DancinNancy

    Thread Starter Member

    Oct 1, 2008
    24
    0
    I was wrong i tested the while loop to just blink LEDs and it worked fine. I had to play with that if statement a lot to make it work. Anyone know how to rework that so that if switch two is pressed it goes to one loop and if switch 1 is pressed it goes to another? Any help at all is appreciated.
     
Loading...