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
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
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: