Reputation: 21
I have some code that sends a number that is pre-calculated to an Arduino. instead of a pre-calculated number, I want a random number between a range of number, for example between the numbers 1 to 20, the random number is 18, after the random number is found put it in a string so I can work with it.
I've tried many things on stack overflow but things are too complicated and I work with C#. the code below is what I have right now, I would prefer that it will send that random number to the Arduino
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}",
(coordinates.X / (Size.Width / 180)),
(coordinates.Y / (Size.Height / 180))));
}
}
}
}
Upvotes: 2
Views: 118
Reputation: 1342
To create a random number as a string you use this code
private Random random = new Random();
public string RandomNumber(int min, int max)
{
return random.Next(min, max).ToString();
}
Please note that it is probably best practice to declare a "Random random = new Random();" as a class property because when a Random is created too closely together they will just keep having the same value
Upvotes: 2