Servo control with hyperterminal

Hey,
I apologise in advance for posting a separate query on this thread i just could not for the life of me find the new post button on the forums :frowning:
I am undergoing a project and my ultimate aim is to receive serial angular data in real time on my PC and adjust a servo using arduino uno according to the change in angle, i need to do this in just a couple weeks and i am still in the very early stages.
My current problem is to do with getting hyper terminal to work with arduino UNO. I am hoping to control the servo with hyperterminal, i have managed to use just arduino to do a basic 'sweep' movement as well as moving to a set angle from 0 to 180 degrees. I now want to be able to do the same but on hyperterminal so that i can eventually use java to create a nice interface in which the angle of the servo can be set.
I have downloaded hyperterminal as i have windows 7, i then set up a new connection called arduino, selected the appropriate COM3 port, baud 9600, 8 bit, 1 bit stop, flow control - none. this process then takes me to the blank screen of hyperterminal, what do i do from here? do i have to write more code in arduino to set the angle from hyperterminal? how do i know what commands to put into hyperterminal? does it use a programming language?
the code used in arduino is:
#include <Servo.h>

Servo myservo;

void setup()
{
myservo.attach(9);
myservo.write(0); // set servo to mid-point
}

void loop() {}

Im completely new to arduinos, i have a very basic skill in matlab and java but no experience in C. And i know little about electronics in general, i am keen to learn but for the purpose of this projects i dont have the time to learn the foundations

thanks for any help in advance!

I am undergoing a project and my ultimate aim is to receive serial angular data in real time on my PC

From where?

and adjust a servo using arduino uno according to the change in angle

Pretty simple.

My current problem is to do with getting hyper terminal to work with arduino UNO.

Start with the Serial Monitor, then.

I now want to be able to do the same but on hyperterminal

Why?

so that i can eventually use java to create a nice interface in which the angle of the servo can be set.

This doesn't follow. Hyperterminal and Java are completely unrelated. Like Venus and Snickers.

how do i know what commands to put into hyperterminal? does it use a programming language?

No, it's a data entry program. Enter whatever data you have programmed the Arduino to respond to.

the code used in arduino is:

Missing a lot, like code to see of there is serial data, code to read and store that serial data, code to parse and interpret that serial data, and code to actually use that serial data.

Hey Paul,

Thank you for your swift reply.

The experiment is for MAV pitch control. I wish to receive serial data from an angular sensor attached to my MAV test rig and then use this in a feedback loop with arduino to control a servo in order to return the MAV model back to its trim position.

I do not know a lot about hyperterminal and its interaction with java but my supervisor has asked me to firstly get servo control from a hyperterminal input and then when accomplished get a java programm which relay the same command to hyperterminal hence arduino. I assumed this so that that a nice GUI could be made that allowed the angle to be changed by a 'slider.' I think his eventual aim is to have the feedback loop written in java once the angular senosor has be set up.

I have gotten some code that allows servo movement with hyper terminal input, however the results seem pretty random, i cant type in '180' and it move to that position. It just seems to move by a random amount when pressing integers or characters on my keyboard. I guess hyperterminal has assigned some sort of value to the keys already? The code is:

#include <Servo.h>

Servo myservo;

void setup()
{
Serial.begin(57600);
myservo.attach(9);
}

void loop() {

while (Serial.available() > 0) {
int inByte = Serial.read();
myservo.write(inByte);
Serial.write(inByte);
}
}

Is there some code i need in order to tell arduino only to move the servo once a combination of keys have been pressed?
like a code that says if 'int' is entered then myservo.write() ??
Sorry again if im not making much sense my knowledge is limited

Thank you

Hey Again,

So I got code that lets me select angles from 0 to 9 degrees using:

#include <Servo.h>

Servo myservo;

void setup()
{
Serial.begin(57600);
myservo.attach(9);
}

void loop()
{
if ( Serial.available()) // Check to see if at least one character is available
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
{

myservo.write(ch);
Serial.write(ch);
}
}
}

Simple and silly i know, but im not sure how to write the code to allow any number from 0 to 180 to be entered as this allows only for single digits :S
Cheers

Simple and silly i know, but im not sure how to write the code to allow any number from 0 to 180 to be entered as this allows only for single digits

There are a couple of ways. You could collect each digit into an array, appending a NULL after each digit. When some non-digit character, such as carriage return or line feed arrives, call atoi() with that array.

Or, each time another digit arrives, multiply the previous value by 10, and add the new digit. Actually use the value when a non-digit arrives.

The key, in either case, is that you need to send some kind of end of packet marker.

Without an end of packet marker, what does this collection of input mean?
1274567372165785
With end of packet markers (;), what does this collection of input mean?
127;45;67;37;21;65;7;85;

In one case, it is easy to see how to interpret the data. In the other, it is impossible.

Zoomkat will be along shortly with some example code.

I think ive got it, this seems to work:

#include <Servo.h>

Servo myservo;

