Night rider with modification

Discussion in 'Embedded Systems and Microcontrollers' started by Shagas, Jul 17, 2013.

  1. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    This bunch of code gives me 6 errors upon attempt to compile. Basically it's a 'nightrider' (or whatever the internet calls it nowadays) thingy which flashed LEDs from LED0 to LED7 and then back in a sequential order . The only modification is that I have 2 buttons which manually change the direction of flashing .

    There is a left button and a right button . If the pattern is progressing to the right and I press the left button , then it changes direction to the left starting from the led that was active when I pressed the button . If I press the right button while it's travelling right then nothing happens .
    Vice versa for the other side.

    Code ( (Unknown Language)):
    1.  
    2. #include <avr/io.h>
    3. #include <util/delay.h>
    4.  
    5. int x=0;
    6. int y=0;
    7.  
    8. int roamleft(int x);
    9. int roamright(int y);
    10.  
    11.  
    12. DDRB = 0b11111111;
    13. DDRD = 0b00000011;
    14.  
    15. PORTD = 1<<PIND2 | 1<< PIND3;
    16.    
    17.  
    18.  
    19. int main(void)
    20. {
    21.    
    22.    
    23.  
    24.    
    25.     while(1)
    26.     {
    27.    
    28.         x = roamright(y); // flash to the right  sequentially from LED Y to LED 7
    29.         y = roamleft(x);  // flash  to the left   sequentially from LED X to LED 0
    30.                
    31.     }
    32.    
    33.        
    34. }
    35.  
    36. int roamright(y)
    37. {
    38.     for(x=y;x<8;x++)    // cycles through the LED ports
    39.     {
    40.     PORTB = 1 << x;     // Flash the led
    41.     _delay_ms(100);     // delay before flashing next led
    42.     PORTB = 0;          // Turn LED port off
    43.     if(bit_is_clear(PIND,2))   // Test if button pressed
    44.     {
    45.         return x;             //break the cycle and call 'roamleft' carrying in the value that the led stopped at.
    46.     }
    47.    
    48.     }
    49.    
    50.     return x;                 // if button is not pressed , then x=7 will be carried into 'roamleft' so it starts to flash to the left sequentially starting from LED 7
    51.    
    52.        
    53.        
    54. }
    55.  
    56. int  roamleft(x)
    57.     {
    58.        
    59.  
    60.     for(y=x;y>=0;y--)
    61.    
    62.     {
    63.        
    64.     PORTB = 1 << y;
    65.     _delay_ms(100);
    66.     PORTB = 0;
    67.    
    68.     if(bit_is_clear(PIND,3))
    69.     {
    70.         return y;
    71.     }
    72.    
    73.    
    74.     }
    75.      return y;
    76.    
    77. }
    78. Select All
    79.  
    Can someone just skim through that and tell me if there are any coarse syntax errors or whatever that cannot be done the way I did it , i'd appreciate it.
    I'm using Atmel studio 6. It tells me I have 6 errors but It doesn't show me where they are.
    Thanks in advance


    I've had some experience in C++ and java in the past but some things confuse me here.
    Why do I see functions declared in some tutorials and others just omit it? Does it depend on the compiler or something?
     
  2. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Hmm apparently I'm supposed to declare the DDRB and D registers in the main function ...
    I thought that i've been doing it outside it all the time .
     
  3. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    works pretty nicely
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ...I tried pointing that out, but my phone kept getting feisty worth me, so I figured someone else could do it.

    You are attempting to set the value of a register, not set a fuse. Modifying register contents is done in the program...

    Glad to hear it's working now...
     
  5. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Well, thanks for the attempt anyway :)
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I think the code is complex and could be simplified a lot.

    Here's one quick option to get the same effect, this is the whole code and needs to go in main();

    Code ( (Unknown Language)):
    1.  
    2. unsigned char right;
    3.  
    4. PORTB = 0x01;
    5. right = 0;
    6. while(1)
    7. {
    8.   Delay_mS(100);
    9.   if(right)
    10.   {
    11.     PORTB = (PORTB >> 1);           // shift right
    12.     if(PORTB == 0x01) right = 0;    // and test for reached the end
    13.   }
    14.   else    
    15.   {
    16.     PORTB = (PORTB << 1);           // shift left
    17.     if(PORTB == 0x80) right = 1;    // and test for reached the end
    18.   }
    19. }
     
  7. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    hmm I cannot see the button inputs in that piece of code .
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Adding in an interrupt that changes the value of the direction flag (right) depending on the button wouldn't be much more work...
     
  9. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Yes , thing is I don't know how to use interrupts yet .
    I want to do one more program that I have in plan with led's using PWM with normal I/0 before I go into using counters and interrupts
     
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You've got a good reason to learn! ;)
     
  11. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Well when I started out , I just started going over all the aspects ofc the MCU without trying them all out in practice (mostly due to crappy tutorials) and I kept getting pissed off at all these features because I didn't know what they were good for .
    I mean why the F is there a PWM mode when I can make PWM with I/0 and delays right??
    Ok .... I know now after MrChips explained but more importantly I know now because when I tried to create certain programs with I/0 I ran into limitations where the concept of ínterrupts started making sense , so I decided to take it step by step
     
Loading...