C++ Servo Control from Windows, need some help

Good day
I have mannaged to create a C++ app that controls a servo from left to right.
There are three buttons LEFT, CENTER & RIGHT.
At the moment there is something strange going on with my scetch, It does nothing when I press LEFT, and moves right when I click CENTER & when I click RIGHT, I would like it to do what the buttons says.
I have struggled with this until about 11PM last night, and I just can not get it right.
I am attatching the Scetch, C++ Source Code and a link to download the C++ source files with attatched Scetch.
I figure there is something wrong with my IF statements, but have no idea to fix it.

Arduino Scetch

/*
 
* Serial Read Blink
 * -----------------
 * Turns a Servo connected to digital pin 9. 
 * The Servo will move the number of times given by a 
 * ASCII number read from the serial port.
 *
 * Created 11 March 2011
 * copyleft 2006 Tod E. Kurt <tod@todbot.com>
 * http://todbot.com/
 * Edited by Riaan Deyzel
 * based on "serial_read_advanced" example
 */

int ServoPin = 9;  
int val = 1;       

void setup() {
  pinMode(ServoPin,OUTPUT);    
  Serial.begin(9600);        
}

void loop () {
  val = Serial.read();      

 
  if (val > '1' && val <= '600' ) {
    val = val - '0';          
    for(int i=0; i<=val; i++) {
      Serial.println("Move!");
      digitalWrite(ServoPin,HIGH);
      delay(15);
      digitalWrite(ServoPin, LOW);
      delay(15);
    }
    //Serial.println();
  }
}

C++ Source Code

#pragma once


namespace ArduinoPinControl {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::IO::Ports::SerialPort^  serialPort1;
	protected: 
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Label^  label1;






	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma 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>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// serialPort1
			// 
			this->serialPort1->PortName = L"COM3";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(3, 37);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 63);
			this->button1->TabIndex = 0;
			this->button1->Text = L"LEFT";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(105, 37);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 63);
			this->button2->TabIndex = 1;
			this->button2->Text = L"CENTER";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click_1);
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(206, 37);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(68, 63);
			this->button3->TabIndex = 2;
			this->button3->Text = L"RIGHT";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(0, 21);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(95, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"ARDUINO PIN D9";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(298, 133);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"ARDUINO DUAL SERVO CONTROL";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	    {
      this->serialPort1->PortName = "COM3";  // Replace with your COM port!
      this->serialPort1->Open();
      this->serialPort1->Write( "1" );  // In the future,  you'll expand on this
                                        // to write your custom data to the board
      this->serialPort1->Close();
			 }
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void button2_Click_1(System::Object^  sender, System::EventArgs^  e) {
      this->serialPort1->PortName = "COM3";  // Replace with your COM port!
      this->serialPort1->Open();
      this->serialPort1->Write( "2" );  // In the future,  you'll expand on this
                                        // to write your custom data to the board
      this->serialPort1->Close();

		 }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
      this->serialPort1->PortName = "COM3";  // Replace with your COM port!
      this->serialPort1->Open();
      this->serialPort1->Write( "3" );  // In the future,  you'll expand on this
                                        // to write your custom data to the board
      this->serialPort1->Close();
		 }
private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void label2_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
};
}

Good day

In the link below It will show the example picture of the buttons that I am using tho control a servo on PinD9.
Included in this link will be link to download the C++ Source files (Arduino_Pin_Controlnew.rar) with The Arduino Scetch included:
http://robotcontrol.yolasite.com/ardiuno-stuff.php

I am still a newebie, and has tried to make a servo move with buttons now for about 6 months, as I know very little about programing, and Servo's jitter too much with Firmata, so I would like to use this C++ app.

Please help me make this app & source work correctly.

There are three buttons LEFT, CENTER & RIGHT.

Would these be the three objects named button1, button2, and button3?

      this->serialPort1->Open();
      this->serialPort1->Write( "1" );  // In the future,  you'll expand on this
                                        // to write your custom data to the board
      this->serialPort1->Close();

Opening and closing the serial port resets the Arduino. Is this really what you want to do for every button press?

  if (val > '1' && val <= '600' ) {

val was a byte read from the serial port. It will be -1 if there was nothing to read, which is the usual case, since the PC application sends serial data before the Arduino is ready, or 0 to 255 if there was data. '1' is in that range, true. '600' is a multibyte character, which is syntactically valid but not supported on the Arduino.

Writing the servo pin HIGH or LOW is not the correct way to control a servo.

Hi Paul

Yes button 1,2,3 are the 3 control buttons.
1, Left
2, Right
3, Center

Would like to move the servo about 5 to 10 degrees every time I click the
Buttons 1 & 2 and move to 180 when I press Center.

You need stop opening and closing the serial port each time a button is clicked.

A typical servo moves through a 180 degree sweep, from 0 to 180 degrees. Center is not at 180 degrees. What kind of servo do you have?

Do you want the PC to keep track of the position of the servo, so that an absolute position is sent every time, or do you want the Arduino to keep track of that, and increment, decrement, or reset that value based on what the PC sends?

Good day

I would like the Arduino to keep track of the servo movements.
I use a standard Power HB 3001HB servo that moves only 180 degrees.
I also use an Arduino Deumilanueve and on the PC windows 7 Pro.
My goal is to pan & tilt two servo's, one on pin D9 & the other on D10.
I use Visual Studio 2008 Standard, I think it supports C# VB and C++,
So source in any one of theese will be appreciated.

So source in any one of theese will be appreciated.

You already have source code that almost works. You simply need to add two buttons to the form. One button should be labeled "Connect", and should set up the serialPort1 variable, and call it's Open method.

The other should be labeled "Disconnect" and should close the serial port. In the existing functions, quit opening and closing the port.

Have you achieve ther target of controlling the servos? Could you update the final code? thanks

I don't know about M$ C++, but with C# and VB you need to use multithreading
to use the serial port objects with Forms. See my comment on this thread:

http://arduino.cc/forum/index.php/topic,112253.0.html