Go Down

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

Riaan Deyzel

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.

Riaan Deyzel

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.

Riaan Deyzel

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?

Go Up