Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C, probably tables proglem?

Status
Not open for further replies.

konradIC13

Member
Hello,

Im playing with my code for ATmega8 in C, doing some things, now i decided i want to make a program with my LCD module. What i want it to do? Besides of displaying current temperature it will use 4 buttons (up, down, left, right) to display on LCD an "X" sign that will move on LCD according to pushed buttons. In addition to that there will be "obstacles" on LCD and if user will want to move "X" into a spot where obstacle is "X" will stay in same place as before and will not move.

What is working: temperature, displaying and moving X, displaying obstacles. What is not working? Program checks only for first "obstacle" x and y, but does not check for other obstacles which results that you cant go past first obstacle but you can go through all others.

Here is my code with comments
Code:
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "ds18b20.c"
#include "buttons.c"
#include "lcd.c"
volatile uint8_t count=0;
/******************************************************
   Define LCD module connections and DS18B20 connections
   in ds18b20.h and lcd.h, initialize buttons with
   buttons_init()
*******************************************************/

int main(void)
{
	DDRD|=_BV(PD4);
	DDRC|=_BV(PC5);
	PORTD&=~_BV(PD4);
	PORTC&=~_BV(PC5);
	TCCR0=0b00000101;
	TIMSK=0b00000001;
	char UP,DOWN,LEFT,RIGHT;
	int current[2]={0,1}; //current position {x,y}, new is new position {x,y}
	int newc[2]={0,1};  
                                       //0 1
                                       //X Y
        int obstacle [6][2]={7,0, //0
                                         9,1, //1
                                       11,0, //2
                                       12,0, //3
                                       13,0, //4
                                       15,1};//5
	buttons_init();
	lcd_init(LCD_DISP_ON);
	sei();
	while(1)
	{
		UP=(BTUP_PIN&_BV(BTUP_CON));
		DOWN=(BTDOWN_PIN&_BV(BTDOWN_CON));
		LEFT=(BTLEFT_PIN&_BV(BTLEFT_CON));
		RIGHT=(BTRIGHT_PIN&_BV(BTRIGHT_CON));
		lcd_clrscr();
		/*
		according to buttons, increment or decrement {x,y} of new cursor position newc[0] is x and newc[1] is y 
		   X--->
		Y * 0 1 2 3 4 5 6 7 8 9 A B C D E F
		| 0               X       X X X
		V 1                   X           X
		
		if new position will be out screen (you are in upper row and go up) cursor apears in lower, 
		if you are in top right position and go right you end in leftmost position
		*/
		if(UP==0)
		{
			PORTD|=_BV(PD4);
			++newc[1];	
			if (newc[1]>1) newc[1]=0; 
		}
		if(DOWN==0)
		{
			PORTD|=_BV(PD4);
			--newc[1];
			if (newc[1]<0) newc[1]=1;
		}
		if(LEFT==0)
		{
			PORTD|=_BV(PD4);
			--newc[0];
			if (newc[0]<0) newc[0]=15;
		}
		if(RIGHT==0)
		{
			PORTD|=_BV(PD4);
			++newc[0];
			if (newc[0]>15) newc[0]=0;
		}
		if((UP&&DOWN&&LEFT&&RIGHT)!=0)
		{
			PORTD&=~_BV(PD4);
		}
		lcd_puts(tempBuf);
		/*
		Check if new position coordinates stored in new{x,y} match any coordinates of obstacle
		if new coordinates location dont match location of any obstacle cursor goes there and current 
		coordinates are updated with new ones
		
		if coordinates of new location match any of obstacle location (both x and y match) 
		cursor stays in current position (unchanged) and new position values are updated with current position
		*/
		for (int i=0;i<6;i++)
		{
			if(((newc[0]==obstacle[i][0])&(newc[1]==obstacle[i][1])!=0))
			{
				lcd_gotoxy(current[0],current[1]);
				newc[0]=current[0];
				newc[1]=current[1];
			}
			else
			{
				
				lcd_gotoxy(newc[0],newc[1]);
				current[0]=newc[0];
				current[1]=newc[1];
			}
		}		
		lcd_putc('X');
		for(int i=0;i<6;i++)
		{
			lcd_gotoxy(obstacle[i][0],obstacle[i][1]); //fill LCD with "obstacles"
			lcd_putc(0xFF);
		}
		_delay_ms(100);
	}
} 

ISR(TIMER0_OVF_vect)
{
	TCNT0=0b00111100;
	count++;
	if(count>19)
	{
		ds_conv();
		count=0;
	}
}

Just as i said, it checks only if new coordinates match coordinates of first obstacle at position x=7 (obstacle[0][0]) y=0 (obstacle[0][1]) and if they match it wont allow X to move but does not check if coordinates of new position match other obstacles coordinates.

Any help appreciated, it may be some problem i dont know about or something simple i didnt noticed, thats why i decided to post it here, fresh look always helps :)

If you have more questions about code ill answer ASAP
 
Last edited:
UH, it happened again, i think i know what is the problem, and noticed it just after looked up code here after posting it, i think this causes problem:
Code:
if(((newc[0]==obstacle[i][0])&(newc[1]==obstacle[i][1])!=0))
			{
				lcd_gotoxy(current[0],current[1]);
				newc[0]=current[0];
				newc[1]=current[1];
			}
			else
			{
 
				lcd_gotoxy(newc[0],newc[1]);
				current[0]=newc[0];
				current[1]=newc[1];
			}
Is it this? in first iteration IF checks for coordinates of first obstacle, if they dont match else after it moves cursor there and already updates old coordinates with new coordinates and when second and next iterations check if postion matches another obstacles sure, first if finds that it may match one of them and puts cursor into current postion (not new) but its already overvritten with new coordinates so cursor stays in same position?
 
I just updated my code, it turned out just like i said before
I changed this:
Code:
...
for (int i=0;i<6;i++)
		{
			if(((newc[0]==obstacle[i][0])&(newc[1]==obstacle[i][1])!=0))
			{
				lcd_gotoxy(current[0],current[1]);
				newc[0]=current[0];
				newc[1]=current[1];
			}
			else
			{
				
				lcd_gotoxy(newc[0],newc[1]);
				current[0]=newc[0];
				current[1]=newc[1];
			}
		}		
		lcd_putc('X');
...

to this:
Code:
...
for (int i=0;i<6;i++)
		{
			if(((newc[0]==obstacle[i][0])&(newc[1]==obstacle[i][1])!=0))
			{
				collision=true;
			}
		}
		if (collision==true)
		{
			lcd_gotoxy(current[0],current[1]);
			newc[0]=current[0];
			newc[1]=current[1];
			collision=false;
		}
		else
		{
			lcd_gotoxy(newc[0],newc[1]);
			current[0]=newc[0];
			current[1]=newc[1];
		}
		lcd_putc('X');
...

and it works now, it checks for all obstacles and detecting collision sets collision as true then after checking if collision happened at least once and if yes position is unchanged

but please, tell me more about those arrays are they initialised wrong? Because if yes i have to fix it, even if it works now it can stop in some conditions and i dont want it to happen, also ill be happy for any advices if something can be done better.

i found that way (one i used) to initialize 2d array in AVR-GCC tutorial about arrays
example was like this:
Code:
/* Definition with initialisation of d2 array */
 char inna_tablica[][2] = { 0x31,0x02, 0xf1,0x02, 0x41,0xf2, 0xf1,0xc2, 0x11,0xa2 };
it seems to work but the method that is in your links looks more user friendly, its easier to check elements of it and just tested it and i like it more
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top