Thread: dont know where im going wrong

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    157

    dont know where im going wrong

    hi , i'm trying to make a code that converts an analogue value into an angle in degree's,using given values for an angle with its corresponding voltage. i keep getting -1 one as my answer regardless of what i set my input voltage to be. please help.thanks in advance

    Code:
    int conversion_select(float voltage){ 
         int select;
       
       if(0.780<voltage<=0.800)
           select =0; 
       else if(0.8<voltage<=0.845) 
           select =1;
       else if(0.845<voltage<=1) 
           select =2; 
       else if(1<voltage<=1.69) 
           select =3;   
       else 
           select =4;
    
    
          
        return select;
    }
    
    
    
    
    /*
    * Convert_to_degree() - converts the analogue value into degree's 
    *
    */
    float Convert_to_degree(int A_value){
    
    
       float voltage[10]={0.78,0.8,0.845,0.920,1,1.13,1.26,1.42,1.55,1.69};
       float Angle[10]={0,10,20,30,40,50,60,70,80,90};
       float degree,gradient,input_voltage;
       int select;
       
       
       input_voltage =  (float)(A_value - 6)/(float)(115) ;
       select = conversion_select(input_voltage) ;     
       
       delay(20000);
       
       printf("%f",input_voltage);
     
      switch(select){
        
        case 0:
      {
          
       gradient=(float)(Angle[1]-Angle[0])/(float)(voltage[1]-voltage[0]);
       degree = gradient*(input_voltage-voltage[0])+Angle[0];
       
          
      } 
        case 1:
      {
      
       gradient=(float)(Angle[2]-Angle[1])/(float)(voltage[2]-voltage[1]);
       degree = gradient*(input_voltage-voltage[1])+Angle[1];
       
          
      } 
        case 2:
      {
       
       gradient=(float)(Angle[4]-Angle[2])/(float)(voltage[4]-voltage[2]);
       degree = gradient*(input_voltage-voltage[2])+Angle[2];
       
          
      } 
        case 3:
      {
       
       gradient=(float)(Angle[9]-Angle[4])/(float)(voltage[9]-voltage[4]);
       degree = gradient*(input_voltage-voltage[4])+Angle[4];
       
          
      } 
        case 4:
        
        degree = -1;
          
      }
        return degree ;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    < than and <= operations are binary, so expressions like "0.780<voltage<=0.800" don't test if voltage is between 0.78 and 0.8.

    Instead, your program will evaluate the expression "0.780 < voltage" first. That gives a value of zero (if voltage is not greater than 0.78) or 1 (otherwise). That 0/1 result is then compared to see if it is less than or equal to 0.8.

    More concisely, the expression "0.780<voltage<=0.800" is equivalent to "(0.780<voltage)<=0.800". Note the brackets - they are important.

    To do what you probably intend, you need to do "(0.78 < voltage) && (voltage <= 0.800)". The && is the logical AND operator. Strictly speaking, the brackets in this form are optional (since && has lower precedence than comparison operators), but they do help with readability.

    Or, to summarise, a C compiler is not a mathematician.
    Last edited by grumpy; 10-06-2013 at 06:33 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    thanks,had completely forgotten about that part...too long without programing
    thanks alot

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    although it still gives me false values for the angle,(always -1)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    So what value of A_value are you providing to Convert_to_degree()? Lemme guess ... you're picking a few small values.

    Any value less than 89 or any value exceeding 200 ill cause your conversion_select() to return 4 .... which in turn will cause your Convert_to_degree() to return -1.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    have a voltage module as an input device to the program,using an IDE by the way,had forgoten to brreak after each case statement.problem solved.thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I dont know what is wrong???
    By amroto in forum C Programming
    Replies: 3
    Last Post: 05-18-2012, 06:19 AM
  2. dont know what im doing wrong... array
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2009, 12:23 PM
  3. dont know whats wrong
    By otchster in forum C Programming
    Replies: 2
    Last Post: 11-02-2005, 11:14 AM
  4. i dont know what is wrong with it (help plz)
    By abuna in forum C++ Programming
    Replies: 16
    Last Post: 09-08-2005, 12:03 PM
  5. help......i dont understand what i am doing wrong?
    By Grifftt in forum C Programming
    Replies: 2
    Last Post: 10-18-2002, 11:37 PM