How to control motor using bluetooth using c# script T'ReX

[code]private void btnLeft_Click(object sender, EventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0x00;
send();

}

private void btnRight_Click(object sender, EventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0xFF;
send();
}

private void btnSend_MouseDown(object sender, MouseEventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0xFF;
send();
}

private void btnLeft_MouseDown(object sender, MouseEventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0x00;
send();
}

private void btnRight_MouseDown(object sender, MouseEventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0xFF;
send();
}

private void btnRight_KeyDown(object sender, KeyEventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0xFF;
send();
}[/code]

I want to make my motor to interia/looping and i want to control my motor by using keyboard arrow key. PLEASE DO HELP ME TO SOLVE THIS PROBLEM.
Thank You
Your regards,
Desmond=)

Hi Desmond and welcome to our forums!

I’m not sure what you mean by make your motor “interia/looping”. Can you clarify?

As for controlling your motor using your keyboard arrows, you would need to do something like this:

private void btnRight_KeyDown(object sender, KeyEventArgs e)
{
  switch (e.KeyCode)
  {
	 case Keys.Left:
	   LeftMotorSpeed = 0xFF;
	   RightMotorSpeed = 0x00;
	   break;
	 case Keys.Right:
	   LeftMotorSpeed = 0x00;
	   RightMotorSpeed = 0xFF;
	   break;
	 case Keys.Up:
	   LeftMotorSpeed = 0xFF;
	   RightMotorSpeed = 0xFF;
	   break;
	 default:
	   LeftMotorSpeed = 0x00;
	   RightMotorSpeed = 0xFF;
	   break;
  }
  Header = 0x0F;
  send();
}

Note: This code is incomplete so hasn’t been tested.

You might also want to check the KeyDown event of another object if you’re not just checking for keys press when the btnRight object is in focus.

Hope this helps,

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.IO.Ports;
using System.Diagnostics;
using System.Threading.Tasks;

namespace idogblutooth
{
   public partial class Form1 : Form
   {
	 byte] Buffer = new byte[3];
	 byte Header = 0x0F;
	 byte LeftMotorSpeed = 0xFF;
	 byte RightMotorSpeed = 0xFF;
	 SerialPort port;
	 String scom;

	 public Form1()
	 {
	    InitializeComponent();
	 }
	 private void Form1_Load(object sender, EventArgs e)
	 {
	    Buffer[0] = Header;
	    Buffer[1] = LeftMotorSpeed;
	    Buffer[2] = RightMotorSpeed;
	 }
	 public List<string> GetAllPorts()
	 {
	    List<String> allPorts = new List<String>();
	    foreach (String portName in System.IO.Ports.SerialPort.GetPortNames())
	    {
		  allPorts.Add(portName);
	    }
	    return allPorts;
	 }

