RC-Heli über MX-12 mit X-Box Controller steuern

Das Problem mit der Verzögerung hab ich gelöst! :smiley: Problem war, dass der vb.net Timer nicht jede Millisekunde das Signal gesendet hat. Zudem musste ich die "if (Serial.available() > 0)" Methode benutzen, da ansonsten der Stellwert sich immer wieder auf 1000 zurückgesetzt hat. Zudem hab ich auch noch die Stellgrößen duration 2 und 3 übertragen!

Grüße

Christoph

Hier der arduino-Sketch:

int pin = 3;  // PPM INPUT PIN
unsigned int duration1 = 1000;
unsigned int duration2 = 1000;
unsigned int duration3 = 1000;
int lowduration = 350;

void setup()
{
  Serial.begin(115200);
  pinMode(pin, OUTPUT);
}

void loop()
{        
 if (Serial.available() > 0) {
   
   if (Serial.read() == 255){
        duration1 = map(Serial.read(), 0, 250, 700, 1500);
        duration2 = map(Serial.read(), 0, 250, 700, 1500);
        duration3 = map(Serial.read(), 0, 250, 700, 1500);
    }
  }
  
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(duration3);
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(duration1);
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(duration2);
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(1000);
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(1000);
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(1000);
digitalWrite(3, LOW);
delayMicroseconds(lowduration);

digitalWrite(3, HIGH);
delayMicroseconds(20000 - (lowduration*7) - (1000*3) - duration1 - duration2 - duration3);
}

Und hier der VB.code

Imports System.IO.Ports     'COM-Port
Imports System

Public Class Form1

    Dim JInfo As JOYINFO
    Public Shared indata As String

    Public s As New SerialPort
    Private Declare Function joyGetPos Lib "winmm.dll" (ByVal uJoyID As Integer, ByRef pji As JOYINFO) As Integer

    Private Structure JOYINFO

        Dim X As Integer
        Dim Y As Integer
        Dim Z As Integer
        Dim Buttons As Integer

    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        s.Close()
        s.PortName = "com3"                         'will need to change to your port number
        s.BaudRate = 115200
        s.DataBits = 8
        s.Parity = Parity.None
        s.StopBits = StopBits.One
        s.Handshake = Handshake.None
        s.Encoding = System.Text.Encoding.Default   'very important!
        s.Open()
        s.RtsEnable = True

        AddHandler s.DataReceived, AddressOf DataReceviedHandler

    End Sub

    Private Shared Sub DataReceviedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        Dim sp As SerialPort = CType(sender, SerialPort)
        indata = sp.ReadLine

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim x, y, z As Integer

        joyGetPos(0, JInfo)      'Joystick 0

        x = Math.Round(Str$(JInfo.X) / 65525 * 250, 0)
        y = Math.Round(Str$(JInfo.Y) / 65525 * 250, 0)
        z = Math.Round(Str$(JInfo.Z) / 65525 * 250, 0)


        TextBox1.Text = x + y + z

        TextBox2.Text = x
        TextBox3.Text = y
        TextBox4.Text = z
   
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim x, y, z As Integer

        joyGetPos(0, JInfo)      'Joystick 0

        x = Math.Round(Str$(JInfo.X) / 65525 * 250, 0)
        y = Math.Round(Str$(JInfo.Y) / 65525 * 250, 0)
        z = Math.Round(Str$(JInfo.Z) / 65525 * 250, 0)

        s.Write(Chr(255) & Chr(x) & Chr(y) & Chr(z))

    End Sub