Pages: [1]   Go Down
Author Topic: Serial Servo Control  (Read 355 times)
0 Members and 1 Guest are viewing this topic.
Essex UK
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm new; please be gentle! smiley-red

I have a question regarding servos and serial control (as lots of people seem to have).

I am controlling four standard servos from an Uno and using a HyperTerminal emulator to allow keyboard keystrokes to send servos to different positions.  All works good. Example line; 

Code:
else if (moveServo == '4') { servo2.write(1); } // left

My question is; is there a line of code I can put in my sketch that basically says if no keys are being pressed, send servoX to positionX, kind of like assigning a home/default position?

I’ve been looking around for answers for a while but sorry if this is a simple one.

Thanks.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How does your sketch (which we can't see) decide that no key presses are being received?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Faraday Member
**
Karma: 68
Posts: 2806
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use the 'switch case', in 'default' you can do what you want to do.
Code:
switch (var) {
  case label:
    // statements
    break;
  case label:
    // statements
    break;
  default:
    // statements  <<<<< send servoX to positionX, kind of like assigning a home/default position
}
Logged

The way you have it in your schematic isn't the same as how you have it wired up! That goes for me too.

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 118
Posts: 4338
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is possible to do what you want.  You would need to wait a while before deciding that no keys are being depressed otherwise the servos would constantly go to their home positions.  This should be easy to incorporate into your code so please post it, using code tags, so that more advice can be given.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My question is; is there a line of code I can put in my sketch that basically says if no keys are being pressed, send servoX to positionX, kind of like assigning a home/default position?
No keys are being pressed on what? Does your Arduino have keys? Mine do not.

If you are using the Serial Monitor to send data, it only sends data ONCE when the enter key is pressed (or the Send icon is pressed).

You need to clarify what you mean by "no keys are pressed".
Logged

Essex UK
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your quick replies! Please see code below which has been ripped from another thread on this forum.

Code:
#include <Servo.h>

int moveServo;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4; // name servos


void setup()
{
 
  servo1.attach(2);
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  Serial.begin(9600);
  Serial.println("      Cam L  7  Forward   8  Cam R  9");
  Serial.println("      Left   4  Straight  5  Right  6");
  Serial.println("      CamDn  1  Reverse   2  CamUp  3");
  Serial.println("      Stop   0");
  Serial.println(); // on screen notes
 
}

void loop () {
 
  if (Serial.available() > 0) {
    moveServo = Serial.read();
   
    if (moveServo == '0') { servo1.write(90); }  // stop
    else if (moveServo == '1') { servo4.write(1); } // cam down
    else if (moveServo == '2') { servo1.write(1); } // reverse
    else if (moveServo == '3') { servo4.write(180); } // cam up
    else if (moveServo == '4') { servo2.write(1); } // left
    else if (moveServo == '5') { servo2.write(90); } // straight
    else if (moveServo == '6') { servo2.write(180); } // right
    else if (moveServo == '7') { servo3.write(1); } // cam left
    else if (moveServo == '8') { servo1.write(180); } // forward
    else if (moveServo == '9') { servo3.write(180); } // cam right
  }
}

As you can see its v.basic, I have never coded anything before.  I was hoping it would be as simple as an "else if" line with a 'no serial' comand then a 'servo write' comand!

Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was hoping it would be as simple as an "else if" line with a 'no serial' comand then a 'servo write' comand!
No, I'm afraid it isn't that simple.
Serial is really slow, so the "no serial" condition is pretty much most of the time, as someone already pointed out.
What you should do is note the time ("millis ()" ) each time you receive a character, then every time through the main loop, check to see if 'x' seconds have elapsed.
As usual, the blink without delay tutorial will give you some clues.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: