Need help with a Windows Form project written in c# please!

  • Thread starter Thread starter Erik colman
  • Start date Start date
E

Erik colman

Guest
Ive build a small robot (i'm kind of a begginer) according to youtube tutorial by Michael Reeves.

My robot uses a form window that detects the movement of the mouse(using "MouseMove" eventlistener), it gets the coordinates of the mouse, converts them to angles and sends to an arduino cheap to pass to some servo motors (for example -

coordinates.Y / ((Size.Height / 180));

The problem - The form sends the instructions every 16 milliseconds, and every time it sends the data it sort of pretends that the new angle is the defult one, and instead of getting to a limit somewhen and tell the servos to stop, it continues to turn them well over 180 degrees and causes the robot to break.


All i need is the robot to get into a limit and stop, will appriciate any help! thanks.

including the c# and arduino(also written in c#) scripts:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace MyLaserTurret
{
public partial class Form1 : Form
{
public Stopwatch watch { get; set; }

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

watch = Stopwatch.StartNew();

Port.Open();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
writeToPort(new Point(e.X, e.Y));
}

public void writeToPort(Point coordinates)
{
if (watch.ElapsedMilliseconds > 15)
{
watch = Stopwatch.StartNew();

Port.Write(String.Format("X{0}Y{1}",
(180 - coordinates.X / Convert.ToDouble((Size.Width / 180))),
(180 - coordinates.Y / Convert.ToDouble((Size.Height / 180)))));
}

}
}
}


#include <Servo.h>

Servo serX;
Servo serY;

String serialData;

void setup() {
serX.attach(10);
serY.attach(11);
Serial.begin(9600);
Serial.setTimeout(10);
}

void loop() {

}

void serialEvent(){
serialData = Serial.readString();

serX.write(parseDataX(serialData));
serY.write(parseDataY(serialData));
}

int parseDataX(String data){
data.remove(data.indexOf("Y"));
data.remove(data.indexOf("X"), 1);

return data.toInt();
}

int parseDataY(String data){
data.remove(0, data.indexOf("Y") + 1);

return data.toInt();
}

Continue reading...
 
Back
Top