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();
}
}
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.
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.
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?
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.