Good day to all,
Please i need your support on how to solve this problem.I am using the Trackbar to control the brightness of Leds but the value of the Trackbar I am getting in(%) does not corespond to the led's brightness.For example if i move the Trackbar slider to say 50%,the Oscilloscope will show something 30%,if i move the slider further say to 80%,the Oscilloscope may show something like 50% or 60% or at times the PWM Signal will disappear.I have gone through my codes but i was unable to locate the error.My assumption is that the calculation and conversion i made contain
error but i do not know how to figure it out.i will highly appreciate it if somebody could put me through on how to sort the problem .Below are my codes:
C# CODE://this is code the Graphic User Interface uses in order to send data to the microcontroller
Code:
        private void button3_Click(object sender, EventArgs e)
        {
            
            string  dataToWrite = "a" ;
            UInt32 numBytesWritten = 1;
            myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string dataToWrite = "b";
            UInt32 numBytesWritten = 1;
            myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);

        }


        private void trackBar1_Scroll(object sender, EventArgs e)
        {  
            UInt32 numBytesWritten = 1;

            int percent = (int)(((double)trackBar1.Value / (double)trackBar1.Maximum) * 100);
            label2.Text = (percent.ToString()) + "%";
 
             if(percent <= 9)//this IF condition send string from "0" to "9"
               {
                   chdata[0] = "1";
                   myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
                   chdata[0] = Convert.ToString(percent);
                   myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
               }
               else //this ELSE  condition sends string from "10" to "99"
               {
                   chdata[0] = "2"; // The  "2" here  represents the length of the string being sent
                   myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
                   dgdata[0] = percent/ 10;
                   chdata[0] = Convert.ToString(dgdata[0]);
                   myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
                   dgdata[1] = percent % 10;
                   chdata[0] = Convert.ToString(dgdata[1]);
                   myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
              
               }
         
            
          }
C-CODE SECTION://This code is used by the microcontroller to process the data it receives from the Graphic User Interface.
Code:
int wait_command(void)

{
        unsigned char  InData;
	unsigned char  InBuffer[50];
	unsigned int   length;
        unsigned int   trackvalue=0;
	unsigned short pwr_led1=655;
        int   i;
        int   length;
	
	InData = USART_Receive();
	
	if(InData =='a' || InData =='b' )
	{
	    InBuffer[0]= InData;
	}
	
	else if(InData >='0' || InData <= '9'  )
	{
		length = InData - '0';//lenght variable here is used to get the actual value sent from GUI.Lenght is 1 for string from "9" to "9" while it is 2 for string from "10" t "99"
		for (i = 0; i<length; i++)
		{
			trackvalue *=10;
			trackvalue += USART_Receive()- '0';
			pwr_led1= 655 * trackvalue;
			_delay_ms(200);
			
			
		}	  
	}
	
	else  
	{
	   // do nothing	
	}

   switch(InBuffer[0])
	{
				
	 case 'a':
	 if (led1_on == 1) led1_on=0; else led1_on=1;
	 break;
	 case 'b':
	 if (led2_on == 1) led2_on=0; else led2_on=1;
	 break;
         default:
	 break;	

	}  // switch
	
	SetRegister(pwr_led1*spur1_on,pwr_led2*spur2_on);
	GenerateLatch();
 }
I would be very glad if somebody could guide me on how to solve this problem.Thank you all for the usual support.
Best regards.
Firstoption.