Here what I am trying to do is control servo motor with a pottentiometer and record its movement using analog to digital converter and than play. I started with just moving the servo. To see which angle is the servo on, I print the angle calculation to lcd screen. I do this with this code and it works fine:
void servo(unsigned long int b,c)
{
output_high(pin_d0);
delay_us(550+b);
output_low(pin_d0);
delay_us(19450-b);
printf(lcd_putc,"\fVal = %u DEGREE",c);
}
int n=0;
int a=0;
while(1)
{
i=read_adc();
degree=i*2/11.1;
i=i*2;
servo(i,degree);
}
but problem starts when i try to record and play the movement. Here what I did when first button pressed record movements and when second button pressed play the record.
while(1)
{
while(input(pin_a1)){ //while button 1 pressed record
i=read_adc();
degree=i*2/11.1;
i=i*2;
servo(i,degree);
write_eeprom(n,i);
n++;
delay_ms(1000);
if(input(pin_a2)) //if button 2 pressed go to play
delay_ms(50);
break;
}
while(input(pin_a2)){ //play eeprom
i=read_eeprom(a);
degree=i*2/11.1;
servo(i,degree);
delay_ms(1000);
a++;
if(a==10)
a=0;
}
}
First I press and hold first button to record values and I record 10 values with a delay of 1000ms and lcd screen shows the right angles. but after I press and hold second button servo is not moving just lcd is active. and LCD shows wrong angles. For example I record 160 , 50 , 45,... it plays 20,7,10... I couldn't find any mistake. If anybody can help I will be appreciated. Thanks for reading.
Here my real code is
#include <16f877.h>
#device adc=10
#FUSES XT
#FUSES NOWDT
#FUSES NOPUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES NOCPD
#FUSES NOWRT
#define use_portb_lcd TRUE
#use delay (clock=4000000)
#include <lcd.c>
unsigned long int i=0,degree=0;
int n=0;
int a=0;
void servo(unsigned long int b,c)
{
output_high(pin_d0);
delay_us(550+b);
output_low(pin_d0);
delay_us(19450-b);
printf(lcd_putc,"\fVal = %u DEGREE",c);
}
void main()
{
set_tris_a(0x01);
set_tris_d(0x00);
output_d(0x00);
output_b(0x00);
setup_adc(adc_clock_div_32);
setup_adc_ports(AN0);
set_adc_channel(0);
lcd_init();
while(1)
{
while(input(pin_a1)){
i=read_adc();
degree=i*2/11.1;
i=i*2;
servo(i,degree);
write_eeprom(n,i);
n++;
delay_ms(1000);
if(input(pin_a2))
delay_ms(50);
break;
}
while(input(pin_a2)){
i=read_eeprom(a);
degree=i*2/11.1;
servo(i,degree);
delay_ms(1000);
a++;
if(a==10)
a=0;
}
}
}
PROBLEM SOLVED I have been trying to write on eeprom values more then 8bit(256) which is "i".