Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: conz2512 on Jul 11, 2016, 04:02 pm

Title: Help with Servo motors and Serial
Post by: conz2512 on Jul 11, 2016, 04:02 pm
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.



Code: [Select]

#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.

Title: Re: Help with Servo motors and Serial
Post by: Robin2 on Jul 11, 2016, 04:44 pm
Please modify your post and use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum (http://forum.arduino.cc/index.php?topic=149014.0)

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 (http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data. There is also a parse example.

...R
Title: Re: Help with Servo motors and Serial
Post by: conz2512 on Jul 11, 2016, 05:13 pm
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.
Title: Re: Help with Servo motors and Serial
Post by: MorganS on Jul 11, 2016, 06:36 pm
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.
Title: Re: Help with Servo motors and Serial
Post by: Robin2 on Jul 11, 2016, 08:12 pm
It doesn't seem to work with string rather than String.
It will if the code is correct.

Quote
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