Help with Servo motors and Serial

Hi,

I'm using an Arduino Uno board along with a Parallax Inc Servo Motor, 6 → 8.4 V, 180 rpm.

I am trying to use this to move a piece of paper to change an intensity value on a camera or photoresistor.

I am hoping to make it rotate fast at first until it finds the minimum then pause. After this it should check the intensity again and then rotate backwards slowly until it finds it (due to overshooting it before). Then repeating this in the opposite direction slower and stopping once more at the minimum value.

I was wondering how I would do this as I previously was using several if statements and delays.

#include<Servo.h>


Servo halfWave; //Servo Motor name

const int servPin1 = 9; //Pin the Servo is attached to (See Below)


void setup() {
 

 Serial.begin(9600);

 pinMode(servPin1, OUTPUT);

 halfWave.attach(9);


}


void loop() { 


 Serial.println("Ready"); //Signal to LabVIEW

 while(Serial.available() == 0);

 String intensity = Serial.readString(); 

 int intenVal = intensity.toInt(); //Need the integer value of the Intensity


 do {

   halfWave.write(180);  //If the intensity isn't right then it rotates the servo continuously until it is low 

enough

 }while(intenVal != 200);


}

This is currently what I have to find the minimum the first time, but now I am wondering how it can check it again after this and then do another "do" loop but at a slower speed.

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

I presume you are using a continuous rotation servo - which is just a DC motor in a convenient package.

Don't use the String (capital S) class - it can cause memory problems in the small Arduino memory.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

It doesn't seem to work with string rather than String.

And it isn't exactly the Servo I am having trouble with, its more my code.

The does doesn't attempt to do what you explained.

When the thing is just spinning, how do you expect it to get exactly 200 intensity without over or under? Use greater-than and less-than comparisons.

conz2512:
It doesn't seem to work with string rather than String.

It will if the code is correct.

And it isn't exactly the Servo I am having trouble with, its more my code.

Post the latest version of your code

Make a simpler version of the program that works with a fixed value rather than a value that comes from the serial port.

...R