Go Down

Topic: Arduino VB or C# servo control (Read 3088 times) previous topic - next topic

Mar 02, 2011, 04:08 pm Last Edit: Mar 02, 2011, 04:11 pm by Riaan Deyzel Reason: 1
Good day

Can anyone please help me with the following project.
I need a c# or VB app that looks and works similar to this one that i created http://robotcontrol.yolasite.com/robotics-forum.php#bn-forum-1-1-4078249718/6874 but the one I created does not work, it was only used as a template and example.
I am currently using Firmata, bit the PWM sliders are just not cutting it.
Even if someone out there know how to convert the PWM sliders in Firmata to buttons I would realy appreciate it.

Please help me with this :smiley-mr-green:.

Thank you & regards,
Riaan Deyzel

PaulS

Quote
Even if someone out there know how to convert the PWM sliders in Firmata to buttons I would realy appreciate it.

You want to be able to push the button some amount in C#? Doesn't make sense. Buttons are either pressed, or not. Sliders have a range of values. You can't use a button in place of a slider.

I would like to click on momentary buttons, every click makes it move 10 degrees.
Exactly like this project, but only adding support for another servo (servo on pin D9 & D10) http://blog.renatopeterman.com.br/?p=164 and in english if possibe.
I do not have any programming knowlege, so would really appreciate it if anyone could send me the modified source, or a similar pan tilt project source someone might have done to pan and tilt two servos via arduino.

Please help with thIis, I have tried this now for about 6 months now.

Thank you

Good day

I have managed to get a C++ app working, turning the led on pin 13 on and off.
Follow this link for example: http://arduino.cc/playground/Interfacing/CPPWindows, but i would now like to use the same button to make a servo move about 10 degrees every time i click the button.
Please give me some input on this, or links to similar C++ or C# apps. 

PaulS

Quote
but i would now like to use the same button to make a servo move about 10 degrees every time i click the button.

No change is needed to the PC application. The Arduino simply needs to respond differently to the same input.

Hi, Thank you for the quick responce

Does that mean that I only need to change the scetch to perform the function, If so, anyone out the willing to try this please.I am reletavely new to the Programming world, and the link above is actualy the first app that I have ever written.

Thank you,
Riaan Deyzel

PaulS

Quote
Does that mean that I only need to change the scetch to perform the function

Yes.

Quote
If so, anyone out the willing to try this please.

Aren't you? There are sample sketches to control a servo in a variety of ways. Look at them to understand how a servo is controlled. Then, change the source of the control data to the serial data that you receive.

Good day

Will the following code work to control a servo conected to Digital Pin9?
I am planning to copy the C++ Button & create another button, but just changing the value sent to '00'.
Code: [Select]
/*
* Serial Read Servo
* -----------------
* Moves a Servo connected to digital pin 9.
* The Servo will move the number given by a
* ASCII number read from the serial port.
* One Servo with two buttons (Pan Left & Pan Right)
* Created 10 March 2011
*/

int Servo1 = 9;    // select the pin for Servo1
int val = 0;       // variable to store the data from the serial port

void setup() {
pinMode(Servo1,OUTPUT);    // declare Servo1 pin as output
Serial.begin(9600);        // connect to the serial port
}
void loop () {
val = Serial.read();      // read the serial port

if (val > '0' && val <= '180' )  //Button1 Pan Left represented by a '0'
if (val > '00' && val <= '180' ) //Button2 Pan Right represented by a '00'
val = val - '0';                 //Read Value from Button1
val = val - '00';                //Read Value from Button2
for(int i=0; i<val; i++)         //Button1 Pan Left
for(int i=00; i<val; i++)        //button2 Pan Right

Serial.println("MOVE!");         //message to display in Serial Monitor
digitalWrite(Servo1,val);
delay(15);
}


Feel free to input any sugestions.

Here is my C++ Code & Screensot of my Goal:

Code: [Select]
#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::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->SuspendLayout();
//
// serialPort1
//
this->serialPort1->PortName = L"COM17";
//
// button1
//
this->button1->Location = System::Drawing::Point(3, 37);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
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(148, 37);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Right";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click_1);
//
// button3
//
this->button3->Location = System::Drawing::Point(85, 37);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(57, 23);
this->button3->TabIndex = 2;
this->button3->Text = L"Center";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(225, 99);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Arduino Control";
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
      this->serialPort1->PortName = "COM17";  // Replace with your COM port!
      this->serialPort1->Open();
      this->serialPort1->Write( "0" );  // 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 = "COM17";  // Replace with your COM port!
      this->serialPort1->Open();
      this->serialPort1->Write( "00" );  // 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 = "COM17";  // Replace with your COM port!
      this->serialPort1->Open();
      this->serialPort1->Write( "000" );  // In the future,  you'll expand on this
                                        // to write your custom data to the board
      this->serialPort1->Close();
}
};
}