void setup()
{
Serial.begin(57600);
myservo.attach(9);
}
const int MaxChars = 3; // an int string contains up to 3 digits and
// is terminated by a 0 to indicate end of string
char strValue[MaxChars+1]; // must be big enough for digits and terminating null
int index = 0;
int angle;// the index into the array storing the received digits
void loop()

{
if( Serial.available())
{
char ch = Serial.read();
if(index < MaxChars && ch >= '0' && ch <= '9'){
strValue[index++] = ch; // add the ASCII character to the string;
}
else
{
// here when buffer full or on the first non digit
strValue[index] = 0; // terminate the string with a 0
angle = atoi(strValue);// use atoi to convert the string to an int
myservo.write(angle);
Serial.println(angle);
index = 0;
}
}

}

Although this wont allow me to put a simple "the angle set is: " comment in the println code :S

You mean like this?

myservo.write(angle);
Serial.print ("The angle set is: ");
Serial.println(angle);
index = 0;

{Taps watch, glares in Zoomkat's general direction}

yeah for some reason when i do that it likes to go a bit weird ha, ah well not crucial to be honest. Now i need to figure out how to have all my inputs in java rather than into hyperterminal, any tips are much appreciated :slight_smile:

Hey again,

Ive been trying to write this java code for days now and I just cannot get it working. Can anyone help please?

I want to be able to enter a number in java and for the servo to move accordingly. I have the arduino coded to accept a 3 integers followed by any 4th key in order to set an angle. for example 090a is 90 degrees 170k is 170 degrees. The 'a' and 'k' could be any key as its effectively an execute command.
I have the RXTX library and have code that successfully communicates with it:


/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package serialswitch;

/**
*

  • @author
    */
    import gnu.io.CommPortIdentifier;
    import gnu.io.PortInUseException;
    import gnu.io.SerialPort;
    import gnu.io.UnsupportedCommOperationException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.util.Enumeration;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

public class SerialSwitch implements ActionListener, Runnable {
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM3", // Windows
};
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 57600;

private SerialPort serialPort;
private OutputStream output;
private ByteArrayOutputStream bout;
private OutputStreamWriter writer;

private boolean on;

public SerialSwitch() {
bout = new ByteArrayOutputStream();
writer = new OutputStreamWriter(bout);
}

@SuppressWarnings("CallToThreadDumpStack")
public void initialize() {
CommPortIdentifier portId = findPortId();
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
serialPort = (SerialPort)portId.open(this.getClass().getName(), TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
output = serialPort.getOutputStream();
serialPort.notifyOnDataAvailable(true);
} catch (PortInUseException
| UnsupportedCommOperationException
| IOException e) {
e.printStackTrace();
}
}

private CommPortIdentifier findPortId() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier)portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
return portId;
}

public synchronized void close() {
if (serialPort == null) {
return;
}
serialPort.close();
}

private void createAndShowGui() {
JFrame frame = createFrame();
frame.setVisible(true);
}

private JFrame createFrame() {
JFrame frame = new JFrame("SerialSwitch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(createPane());
frame.pack();
return frame;
}

private JPanel createPane() {
JPanel pane = new JPanel();
pane.add(createButton());
return pane;
}

private JButton createButton() {
JButton button = new JButton("ON");
button.addActionListener(this);
return button;
}

@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton)event.getSource();
onButton(button);
}

@SuppressWarnings("CallToThreadDumpStack")
private void onButton(JButton button) {
try {
String command = getCommand();
writer.write(command, 0, command.length());
writer.flush();
bout.writeTo(output);
button.setText(getButtonText());
on = !on;
bout.reset();
} catch (IOException e) {
e.printStackTrace();
}
}

private String getCommand() {
return on ? "OFF\n" : "ON\n";
}

private String getButtonText() {
return on ? "ON" : "OFF";
}

@Override
public void run() {
createAndShowGui();
}

public static void main(String[] args) throws Exception {

Enumeration port_list = CommPortIdentifier.getPortIdentifiers();

while (port_list.hasMoreElements())
{
CommPortIdentifier port_id = (CommPortIdentifier)port_list.nextElement();

if (port_id.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println ("Serial port: " + port_id.getName());
}
else if (port_id.getPortType() == CommPortIdentifier.PORT_PARALLEL)
{
System.out.println ("Parallel port: " + port_id.getName());
}
else
System.out.println ("Other port: " + port_id.getName());
}
SerialSwitch main = new SerialSwitch();
main.initialize();
SwingUtilities.invokeLater(main);
System.out.println("Started");

}
}


This gives the ouput:
Serial port: COM3
Started

It also opens a window in which a on off switch can be pressed.
I now either a input textbox or a slider to change the angle from java but i have no idea how. Is there a send data command? i have search forums and read textbooks but i think maybe its out of my depth currently and for this purpose i simply dont have the time required to learn the foundations.
could someone please help cheers!

Ste

^^ thats exactly what im trying to do .....but failing lol