Night rider with modification

Thread Starter

Shagas

Joined May 13, 2013
804
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.

Rich (BB code):
#include <avr/io.h>
#include <util/delay.h>

int x=0;
int y=0;

int roamleft(int x);
int roamright(int y);


DDRB = 0b11111111;
DDRD = 0b00000011;

PORTD = 1<<PIND2 | 1<< PIND3;
	


int main(void)
{
   
   	

	
   	while(1)
   	{
	
		x = roamright(y); // flash to the right  sequentially from LED Y to LED 7
		y = roamleft(x);  // flash  to the left   sequentially from LED X to LED 0
		  	   
    }
	
	   	
}

int roamright(y)
{ 
	for(x=y;x<8;x++)    // cycles through the LED ports
	{
	PORTB = 1 << x;     // Flash the led
	_delay_ms(100);     // delay before flashing next led
	PORTB = 0;          // Turn LED port off
	if(bit_is_clear(PIND,2))   // Test if button pressed
	{
		return x;             //break the cycle and call 'roamleft' carrying in the value that the led stopped at.
	}
	
	}
	
	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
	
		
		
}

int  roamleft(x)
	{
		

	for(y=x;y>=0;y--)
	
	{
		
	PORTB = 1 << y;
	_delay_ms(100);
	PORTB = 0;
	
	if(bit_is_clear(PIND,3))
	{
		return y;
	}
	
	
	}
	 return y;
	
}
Select All
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?
 

Thread Starter

Shagas

Joined May 13, 2013
804
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 .
 

tshuck

Joined Oct 18, 2012
3,534
...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...
 

THE_RB

Joined Feb 11, 2008
5,438
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();

Rich (BB code):
unsigned char right;

PORTB = 0x01;
right = 0;
while(1)
{
  Delay_mS(100);
  if(right)
  {
    PORTB = (PORTB >> 1);           // shift right
    if(PORTB == 0x01) right = 0;    // and test for reached the end
  }
  else     
  {
    PORTB = (PORTB << 1);           // shift left
    if(PORTB == 0x80) right = 1;    // and test for reached the end
  }
}
 

Thread Starter

Shagas

Joined May 13, 2013
804
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();
hmm I cannot see the button inputs in that piece of code .
 

tshuck

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

Thread Starter

Shagas

Joined May 13, 2013
804
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
 

Thread Starter

Shagas

Joined May 13, 2013
804
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
 
Top