Screenshot: http://robotcontrol.yolasite.com/ardiuno-stuff.php

Please help with this, as I am still a newbie to the Arduino & code.

PaulS

Quote
Will the following code work to control a servo conected to Digital Pin9?

No.

The Serial.read() function reads 1 byte. The value in that byte will be between 0 and 255. That byte represents ONE character.

'0' is one character. '00' is not one character.

Quote
I am planning to copy the C++ Button & create another button, but just changing the value sent to '00'.

Something wrong with sending '1' or '2', instead? Why are you TRYING to make life difficult for yourself?

Thanks PaulS for your input so far, you do contribute very valueble information, and you definitely know alot about coding and hardware.
I realy do appreciate it.
Please note that I have now created 3 more buttuns for a second servo, but they are not coded in yet to do anything, so  ignore buttons 4 through 6 in the C++ Code.

Revised Scetch:

Code: [Select]
/*
* Serial Read Servo
* -----------------
* Moves a Servo connected to digital pin 9.
* The Servo will move the number given by a
* ASCII number read from the serial port.
* One Servo with two buttons (Pan Left & Pan Right)
* Created 10 March 2011
*/

int Servo1 = 9;                  // select the pin for Servo1
int val = 0;                        // variable to store the data from the serial port

void setup() {
pinMode(Servo1,OUTPUT);    // declare Servo1 pin as output
Serial.begin(9600);              // connect to the serial port
}
void loop () {
val = Serial.read();              // read the serial port

if (val > '1' && val <= '180' )   //Button1 Pan Left represented by a '1'
if (val > '2' && val <= '180' )   //Button2 Pan Right represented by a '2'
if (val > '3' && val == '90' )    //Button3 Center represented by a '3'

val = val - '1';                      //Read Value from Button1 converted from character to number
val = val - '2';                      //Read Value from Button2 converted from character to number
val = val - '3';                      //Read Value from Button3 converted from character to number

for(int i=1; i<val; i++)           //Button1 Pan Left
for(int i=2; i<val; i++)           //button2 Pan Right
for(int i=3; i<val; i++)           //button3 Center

Serial.println("MOVE!");         //message to display in Serial Monitor
digitalWrite(Servo1,val);        //Write Value to Servo1
delay(15);                           //Delay for Servo1
Serial.flush();                      //Flush Serial Data, to prevent intermittent movement of servo
return;
}

C++ Code:

Code: [Select]
#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::Windows::Forms::Button^  button4;
private: System::Windows::Forms::Button^  button5;
private: System::Windows::Forms::Button^  button6;
private: System::Windows::Forms::Label^  label2;


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->button4 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// serialPort1
//
this->serialPort1->PortName = L"COM17";
//
// 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(199, 37);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 63);
this->button2->TabIndex = 1;
this->button2->Text = L"RIGHT";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click_1);
//
// button3
//
this->button3->Location = System::Drawing::Point(105, 37);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(68, 63);
this->button3->TabIndex = 2;
this->button3->Text = L"CENTER";
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);
//
// button4
//
this->button4->ForeColor = System::Drawing::SystemColors::ControlDark;
this->button4->Location = System::Drawing::Point(105, 140);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(68, 60);
this->button4->TabIndex = 6;
this->button4->Text = L"CENTER";
this->button4->UseVisualStyleBackColor = true;
//
// button5
//
this->button5->ForeColor = System::Drawing::SystemColors::ControlDark;
this->button5->Location = System::Drawing::Point(199, 140);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(75, 60);
this->button5->TabIndex = 5;
this->button5->Text = L"DOWN";
this->button5->UseVisualStyleBackColor = true;
//
// button6
//
this->button6->ForeColor = System::Drawing::SystemColors::ControlDark;
this->button6->Location = System::Drawing::Point(3, 140);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(75, 60);
this->button6->TabIndex = 4;
this->button6->Text = L"UP";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->ForeColor = System::Drawing::SystemColors::ButtonShadow;
this->label2->Location = System::Drawing::Point(0, 124);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(101, 13);
this->label2->TabIndex = 7;
this->label2->Text = L"ARDUINO PIN D10";
this->label2->Click += gcnew System::EventHandler(this, &Form1::label2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(298, 212);
this->Controls->Add(this->label2);
this->Controls->Add(this->button4);
this->Controls->Add(this->button5);
this->Controls->Add(this->button6);
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 = "COM17";  // 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 = "COM17";  // 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 = "COM17";  // 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) {
}
};
}




Thank you very much for the help so far, I would like to test this code tonight, and hope to move a servo left to right and center it.

Please note that I have now moved this Post to this link http://arduino.cc/forum/index.php/topic,54994.0.html, as I no longer use C# or VB to control a servo,  I now use C++ in Windows, and got it working to an certain extent.

I Still need help on this new Post Please

Go Up