Wrong trackbar value when using it to control led brightness.

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
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 like 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

C#:
[TABLE]
[TR]
[TD="class: content"]private void button3_Click(object sender, 
EventArgs e)
[/TD]
[/TR]
[/TABLE]


[TABLE]
[TR]
[TD="class: number"]02
[/TD]
[TD="class: content"]{ 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]03
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]




[TABLE]
[TR]
[TD="class: number"]05
[/TD]
[TD="class: content"]    UInt32 numBytesWritten = 1; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]06
[/TD]
[TD="class: content"]    myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]07
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]08
[/TD]
[TD="class: content"]} 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]09
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]10
[/TD]
[TD="class: content"]private void button4_Click(object sender, EventArgs e)
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]11
[/TD]
[TD="class: content"]{ 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]12
[/TD]
[TD="class: content"]    string dataToWrite = "b"; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]13
[/TD]
[TD="class: content"]    UInt32 numBytesWritten = 1; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]14
[/TD]
[TD="class: content"]    myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]15
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]16
[/TD]
[TD="class: content"]} 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]17
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]18
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]19
[/TD]
[TD="class: content"]private void trackBar1_Scroll(object sender, EventArgs e)
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]20
[/TD]
[TD="class: content"]{   
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]21
[/TD]
[TD="class: content"]    UInt32 numBytesWritten = 1; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]22
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]23
[/TD]
[TD="class: content"]    int percent = (int)(((double)trackBar1.Value / (double)trackBar1.Maximum) * 100); 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]24
[/TD]
[TD="class: content"]    label2.Text = (percent.ToString()) + "%"; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]25
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]26
[/TD]
[TD="class: content"]     if(percent <= 9)//this IF condition send string from "0" to "9"
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]27
[/TD]
[TD="class: content"]       { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]28
[/TD]
[TD="class: content"]           chdata[0] = "1"; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]29
[/TD]
[TD="class: content"]           myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]30
[/TD]
[TD="class: content"]           chdata[0] = Convert.ToString(percent); 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]31
[/TD]
[TD="class: content"]           myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]32
[/TD]
[TD="class: content"]       } 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]33
[/TD]
[TD="class: content"]       else //this ELSE  condition sends string from "10" to "99"
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]34
[/TD]
[TD="class: content"]       { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]35
[/TD]
[TD="class: content"]           chdata[0] = "2"; // The  "2" here  represents the length of the string being sent
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]36
[/TD]
[TD="class: content"]           myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]37
[/TD]
[TD="class: content"]           dgdata[0] = percent/ 10; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]38
[/TD]
[TD="class: content"]           chdata[0] = Convert.ToString(dgdata[0]); 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]39
[/TD]
[TD="class: content"]           myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]40
[/TD]
[TD="class: content"]           dgdata[1] = percent % 10; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]41
[/TD]
[TD="class: content"]           chdata[0] = Convert.ToString(dgdata[1]); 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]42
[/TD]
[TD="class: content"]           myFtdiDevice.Write(chdata[0], chdata[0].Length, ref numBytesWritten);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]43
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]44
[/TD]
[TD="class: content"]       } 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]45
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]46
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]47
[/TD]
[TD="class: content"]  }
[/TD]
[/TR]
[/TABLE]
C-CODE SECTION://This code is used by the microcontroller to process the data it
receives from the Graphic User Interface.


C#:
[TABLE]
[TR]
[TD="class: content"]int wait_command(void) 
[/TD]
[/TR]
[/TABLE]


[TABLE]
[TR]
[TD="class: number"]02
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]03
[/TD]
[TD="class: content"]{ 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]04
[/TD]
[TD="class: content"]        unsigned char  InData;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]05
[/TD]
[TD="class: content"]    unsigned char  InBuffer[50];
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]06
[/TD]
[TD="class: content"]    unsigned int   length;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]07
[/TD]
[TD="class: content"]        unsigned int   trackvalue=0;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]08
[/TD]
[TD="class: content"]    unsigned short pwr_led1=655;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]09
[/TD]
[TD="class: content"]        int   i; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]10
[/TD]
[TD="class: content"]        int   length; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]11
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]12
[/TD]
[TD="class: content"]    InData = USART_Receive(); 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]13
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]14
[/TD]
[TD="class: content"]    if(InData =='a' || InData =='b' )
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]15
[/TD]
[TD="class: content"]    { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]16
[/TD]
[TD="class: content"]        InBuffer[0]= InData; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]17
[/TD]
[TD="class: content"]    } 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]18
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]19
[/TD]
[TD="class: content"]    else if(InData >='0' || InData <= '9'  ) 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]20
[/TD]
[TD="class: content"]    { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]21
[/TD]
[TD="class: content"]        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"
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]22
[/TD]
[TD="class: content"]        for (i = 0; i<length; i++)
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]23
[/TD]
[TD="class: content"]        { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]24
[/TD]
[TD="class: content"]            trackvalue *=10;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]25
[/TD]
[TD="class: content"]            trackvalue += USART_Receive()- '0'; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]26
[/TD]
[TD="class: content"]            pwr_led1= 655 * trackvalue; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]27
[/TD]
[TD="class: content"]            _delay_ms(200);
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]28
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]29
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]30
[/TD]
[TD="class: content"]        }
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]31
[/TD]
[TD="class: content"]    } 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]32
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]33
[/TD]
[TD="class: content"]    else
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]34
[/TD]
[TD="class: content"]    { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]35
[/TD]
[TD="class: content"]       // do nothing     
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]36
[/TD]
[TD="class: content"]    } 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]37
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]38
[/TD]
[TD="class: content"]   switch(InBuffer[0]) 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]39
[/TD]
[TD="class: content"]    { 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]40
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]41
[/TD]
[TD="class: content"]     case 'a':
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]42
[/TD]
[TD="class: content"]     if (led1_on == 1) led1_on=0; else led1_on=1;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]43
[/TD]
[TD="class: content"]     break; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]44
[/TD]
[TD="class: content"]     case 'b':
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]45
[/TD]
[TD="class: content"]     if (led2_on == 1) led2_on=0; else led2_on=1;
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]46
[/TD]
[TD="class: content"]     break; 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]47
[/TD]
[TD="class: content"]         default: 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]48
[/TD]
[TD="class: content"]     break;  
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]49
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]50
[/TD]
[TD="class: content"]    }  
// switch 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]51
[/TD]
[TD="class: content"][/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]52
[/TD]
[TD="class: content"]    SetRegister(pwr_led1*spur1_on,pwr_led2*spur2_on); 
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]53
[/TD]
[TD="class: content"]    GenerateLatch();
[/TD]
[/TR]
[/TABLE]



[TABLE]
[TR]
[TD="class: number"]54
[/TD]
[TD="class: content"] }
[/TD]
[/TR]
[/TABLE]
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.
 
Top Bottom