	 void adddevice()
	 {
	    try
	    {  //add device n os
		  Process p = Process.Start("C:\\Windows\\System32\\DevicePairingWizard.exe");//here write ur own windows drive
		  while (true)
		  {
			if (p.HasExited) //determine if process end
			    break;
		  }

		  //listBox1.DataSource = GetAllPorts();
		  richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "COMPORT GENERATED";
	    }
	    catch (Exception ee)
	    {
		  if (DialogResult.Retry == MessageBox.Show("CANT FIND UR ADDED DEVICE..", "Problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error))
			adddevice();
	    }
	 }
	 void Removedevice()
	 {
	    try
	    {  //add device n os
		  Process p = Process.Start("C:\\Windows\\System32\\control.exe");//here write ur own windows drive
		  while (true)
		  {
			if (p.HasExited) //determine if process end
			    break;
		  }

		  //listBox1.DataSource = GetAllPorts();
		  richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "COMPORT REMOVED";
	    }
	    catch (Exception ee)
	    {
		  if (DialogResult.Retry == MessageBox.Show("..", "Problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error))
			adddevice();
	    }
	 }
	 private void button1_Click(object sender, EventArgs e)
	 {
	    adddevice();
	 }

	 private void button2_Click(object sender, EventArgs e)
	 {
	    Removedevice();
	 }
	 private void connectbluetooth()
	 {
	    try
	    {
		  if (listBox1.SelectedIndex != -1)
		  {
			scom = listBox1.SelectedItem.ToString();
			port = new SerialPort(scom, 9600, Parity.None, 8, StopBits.One);
			port.Open();
			richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "CONNECTION OPEN SUCCESSFUL"; ;
			//return true;
		  }
		  else
			MessageBox.Show("Please select com port", "Missing port", MessageBoxButtons.OK, MessageBoxIcon.Information);
		  // return false;
	    }
	    catch (Exception a)
	    {
		  if (DialogResult.Retry == MessageBox.Show(a.Message, "problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning))
			connectbluetooth();
		  //else
		  //    return false;
		  // return false;
	    }
	 }
	 bool send()
	 {
	    try
	    {
		  Buffer[0] = Header;
		  Buffer[1] = LeftMotorSpeed;
		  Buffer[2] = RightMotorSpeed;
		  port.WriteTimeout = 10000;//define how much time wait for send data
		  port.Write(Buffer, 0, Buffer.Length);
		  return true;
	    }
	    catch (Exception a)
	    {
		  //if (DialogResult.Retry == MessageBox.Show(a.Message, "Problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning))
		  //  //  send(Buffer, 0, Buffer.Length);
		  //else
		  //    return false;
		  return false;
	    }
	 }
    
	 private void button3_Click(object sender, EventArgs e)
	 {
	    connectbluetooth();
	 }

	 private void button4_Click(object sender, EventArgs e)
	 {

	 }

	 private void button5_Click(object sender, EventArgs e)
	 {
	    listBox1.DataSource = GetAllPorts();
	 }

	 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
	 {

	 }

	 private void button6_Click(object sender, EventArgs e)
	 {
	    Header = 0x0F;
	    LeftMotorSpeed = 0xFF;
	    RightMotorSpeed = 0x00;
	    send();
	 }

	 private void button7_Click(object sender, EventArgs e)
	 {
	    Header = 0x0F;
	    LeftMotorSpeed = 0xFF;
	    RightMotorSpeed = 0xFF;
	    send();
	 }

	 private void button8_Click(object sender, EventArgs e)
	 {

	    Header = 0x0F;
	    LeftMotorSpeed = 0x00;
	    RightMotorSpeed = 0xFF;
	    send();
	 }

	 private void button9_Click(object sender, EventArgs e)
	 {
	    Header = 0xFF;
	    
	    send();
	 }

	 private void button10_Click(object sender, EventArgs e)
	 {
	    Header = 0x0F;
	    LeftMotorSpeed = 0x00;
	    RightMotorSpeed = 0x00;
	    send();
	 }


	 private void button5_KeyDown(object sender, KeyEventArgs e)
	 {
	    switch (e.KeyCode)
	    {
		  case Keys.Left:

			LeftMotorSpeed = 0xFF;
			RightMotorSpeed = 0x00;
			break;
		  case Keys.Right:

			LeftMotorSpeed = 0x00;
			RightMotorSpeed = 0xFF;
			break;
		  case Keys.Up:

			LeftMotorSpeed = 0xFF;
			RightMotorSpeed = 0xFF;
			break;

		  default:

			LeftMotorSpeed = 0x00;
			RightMotorSpeed = 0xFF;
			break;
	    }
	    Header = 0x0F;
	    send();
	 }
   }
}

This is my whole coding . Thx for the code you give me !:slight_smile: but the backward key can’t use.
Do you have speaker sensor coding for it? i don’t know how to make out the coding to match with this script on the top.
Thank you
Your regards,
Desmond=)

To go backwards, I think you need to set your speed to -255, but since you’re using byte (unsigned) for your variables, that won’t work…

Are you implementing the same protocol as specified on page 9 of the manual? It doesn’t seem to be using the same format as you…
T-Rex User Manual.pdf (1.28 MB)

Do you know how to do script to produce sound when my robot sense an obstacle in front?

Your regards,
Desmond

Wouldn’t a simple digital buzzer be sufficient?
robotshop.com/en/dfrobot-buzzer-module.html

Sorry maybe i phrase it wrongly. What i want to know is how to make a c# script to make the sound sensor works?

Your regards,
Desmond

If you’re using the T’rex robot controller, it’s ideally programmed in Arduino (unless you flashed a new bootloader).
As such, you can simply connect the buzzer to a digital I/O pin on the board (and GND) and refer to the digital example in the Arduino software.
Making the buzzer work only when an obstacle is detected is just a matter of “if… then”; Pseudo-code:

if (value from sound sensor)< (threshold value).. then (activate digital IO pin)