Search code examples
cavrservo

C what is wrong with function?


Hi i have problem with function to control my 4servos. I want to take this code to function but it's not working.

volatile float servo1;

            variable=  uart_getchar();
            _delay_ms(100);
            variable=variable/10;
            servo1=variable;
            sprintf(bufor,"Servo_1= %4.1f\n",servo1);
            uart_puts(bufor);

when this code is not in function everything is okay, servo works good. Problem is when i do this:

void get(float Servo, char Number)
{
            variable=  uart_getchar();
            _delay_ms(100);
            variable=variable/10;
            Servo=variable;
            sprintf(bufor,"Serwo_%c= %4.1f\n",Number,Servo);
            uart_puts(bufor);
}

and when i call get(servo1,'1');servo stayed in the same place all the time.. any idea what is wrong??


Solution

  • If you want to change a variable you passed to a function, you must use pointers.

    Basically it's used like this:

    void f(int* x){
      *x = 5;
    }
    
    int main() {
      int y = 7;
      f(&y);
      printf("%i\n", y);
      return 0;
    }
    

    In short, & get the address of the variable and * get the value at the address