Go Down

Topic: Problem with 2 TrackBars in 1 SerialPort (Read 138 times) previous topic - next topic


Hello Everyone!
I am trying to control Voltage and Frequency for my 3 phase VFD inverter project through VB in Visual Studio 2012. But i am facing a problem with 2 TrackBars in one SerialPort. As you can see in the image, i am using 2 TrackBar and 2 RadioButton to control COM4. My purpose is when RadioButton1 is checked, then COM4 at TrackBar1 , TrackBar2 is off. And when RadioButton2 is checked, COM4 at TrackBar2. But i can't get it works. Could anyone help me, please??

Here is my VB Code
Code: [Select]
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "COM4"
        SerialPort1.BaudRate = 9600
        SerialPort1.Write(TrackBar1.Value & Chr(10))
        SerialPort1.Write(TrackBar2.Value & Chr(10))
    End Sub

    Public Sub send_data(ByVal data_kirim As String)
        If SerialPort1.IsOpen() Then
            SerialPort1.Write(data_kirim + Environment.NewLine())
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TrackBar1.Value = 255
        TrackBar2.Value = 255
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TrackBar1.Value = 0
        TrackBar2.Value = 0
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim i As Single = SerialPort1.ReadExisting
            lbl1.Text = "PWM Value : " & i.ToString

            If RadioButton1.Checked Then
                SerialPort1.Write(TrackBar1.Value & Chr(10))
            ElseIf RadioButton2.Checked Then
                SerialPort1.Write(TrackBar2.Value & Chr(10))
            End If
        Catch ex As Exception

        End Try
    End Sub

End Class

And here is my Arduino Code
Code: [Select]
int i=0;
int j=0;
int k=0;

int x=0;
int y=0;
int z=0;

