Hello evryone.I just bought my first arduino and started to play with it.Done all of starting tutorials no making a foward backward conntrol control using c#
just after i uploaded my processing code to arduino and launched applicatinon my buttons send signal non stop.Means that my motors keeps on spinning but i wabt them to spinn just until button is pressed and affter i reales it i want motor to stop. My arduino skech is
// set the output pins
// 14-18 are actually analog pins 0-4
int baseMotorEnablePin = 2;
int baseMotorPin1 = 3;
int baseMotorPin2 = 4;
int shoulderMotorEnablePin = 14;
int shoulderMotorPin1 = 15;
int shoulderMotorPin2 = 16;
int elbowMotorEnablePin = 8;
int elbowMotorPin1 = 9;
int elbowMotorPin2 = 10;
int wristMotorEnablePin = 5;
int wristMotorPin1 = 6;
int wristMotorPin2 = 7;
int handMotorEnablePin = 11;
int handMotorPin1 = 17;
int handMotorPin2 = 18;
// set a variable to store the byte sent from the serial port
int incomingByte;
void setup() {
// set the SN754410 pins as outputs:
pinMode(baseMotorPin1, OUTPUT);
pinMode(baseMotorPin2, OUTPUT);
pinMode(baseMotorEnablePin, OUTPUT);
digitalWrite(baseMotorEnablePin, HIGH);
pinMode(shoulderMotorPin1, OUTPUT);
pinMode(shoulderMotorPin2, OUTPUT);
pinMode(shoulderMotorEnablePin, OUTPUT);
digitalWrite(shoulderMotorEnablePin, HIGH);
pinMode(elbowMotorPin1, OUTPUT);
pinMode(elbowMotorPin2, OUTPUT);
pinMode(elbowMotorEnablePin, OUTPUT);
digitalWrite(elbowMotorEnablePin, HIGH);
pinMode(wristMotorPin1, OUTPUT);
pinMode(wristMotorPin2, OUTPUT);
pinMode(wristMotorEnablePin, OUTPUT);
digitalWrite(wristMotorEnablePin, HIGH);
pinMode(handMotorPin1, OUTPUT);
pinMode(handMotorPin2, OUTPUT);
pinMode(handMotorEnablePin, OUTPUT);
digitalWrite(handMotorEnablePin, HIGH);
// start sending data at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// check that there's something in the serial buffer
if (Serial.available() > 0) {
// read the byte and store it in our variable
// the byte sent is actually an ascii value
incomingByte = Serial.read();
// note the upper casing of each letter!
// each letter turns a motor different way.
if (incomingByte == 'Q') {
digitalWrite(baseMotorPin1, LOW);
digitalWrite(baseMotorPin2, HIGH);
}
if (incomingByte == 'W') {
digitalWrite(baseMotorPin1, HIGH);
digitalWrite(baseMotorPin2, LOW);
}
if (incomingByte == 'E') {
digitalWrite(shoulderMotorPin1, LOW);
digitalWrite(shoulderMotorPin2, HIGH);
}
if (incomingByte == 'R') {
digitalWrite(shoulderMotorPin1, HIGH);
digitalWrite(shoulderMotorPin2, LOW);
}
if (incomingByte == 'A') {
digitalWrite(elbowMotorPin1, LOW);
digitalWrite(elbowMotorPin2, HIGH);
}
if (incomingByte == 'S') {
digitalWrite(elbowMotorPin1, HIGH);
digitalWrite(elbowMotorPin2, LOW);
}
if (incomingByte == 'D') {
digitalWrite(wristMotorPin1, LOW);
digitalWrite(wristMotorPin2, HIGH);
}
if (incomingByte == 'F') {
digitalWrite(wristMotorPin1, HIGH);
digitalWrite(wristMotorPin2, LOW);
}
if (incomingByte == 'Z') {
digitalWrite(handMotorPin1, LOW);
digitalWrite(handMotorPin2, HIGH);
}
if (incomingByte == 'X') {
digitalWrite(handMotorPin1, HIGH);
digitalWrite(handMotorPin2, LOW);
}
// if a O is sent make sure the motors are turned off
if (incomingByte == 'O') {
digitalWrite(baseMotorPin1, LOW);
digitalWrite(baseMotorPin2, LOW);
digitalWrite(shoulderMotorPin1, LOW);
digitalWrite(shoulderMotorPin2, LOW);
digitalWrite(elbowMotorPin1, LOW);
digitalWrite(elbowMotorPin2, LOW);
digitalWrite(wristMotorPin1, LOW);
digitalWrite(wristMotorPin2, LOW);
digitalWrite(handMotorPin1, LOW);
digitalWrite(handMotorPin2, LOW);
}
}
}
and c# code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SerialPort com1 = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button thisbutton;
thisbutton = (Button)sender;
if (thisbutton.BackColor == Color.WhiteSmoke)
{
thisbutton.BackColor = Color.LawnGreen;
}
else
{
thisbutton.BackColor = Color.WhiteSmoke;
}
if (thisbutton.Text == "10")
{
com1.Write("A");
}
else
{
com1.Write(thisbutton.Text);
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
com1.PortName = "COM5";
com1.BaudRate = 9600;
com1.Parity = Parity.None;
com1.DataBits = 8;
com1.StopBits = StopBits.One;
com1.Handshake = Handshake.None;
com1.ReadTimeout = 500;
com1.WriteTimeout = 500;
com1.Open();
// MessageBox.Show("open port5");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message.ToString(), "Cant open port5");
}
}
}
}