Go Down

Topic: Interfacing with visual studios c# (Read 121 times) previous topic - next topic

Usborne972

Hia, this is probably a pretty simple question, but I am completely stumped on this bug/gaff.
Anyway I am trying to get two servos to turn when I move the mouse over a windows form.

I know using an uno would probably be better, but all I have available right now is a nano, just curious if I could even use a nano. Thanks!



this is my visual studio code.
Code: [Select]
public Stopwatch timer { get; set; }

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
timer = Stopwatch.StartNew();
TheCoolBeans.Open();
}

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

public void writeToPort(Point coordinates)
{
if (timer.ElapsedMilliseconds > 15)
{


timer = Stopwatch.StartNew();
// put in 180- if going in the wrong direction, why than you, oh its
// no problem, but thanks any way!
TheCoolBeans.Write(String.Format("X{0}Y{1}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180))));




}
}
}
}

TheCoolBeans is a serial port
Code: [Select]
#include<Servo.h>

Servo servX;
Servo servY;


String serialData;

void setup() {
  // put your setup code here, to run once:
  servY.attach(10);
  servX.attach(11);
  Serial.begin(9600);
  Serial.setTimeout(10);

}

void loop() {
  //lol
 
}

void SerialEvent(){

serialData = Serial.readString();
parseDataX(serialData);
parseDataY(serialData);
servX.write(parseDataX(serialData));
servY.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();
}


and that was my Arduino nano code.

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



For receiving data on the Arduino 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



If you need more help please post an example of the message that your PC sends to the Arduino.

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

PaulS

Code: [Select]
serialData = Serial.readString();
So, let's assume that the serialData object wraps "X25Y50".

Code: [Select]
parseDataX(serialData);
This modifies the string that the object wraps, so that it now looks like "25", and returns 25, that you ignore.

Code: [Select]
parseDataY(serialData);
Since you didn't bother checking that the wrapped string contains a Y, you removed the rest of the string, converted an empty string to an int that you returned and ignored.

Code: [Select]
servX.write(parseDataX(serialData));
The string that the serialData object wraps is an empty string. This call to parseDataX returns a 0, in that case.

Code: [Select]
servY.write(parseDataY(serialData));
The string that the serialData object wraps is an empty string. This call to parseDataY returns a 0, in that case.

You REALLY need to understand destructive functions, and only call them on copies of data.
The art of getting good answers lies in asking good questions.

Go Up