int OK=0;
int OK1=0;
int OK2=0;
int OK3=0;
int OK4=0;
int sinPWM[]={0,1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,29,31,32,33,34,36,37,38,39,40,42,43,44,45,47,48,49,50,52,53,54,55,56,

#define toggle 7
int sw;
int AnalogDataVolt;
int nilaiVB;

int potvolt = A1; //Pengatur Tegangan
int potfreq = A2; //Pengatur Frequency

float potvoltval;
float potfreqval;

float A;
float F;

String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int val = 0;

int Volt = 0;
int Freq = 0;

void setup() {

pinMode(4, OUTPUT);

cli();// stop interrupts
TCCR0A=0;//reset nilai
TCCR0B=0;//reset nilai
TCNT0=0;//reset nilai
//0b : menulis bits dalam binary
TCCR0A=0b10100001;//phase correct pwm mode
TCCR0B=0b00000001; //tidak ada prescaler

TCCR2A=0;//reset nilai
TCCR2B=0;//reset nilai
TCNT2=0;//reset nilai
//0b : menulis bits dalam binary
TCCR2A=0b10100001;//phase correct pwm mode
TCCR2B=0b00000001; //tidak ada prescaler

TCCR3A=0;//reset nilai
TCCR3B=0;//reset nilai
TCNT3=0;//reset nilai
//0b  : menulis bits dalam binary
TCCR3A=0b10100001;//phase correct pwm mode
TCCR3B=0b00000001; //tidak ada prescaler

TCCR1A=0;//reset nilai
TCCR1B=0;//reset nilai
TCNT1=0;//reset nilai
OCR1A=509;// compare match value
TCCR1B=0b00001001; //WGM12 bit is 1 for CTC mode and no prescaler

TIMSK1 |=(1 << OCIE1A);// memulai interrupts

sei();//stop interrupts
ISR(TIMER1_COMPA_vect){// interrupt mulai ketika timer 1 cocok dengan OCR1A
if(i>629 && OK==0){// nilai akhir dari sample untuk pin 13
i=0;// mulai dari nilai awal sample
OK=1;//mengaktifkan pin 4
if(i>629 && OK==1){// nilai akhir dari sample untuk pin 4
i=0;//mulai dari nilai awal sample
OK=0;//mengaktifkan pin 13
x=round(A*sinPWM[i]);// nilai x berasal dari sample dan i berindex 0
i=i+F;// ke posisi selanjutnya dari sample
OCR0B=0;//pin 4 0
OCR0A=x;//pin 13 memulai pembacaan sample
OCR0A=0;//pin 13 0
OCR0B=x;//pin 4 memulai pembacaan sample

if ((i>=418) || OK1==1){//jika nilai i mencapai 209, maka phase kedua akan dimulai
OK1=1;//mempertahankan if function
if(j>629 && OK2==0){// nilai akhir dari sample untuk pin 10
j=0;// mulai dari nilai awal sample
OK2=1;//mengaktifkan pin 9
if(j>629 && OK2==1){// nilai akhir dari sample untuk pin 9
j=0;//mulai dari nilai awal sample
OK2=0;//mengaktifkan pin 10
y=round(A*sinPWM[j]);// nilai y berasal dari sample dan i berindex 0
j=j+F;// ke posisi selanjutnya dari sample
OCR2B=0;//pin 9 0
OCR2A=y;//pin 10 memulai pembacaan sample
OCR2A=0;//pin 10 0
OCR2B=y;//pin 9 memulai pembacaan sample
if ((j>=418) || OK3==1){//jika nilai j mencapai 209, maka phase ketiga akan dimulai
OK3=1;//mempertahankan if function
if(k>629 && OK4==0){// nilai akhir dari sample untuk pin 5
k=0;// mulai dari nilai awal sample
OK4=1;//mengaktifkan pin 2
if(k>629 && OK4==1){// nilai akhir dari sample untuk pin 2
k=0;//mulai dari nilai awal sample
OK4=0;//mengaktifkan pin 5
z=round(A*sinPWM[k]);// nilai z berasal dari sample dan i berindex 0
k=k+F;// ke posisi selanjutnya dari sample
OCR3B=0;//pin 2 0
OCR3A=z;//pin 5 memulai pembacaan sample
OCR3A=0;//pin 5 0
OCR3B=z;//pin 2 memulai pembacaan sample
void loop() { 
sw = digitalRead(toggle);
    potvoltval= analogRead(AnalogDataVolt);
    A= potvoltval/1023;

    potfreqval= analogRead(potfreq);
    F= map(potfreqval,1023,0,0,50);

    if (sw == 1){ //    vb
    if (stringComplete) {
    val = inputString.toInt();
    AnalogDataVolt = nilaiVB;
    nilaiVB = map(val, 0, 255, 0, 1023);
    inputString = "";
    stringComplete = false;}
    AnalogDataVolt = analogRead(potvolt); //    pot

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;


As you can see in the image, i am using 2 TrackBar and 2 RadioButton to control COM4.
No, you are not. The VB app is sending data to COM4. It is NOT controlling COM4.

Immediately sending data to the serial port after opening it, without allowing time for the Arduino to reset, is pointless.

Code: [Select]
            If RadioButton1.Checked Then
                SerialPort1.Write(TrackBar1.Value & Chr(10))
            ElseIf RadioButton2.Checked Then
                SerialPort1.Write(TrackBar2.Value & Chr(10))
            End If

How is the Arduino supposed to know whether the data that arrives is the trackbar 1 value, the trackbar 2 value, or the temperature of the back side of the moon?

Code: [Select]
int sinPWM[]
Why are you pissing away memory storing byte values in an int array?

Code: [Select]
cli();// stop interrupts

sei();//stop interrupts

Why do you suppose that the Arduino team chose to provide two functions to turn interrupts off?

But i can't get it works.
The code does something that you failed to explain. You expect it to do something, but you failed miserably in explaining that.

Could anyone help me, please??
Yes. Questions only need one question mark. More than one does not make you look smart.
The art of getting good answers lies in asking good questions.


May 14, 2019, 04:08 pm Last Edit: May 14, 2019, 04:11 pm by Robin2
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(','); // comma separator
Serial.println('>'); // end marker

When your PC program opens the serial port it must allow time for the Arduino to reset. I usually get my Arduino program to send a message from setup() - for example Serial.println("Arduino is Ready"); - and my PC program waits until it receives that before trying to send data to the Arduino. Then the PC program should keep the Serial Port open until it is completely finished with the Arduino.

I suspect it would make more sense for your PC program to send the values from both trackbars every time even if the data has not changed. That would make it easy for the Arduino to know which value was which. Something like <trackbar1Val, trackbar2Val>

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up