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=)
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 ! 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)
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)