I'm trying to control 2 things at once via a VB form, a set of servos and a motor. I watched Michael Reeve's tutorial on his laser turret and copied that, and I added the motor. I have the C# code sending serial data to the arduino, coordinates for the servos and a simple turn-on command for the motor. I'm having trouble having the arduino read both commands at once. I assume that the arduino cant read 2 different serial strings at once, but I'm not sure how to go about fixing this.
Here is the arduino code:
#include<Servo.h>
Servo servX;
Servo servY;
String serialData;
String serialDataFire;
void setup()
{
//initialize servos
servX.attach(5);
servY.attach(6);
//initialize motor controller pins
pinMode(8, OUTPUT); //IN3
pinMode(9, OUTPUT); //IN4
pinMode(10, OUTPUT); //ENB/PWM
//Begin Serial Communication
Serial.begin(9600);
Serial.setTimeout(10); //shortens command delay apparently
}
void loop()
{
serialEventFire();
}
//====================================================================
// Servo Stuff
//====================================================================
void serialEvent () //reads the data via serial connection
{
serialData = Serial.readString();
Serial.print(serialData);
servX.write(parseDataX(serialData));
servY.write(parseDataY(serialData));
}
// the data will be coming in as X100Y100 (for example). To use the data, we need to
// isolate the numbers, by removing all of the other characters:
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();
}
//====================================================================
// Motor Controller Stuff
//====================================================================
void serialEventFire() //reads in the Fire comand via serial connection
{
//reads in the string sent via serial
serialDataFire = Serial.readString();
//determines if serialData holds a string
if (serialDataFire.equalsIgnoreCase("fire"))
{
Fire();
}
Serial.print(serialDataFire);
}
void Fire() //turns on the motors
{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
analogWrite(10, 245);
delay (4250);
digitalWrite(8, LOW);
}
Here is the C# code
Quote
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
//IN PROGRESS COMBINATION
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Stopwatch watch { get; set; } //needed to slow the speed at which we send the arduino information for the servos, for latency
public string fire = "Fire";
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) //sends arduino info every 15 milliseconds
{
watch = Stopwatch.StartNew();
port.Write(String.Format("X{0}Y{1}",
(180 - coordinates.X / (Size.Width / 180)), //takes coordinates by the scale. Size of form (600) / max servo rotation (180)
(180 - coordinates.Y / (Size.Height / 180))));
}
}
private void Form1_Click(object sender, EventArgs e)
{
port.Write(fire);
}
}
}