Why is OCR1B values are not stored

atmegaavrpwmservo

I have following program to control two servo motors:

#include <avr/io.h>
#include <avr/delay.h>

void main()
{  
  int x,i,j,t=0,y=0,n=0;
  DDRA=0b11111111;
  DDRC=0b11111111;
  DDRB=0b00001111;

  //Configure TIMER1
  TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);         //NON Inverted PWM
  TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10);  //PRESCALER=64 MODE 14(FAST PWM)

  ICR1=2499;  //fPWM=50Hz (Period = 20ms Standard).

  DDRD|=(1<<PD4)|(1<<PD5);    //PWM Pins as Out

  OCR1A=65;
  OCR1B=50;

  unsigned int k[1],z[1];

  while (1) {
    for (i=0;i<=4;i++) { //for keypad
      PORTB=0xf7>>i| 0xf0;
      x=PINB;

      if (x==0b11101110) {  //key 1 for moving motor 1
        OCR1A=OCR1A+1;
        _delay_ms(100);
      } else if (x==0b11011110) { //key 2 for moving motor 2
        OCR1B=OCR1B+1;
        _delay_ms(100);
      } else if(x==0b10111110) {    //key 3 for storing value of OCR1A
        k[t]=OCR1A;
        t++;
        _delay_ms(1000);
        _delay_ms(1000);              
      } else if (x==0b11101101) {    //key 4 for storing value of OCR1B
        z[n]=OCR1B;
        n++;
        _delay_ms(1000);
        _delay_ms(1000);
      } else if (x==0b11011101) {   //key 5 for moving motor from k[0] to k[1]
        OCR1A=k[0];
        Wait();
        for(OCR1A=k[0];OCR1A<=k[1];OCR1A++) _delay_ms(100);
        _delay_ms(1000); 
      } else if (x==0b10111101) {    //key 6 for moving motor from z[0] to z[1]
        OCR1B=z[0];
        Wait();
        for(OCR1B=z[0];OCR1B<=z[1];OCR1B++) _delay_ms(100);
        _delay_ms(1000);
      }
    }
  }
}

I have 4*3 Keypad connected port B. Both motors work properly through keys, but while storing only values of OCR1A are stored properly and somehow OCR1B values are not stored properly. Is there any difference between functioning of OCR1A and OCR1B?

After moving motors I am storing the values of OCR1A and OCR1B which represent their current positions. I am storing those values in array (k[] and z[]). Keys 5 and 6 are used to access the values stored earlier so that I can move the motors between the two positions.

Best Answer

You have declared the arrays k[] and z[] to have just one element each. Therefore, it is likely that the values you write to k[] are overwriting the values for z[], and the values for z[] are getting clobbered by other variables.