How to work with arrays ( Mikroc )

Discussion in 'Programmer's Corner' started by LETITROLL, May 1, 2014.

  1. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    Hi all of you :) .



    I have a hard time using this program witch consists of controlling the lightning of 4 leds with the press of a button , so when a button is pressed first led goes on and with the next press its the turn of the second one to light up and so on in a loop .

    Unfortunatly the program compiles very well but fails when i simulate the uC on ISIS , am by the way using the PIC16F84A , and using porta.b0 as input for the button , portb.b0 to portb.b3 as outputs for the 4 leds .

    Here is the program script :

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4. #define start porta.b0
    5. #define led1  portb.b0
    6. #define led2  portb.b1
    7. #define led3  portb.b2
    8. #define led4  portb.b3
    9.  
    10.  
    11. int Leds[6]= {0x00,0x01,0x02,0x04,0x08};
    12. int i=0 ;
    13.  
    14.  
    15. void main() {
    16.  
    17.  
    18. porta=0x00;
    19. portb=0x00;
    20. trisa=0xff;
    21. trisb=0x00;
    22. i=1;
    23.  
    24.  
    25. if( start==1){
    26.  
    27.  
    28. delay_ms(200);
    29. portb= Leds[i];
    30. i++;
    31. delay_ms(500);
    32.  if(i==6){
    33.  i=0;
    34.  }
    35. }
    36. }
    37.  
    38. [/i]
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    There is no loop in your code so the start button will only get checked once before coming to the end of your program. It will then probably continue executing the rest of the code memory before coming back round to your code again. Enclosing the main part of your code in a loop should improve things. I've not compiled this code but it should be ok :)

    Code ( (Unknown Language)):
    1.  
    2. i=1;
    3.  
    4. do
    5. {
    6.      if( start==1){
    7.           delay_ms(200);
    8.           portb= Leds[i];
    9.           I++;
    10.           delay_ms(500);
    11.           if(i==6){
    12.                i=0;
    13.           }
    14.      }
    15. } while (1);
    16. [/i]


    The problem your code has at the moment is that if you keep the button pressed the incrementing will happen repeatedly.

    Try changing the button check so that it only responds to a leading edge of the button push. The 200ms and 500ms delays should mean that there's no switch bounce problem.
     
  3. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    Hi thanks , this thread can be deleted , i just double threaded by mistake .
     
Loading...