Servo keeps returning to 0 position

Very frustrated - new to this stuff, so please bear with me.

I have written code in VS and it controls my servo exactly as expected, except after it goes to the specified degree it returns back to zero (starting position). I have included the code for VS and Arduino. What am I doing wrong or is this a function of the servo. it needs to hold the position I stated each time until I give it another position to travel to.

Please help…

here is Arduino code

#include <Servo.h>

Servo myservo;

int val;
void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {
}
void serialEvent (){
  val = Serial.parseInt();
  {
    myservo.write(val);
  }
}

My first question is why bother using serialEvent() ? You might just as well just test whether serial data is available and if so, act on it.

SerialEvent is the [u]problem[/u]. Each time a single character comes in, you attempt to read an integer.

Sorry, here is VS code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace Servo_concept
{
public partial class Form1 : Form
{

SerialPort port;
private int deg_Click;

public Form1()
{
InitializeComponent();
init();
run_Servo();
}

private void init()
{
port = new SerialPort();
port.PortName = “COM5”;
port.BaudRate = 9600;

try
{
port.Open();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
private void lowButton_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
deg_Click = 0;
run_Servo();
}
}

private void medButton_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
deg_Click = 30;
run_Servo();
}
}

private void highButton_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
deg_Click = 60;
run_Servo();
}
}

private void maxButton_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
deg_Click = 90;
run_Servo();
}
}
private void run_Servo()
{
if (port.IsOpen)
{
port.WriteLine(deg_Click.ToString());
clickValue.Text = "Click = " + deg_Click.ToString();
}
}
}
}

Studio code.txt (1.92 KB)

What does WriteLine() do? I bet it writes the string followed by a carriage return and linefeed. Let's imagine you WriteLine("90")...

The 9 arrives in the buffer and you get a serial event. ParseInt() sees that 9 and it waits until it sees the 0 and the CR. Since CR is not a number, it believes that it has seen the whole number and it returns 90.

The servo moves to position 90.

But 10 milliseconds later, the LF arrives, triggering serialEvent(). parseInt() sees this isn't a number so it waits for one whole second for you to send the number. But you send nothing and parseInt() must return an integer. So it returns 0.

After the one second waiting for the non existent integer, the servo moves to position 0.

jremington: SerialEvent is the [u]problem[/u]. Each time a single character comes in, you attempt to read an integer.

What does Serial.read() return. ( Hint: it's not a char)

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thanks guys for all your help, didn't expect that fast of an answer. I'm new to using the Arduino and am trying to stumble through.

I change a few things and it is working as expected now.

Again, thanks for you help

Please post your working code so that others with the same problem can see your solution.

here is the working code:

Visual Studio:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports;

namespace Servo_concept { public partial class Form1 : Form {

SerialPort port; private int deg_Click;

public Form1() { InitializeComponent(); init(); run_Servo(); }

private void init() { port = new SerialPort(); port.PortName = "COM5"; port.BaudRate = 9600;

try { port.Open(); } catch (Exception e1) { MessageBox.Show(e1.Message); } } private void lowButton_Click(object sender, EventArgs e) { deg_Click = 0; run_Servo(); }

private void medButton_Click(object sender, EventArgs e) { deg_Click = 30; run_Servo(); }

private void highButton_Click(object sender, EventArgs e) { deg_Click = 60; run_Servo(); }

private void maxButton_Click(object sender, EventArgs e) { deg_Click = 90; run_Servo(); } private void run_Servo() { port.Write(deg_Click.ToString()); clickValue.Text = "Click = " + deg_Click; }

} }

the Arduino code remained the same as above