Go Down

Topic: How do I parse Arrow Keys from Telnet Client (Read 1 time) previous topic - next topic

pylon

@PaulS: you forgot the smiley. At least I hope it wasn't meant seriously.

PaulS

Quote
@PaulS: you forgot the smiley. At least I hope it wasn't meant seriously.

Yeah, I forgot it.

newb91

Hi guys,

I'm working on a very similar project as to that outlined here. New to the Arduino world and enjoying it so far!  :)

I want to be able to control a number of servo's by sending keyboard keystrokes i.e. not having to press the enter key each time I want to increment servo position. Currently in the early stages of development; I've used the code supplied by MF4040 and adjusted it to my own needs.  I've started with 2 servos, which increment/decrement their position on pressing the WASD keys (as shown in code below).

I'm connecting to the arduino via a telnet connection using PuTTY...

Code: [Select]
//-------Headers---------------------------------------------------------
#include <SPI.h>
#include <WiFi.h>
#include <Servo.h>

//-------Varriables---------------------------------------------------------
char ssid[] = "Addon";     //  Broadcasted SSID
char pass[] = "mypass";
int status = WL_IDLE_STATUS;
WiFiServer server(23);            // Default Port 23
boolean alreadyConnected = false; // Flag to detect if already connected

Servo servo0;
Servo servo1;
Servo servo2;

int servo0pos, servo1pos = 90;
//-------Setup---------------------------------------------------------
void setup()
{
  Serial.begin(9600);   // Initialize Serial 9600 BAUD
  Serial.println("'WiFi Servo Control WASD keys' loaded"); //To let me know whats loaded
  while (!Serial) {;}   // Wait for Serial to connect

// Check WiFi shield is connected
  if (WiFi.status() == WL_NO_SHIELD)
  {           // If WiFi shield is not detected
    Serial.println("WiFi shield not present");   // Print error to Serial
    while(true);                                 // Lock Up and wait for reset
  }
 
// Attempt to connect to Wifi network:
  while ( status != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);   
    status = WiFi.begin(ssid, pass);             // Start session for given SSID
    delay(10000);                                // Wait 10 seconds for response
  }
 
  server.begin();             // Start the Server
  printWifiStatus();          // Print Connected Status
 
  servo0.write(servo0pos);    //set initial servo position
  servo1.write(servo1pos);
  servo0.attach(6);          //the pin for the servo control
  servo1.attach(8);
 
}

//-------Main Loop---------------------------------------------------------
void loop()
{
  WiFiClient client = server.available();   // Wait for a new client

  // When the client sends the first byte, say hello:
  if (client)
  {
    if (!alreadyConnected)
    {
      client.flush();          // clear out the input buffer:
      Serial.println("Laptop has connected to Arduino");
      client.println("Hello Laptop, From Tri-Track!");
      alreadyConnected = true;
    }
 
  while (client.connected())
  {
  while (client.available())
  {
    if (client.available())
    {   
      char input = client.read();   // Read the bytes incoming from the client
      Serial.write(input);          // Echo the bytes to the server
      delay(2);  //slow looping to allow buffer to fill with next character
     
     switch (input)
     {
    case 'w':
      servo0pos = servo0pos + 30;
      servo0.write(servo0pos);
      break;
    case 's':
      servo0pos = servo0pos - 30;
      servo0.write(servo0pos);
      break;
    case 'a':
      servo1pos = servo1pos + 30;
      servo1.write(servo1pos);
      break;
    case 'd':
      servo1pos = servo1pos - 30;
      servo1.write(servo1pos);
      break;
    default:
      delay(5);
    }
 
  if (servo0pos > 180) //Let's servo position values stay in scope.
  {
    servo0pos = 180;
  }
  if (servo1pos > 180)
  {
    servo1pos = 180;
  }
  if (servo0pos < 0)
  {
    servo0pos = 0;
  }
  if (servo1pos < 0)
  {
    servo1pos = 0;
  }
   
   input = '0'; //clears user input
  }
  }
}
}
}
//-------Print WiFi Status Subroutine-------------------------------------------------
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}


I know my code is less than robust! Still learning!  :)

I want to be able to change servo position on a single keystroke and not need to hit the Enter key each time

If anyone on this thread could give me a helping hand it would be much appreciated!

PaulS

Quote
If anyone on this thread could give me a helping hand it would be much appreciated!

What do you need help with?

newb91

Quote
What do you need help with?


At the moment I can adjust servo position on entering W,A,S,D followed by a carriage return into the Telnet client. I want to be able to read if the keys are being pressed in real time and adjust servo position accordingly i.e. not have to hit 'Enter' after each keystroke.

I hope that clarifies?

edit: formatting

Go Up