PC Keyboard

Hi!
I write a simple code to controll two moded (360) servos from a pc keyboard.

My question is how to send signal to servos only when is key hold down? and when key is not pressed do something else.. (like a stop the servos)

Thanks in advance!

Sorry for my bad english :slight_smile:

Here is my code:

#include <Servo.h>
Servo servo_lijevi;
Servo servo_desni;


void setup()
{
  servo_lijevi.writeMicroseconds(1462);
  
  servo_desni.writeMicroseconds(1499);
  
  servo_lijevi.attach(9); 
  servo_desni.attach(7);
  Serial.begin(9600); 
}

void loop()
{
  if (Serial.available() > 0)
  {
  //while (Serial.available() == 0);
  int pozicija = Serial.read() - '0';
  
  //SERVO_LIJEVI 
  if (pozicija == 7) 
  {
    Serial.println("Servo_LIJEVI_naprijed_95"); 
    servo_lijevi.write(95);
	delay(20);
  }
   else if (pozicija == 4)
  {
    Serial.println("Servo_LIJEVI_stop");
    servo_lijevi.writeMicroseconds(1462);
	delay(20);
  }
  else if (pozicija == 1)
  {
    Serial.println("Servo_LIJEVI_nazad_85");
    servo_lijevi.write(85);
	delay(20);
  }
 
  //SERVO_DESNI
   else if (pozicija == 9)
  {
    Serial.println("Servo_DESNI_naprijed_100");
    servo_desni.write(100);
	delay(20);
  } 
  else if (pozicija == 6)
  {
    Serial.println("Servo_DESNI_stop");
    servo_desni.writeMicroseconds(1499);
	delay(20);
  } 
  else if(pozicija == 3)
  {
    Serial.println("Servo_DESNI_nazad_85");
    servo_desni.write(85);
	delay(20);
  }
 
   //ZAJEDNICKI RAD
  else if (pozicija == 8)
 {
   Serial.println("LIJEVI_DESNI_naprijed");
   servo_lijevi.write(95);
   servo_desni.write(85);
   delay(20);
 } 
 else if(pozicija == 5)
 {
   Serial.println("LIJEVI_DESNI_stop");
   servo_lijevi.writeMicroseconds(1462);
   servo_desni.writeMicroseconds(1499);
   delay(20);
 }
 else if(pozicija == 2)
 {
   Serial.println("LIJEVI_DESNI_nazad_85");
   servo_lijevi.write(85);
   servo_desni.write(95);
   delay(20);
 }
 
 else if(pozicija == 0)
 {
	 Serial.println("LIJEVI_DESNI_FULL_SPEED");
	 servo_lijevi.write(180);
	 servo_desni.write(0);
	 delay(20);
 }
   //KRIVI UNOS
   else
   {
     Serial.println("KRIVI UNOS");
   }
   Serial.flush();
 }
}

Your code seems to use the numeric keypad as a simple joystik operation. You should look at the switch ( n )  case construct, it is more fitting for this type of code.

Servos stop on their own. Your code tells the "lijevi"servo to move to position "85" when you push "2". If you hold "2" down, it will receive this command repeatedly, and not move (after the first move has completed). If you mean for it to keep moving away from "90" as long as you hold "2", then you must do something linke
servo_lijevi.write(servoe_lijevi.read()-5) which will move it another 5 degrees for as long as you send "2" from the keyboard.

If the servos are continous, then your code will set the servo speed to "85" (a slow move in one direction) and keep doing that for every "2" (it is already moving, so additional "2"s have no effect, like before). BUT letting go of "2" will keep not stop it (as it remembers the last write). In your loop you should do an "else" to act on (Serial.available is not >0) and send the servo_x.write(nn) that makes it stop.

Another hint: If you hold a key on a pc keyboard for a few seconds, it will send many "2"-characters very quickly, which ar buffered in the Arduino input buffer, so even if you let go, it may still have lots more movement to go.

The English is OK :wink:

If the servos are continous, then your code will set the servo speed to "85" (a slow move in one direction) and keep doing that for every "2" (it is already moving, so additional "2"s have no effect, like before). BUT letting go of "2" will keep not stop it (as it remembers the last write). In your loop you should do an "else" to act on (Serial.available is not >0) and send the servo_x.write(nn) that makes it stop.

Last write ("2" moving 85) stay in buffer until you press another key, so the "else" will never run and servo will never stop on release the key..

My question is how to send signal to servos only when is key hold down? and when key is not pressed do something else.. (like a stop the servos)

What key? One on the PC? If so, what application is caring about the keystrokes? That application should send one value when the key is pressed, and another when the key is released. It could, for instance, send s when the S key is pressed, and S when the S key is released.

The Arduino could then start and action when the lower case version arrived, and stop the action when the upper case letter arrived.

If you are talking about the Serial Monitor as the application that cares about the key presses, forget it. That application is not capable of detecting key press and key release events.

What key? One on the PC? If so, what application is caring about the keystrokes? That application should send one value when the key is pressed, and another when the key is released. It could, for instance, send s when the S key is pressed, and S when the S key is released.

The Arduino could then start and action when the lower case version arrived, and stop the action when the upper case letter arrived.

If you are talking about the Serial Monitor as the application that cares about the key presses, forget it. That application is not capable of detecting key press and key release events.

Yes, pc keyboard, i mentioned in the first post.

Application that caring keystroke are putty... Is possible to program that in arduino ide? or i will need to use another program language?

Is possible to program that in arduino ide?

Program what? The behavior of the executable called putty.exe? No.

You can write your own application (not using the Arduino IDE) that cares about key press and key release events, and sends serial data when an event of interest occurs. C# would be perfect for the job.

Thanks to everyone, i decided to use visual basic and Arduino IDE ... It will do the trick, ty again :slight_smile: