Go Down

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

pejuangsst14

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
        Me.CenterToParent()
        SerialPort1.PortName = "COM4"
        SerialPort1.BaudRate = 9600
        SerialPort1.Open()
        Timer1.Start()
        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
        Try
            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,
              58,59,60,61,62,64,65,66,67,68,70,71,72,73,74,76,77,78,79,80,82,83,84,85,86,87,89,90,91,92,93,94,96,97,98,99,100,101,102,104,105,106,
              107,108,109,110,111,112,114,115,116,117,118,119,120,121,122,123,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
              142,143,144,145,147,148,149,150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,171,172,173,
              174,175,176,177,178,179,179,180,181,182,183,184,185,185,186,187,188,189,189,190,191,192,193,193,194,195,196,197,197,198,199,200,200,
              201,202,203,203,204,205,205,206,207,208,208,209,210,210,211,212,212,213,214,214,215,215,216,217,217,218,219,219,220,220,221,221,222,
              223,223,224,224,225,225,226,226,227,227,228,228,229,229,230,230,231,231,232,232,233,233,234,234,234,235,235,236,236,237,237,237,238,
              238,238,239,239,240,240,240,241,241,241,241,242,242,242,243,243,243,244,244,244,244,245,245,245,245,246,246,246,246,246,247,247,247,
              247,247,247,248,248,248,248,248,248,248,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,
              249,249,249,249,249,249,249,249,249,249,249,249,249,248,248,248,248,248,248,248,247,247,247,247,247,247,246,246,246,246,246,245,245,
              245,245,244,244,244,244,243,243,243,242,242,242,241,241,241,241,240,240,240,239,239,238,238,238,237,237,237,236,236,235,235,234,234,
              234,233,233,232,232,231,231,230,230,229,229,228,228,227,227,226,226,225,225,224,224,223,223,222,221,221,220,220,219,219,218,217,217,
              216,215,215,214,214,213,212,212,211,210,210,209,208,208,207,206,205,205,204,203,203,202,201,200,200,199,198,197,197,196,195,194,193,
              193,192,191,190,189,189,188,187,186,185,185,184,183,182,181,180,179,179,178,177,176,175,174,173,172,171,171,170,169,168,167,166,165,
              164,163,162,161,160,159,158,157,156,155,154,153,152,152,151,150,149,148,147,145,144,143,142,141,140,139,138,137,136,135,134,133,132,
              131,130,129,128,127,126,125,123,122,121,120,119,118,117,116,115,114,112,111,110,109,108,107,106,105,104,102,101,100,99,98,97,96,94,
              93,92,91,90,89,87,86,85,84,83,82,80,79,78,77,76,74,73,72,71,70,68,67,66,65,64,62,61,60,59,58,56,55,54,53,52,50,49,48,47,45,44,43,42,
              40,39,38,37,36,34,33,32,31,29,28,27,26,24,23,22,21,19,18,17,16,14,13,12,11,9,8,7,6,4,3,2,1,0};

#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() {
Serial.begin(9600);
inputString.reserve(200);

pinMode(4, OUTPUT);
pinMode(13,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(5,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
if(OK==0){
OCR0B=0;//pin 4 0
OCR0A=x;//pin 13 memulai pembacaan sample
}
if(OK==1){
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
if(OK2==0){
OCR2B=0;//pin 9 0
OCR2A=y;//pin 10 memulai pembacaan sample
}
if(OK2==1){
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
if(OK4==0){
OCR3B=0;//pin 2 0
OCR3A=z;//pin 5 memulai pembacaan sample
}
if(OK4==1){
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);
    Serial.println(val);
    inputString = "";
    stringComplete = false;}
    }
    else
    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;
    }
  }
}


PaulS

Quote
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?

Quote
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.

Quote
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.

Robin2

#2
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(value1);
Serial.print(','); // comma separator
Serial.print(value2);
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>

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

Go Up