Need help with c# and arduino

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");
            }
        }
    }
}

It appears that when you click a button in your C# application, it sends the text that is on the button, unless the button text is "10", in which case, it sends an "A".

When a letter is received, appropriate pins are activated, presuming that you have buttons labeled Q, W, E, R, S, D, F, Z, X, O, and 10 (10?)

It's not clear what your problem is. Are you having issues because the buttons in the C# form are toggle buttons rather than momentary contact switches?

yes thats right i need momentary button it sends signal bu buttons name say like buton Q or W its starts to spin motor but not stoping after i release it. Sorry but i am beginer at programming just trying to leran.
ant there is no buttons named A or 10

there is no buttons named A or 10

What is this code for then?

            if (thisbutton.Text == "10")
            {
                com1.Write("A");
            }
            else
            {
                com1.Write(thisbutton.Text);
            }

There are a whole variety of events that buttons emit. You have added a delegate to the Click event.

You could remove this delegate (button1_Click), and add delegates for the MouseDown and MouseUp events, instead. Then, the button would behave like a momentary contact switch. MouseDown corresponds to pressed. MouseUp corresponds to released.

Send appropriate data to the serial port in each delegate.

i will try with mouse that c#code is from 10bar led so its not totaly mine i just trying to adopt it and learn where is what

funny but if i remove

            if (thisbutton.Text == "10")
            {
                com1.Write("A");
            }

program doesent even start motors to spin and can someone explain how to use MouseDown and MouseUp

Thanks.

MouseUp and MouseDown are used exactly like Click. Well, almost. You can add a Click handler by clicking on the button on the form. You can't add MouseUp and MouseDown handlers that way.

To add them, select the button. Then, go to the Properties tab/dialog. Select the lightening bolt at the top. This will switch to the Event view.

Locate the MouseUp event. In the delegate field, either enter a name or double click to have the Visual Studio assign the name. Do the same for the Mouse Up event.

Then, write the code - exactly like for the Click event. The MouseDown event needs to send the button1.Text value. The MouseUp event needs to send 'O' to stop the motors.

thank you very much now it start to make sence a bit :slight_smile:

Come on back if you have more questions, or PM me.

do i need to remove this void   private void button1_Click(object sender, EventArgs e)

or i can just add MouseDown and MouseUp?

if it helps this is my Form1.Designer.cs code as far i udenderstand i need to change somthing here too

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.button1.Location = new System.Drawing.Point(434, 205);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "&Q";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.BackColor = System.Drawing.Color.WhiteSmoke;
            this.button2.Location = new System.Drawing.Point(363, 255);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "&W";
            this.button2.UseVisualStyleBackColor = false;
            this.button2.Click += new System.EventHandler(this.button1_Click);             
            // button3
            // 
            this.button3.BackColor = System.Drawing.Color.WhiteSmoke;
            this.button3.Location = new System.Drawing.Point(504, 255);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "&E";
            this.button3.UseVisualStyleBackColor = false;
            this.button3.Click += new System.EventHandler(this.button1_Click);             
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(645, 343);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Robo Control";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
    }
}

i am only using 3 buttons at the moment

This is the code that registers the event handler.

            this.button3.Click += new System.EventHandler(this.button1_Click);

This code is added automatically when you double clicked the button. The appropriate code is added to this file when you register the event handler on the Properties tab, too.

You should never need to edit this file manually.

Done like this for the "Q" button

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            {
                Button thisbutton;
                thisbutton = (Button)sender;

                {
                    com1.Write("Q");
                }
            }
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            {
                Button thisbutton;
                thisbutton = (Button)sender;
                {
                    com1.Write("0");

                }
            }

and it works thank you very much for help

and one more thing is it possible to make servo control sort of scroll bar say left to right ?
And is it hard

and one more thing is it possible to make servo control sort of scroll bar say left to right ?
And is it hard

You can add a slider to your form. Set it's range from 0 to 180. Then, add an event handler for the slider. Send the slider position to the serial port in the delegate.

Is it hard? Not at all.

easy for you :slight_smile:
just made slider with 0 - 180 range but how to make it work in arduino language and on C3 app?

On the Arduino side, use the Servo library. Create an instance, and attach the servo.

Read the servo position from the serial port, convert the string to an integer, and use the Servo::write method to send the servo to that position.

In C#, intellisense should show you a list of methods and values that the slider object supports. One of them should be recognizable as the slider's current position. If not, fire up help, and do some reading.