Pages: [1]   Go Down
Author Topic: Keyboard controlled robot  (Read 539 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i purchased a robot shop rover and at the moment i have the basic wasd keyboard control code working very nicely. I also have a separate servo left and right manipulation code working nicely. I need to combine them for a presentation tommorow afternoon. Here are the two codes if someone could please mix them so that they both function together that would excellent. Thankyou
Code 1 Basic wasd control
/*To control the rover, Copy and paste the code below into the Arduino software*/
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

void setup(void)
{
  int i;
  for(i=5;i<=8;i++)
  pinMode(i, OUTPUT);
  Serial.begin(9600);
  Serial.println("setup");
}

void loop(void)
{
  while (Serial.available() < 1) {} // Wait until a character is received
  char val = Serial.read();
  int leftspeed = 120; //255 is maximum speed
  int rightspeed = 120;
  switch(val) // Perform an action depending on the command
  {
    case 'w'://Move Forward
    case 'W':
      forward (leftspeed,rightspeed);
      break;
    case 's'://Move Backwards
    case 'S':
      reverse (leftspeed,rightspeed);
      break;
    case 'a'://Turn Left
    case 'A':
      left (leftspeed,rightspeed);
      break;
    case 'd'://Turn Right
    case 'D':
      right (leftspeed,rightspeed);
      break;
    default:
      stop();
      break;
  }
}

void stop(void) //Stop
{
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
  Serial.println("stop");
}

void forward(char a,char b)
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
  Serial.println("forward");
}

void reverse (char a,char b)
{
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
  Serial.println("reverse");
}

void left (char a,char b)
{
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
  Serial.println("left");
}

void right (char a,char b)
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
  Serial.println("right");
}

 Code 2 servo control
/*
 * NewSerialServo
 * --------------
 * Servo control from the Serial port
 *
 * Alteration of the control interface to use < and > keys
 * to slew the servo horn left and right.  Works best with
 * the Linux/Mac terminal "screen" program.
 *
 * Created 10 December 2007
 * copyleft 2007 Brian D. Wendt
 * http://principialabs.com/
 *
 * Adapted from code by Tom Igoe
 * http://itp.nyu.edu/physcomp/Labs/Servo
 */ 
 
/** Adjust these values for your servo and setup, if necessary **/ 
int servoPin     =  2;    // control pin for servo motor 
int minPulse     =  600;  // minimum servo position 
int maxPulse     =  2400; // maximum servo position 
int turnRate     =  200;  // servo turn rate increment (larger value, faster rate) 
int refreshTime  =  30;   // time (ms) between pulses (50Hz) 
 
/** The Arduino will calculate these values for you **/ 
int centerServo;         // center servo position 
int pulseWidth;          // servo pulse width 
int moveServo;           // raw user input 
long lastPulse   = 0;    // recorded time (ms) of the last pulse 
 
 
void setup() { 
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin 
  centerServo = maxPulse - ((maxPulse - minPulse)/2); 
  pulseWidth = centerServo;   // Give the servo a starting point (or it floats) 
  Serial.begin(9600); 
  Serial.println("      Arduino Serial Servo Control"); 
  Serial.println("Press < or > to move, spacebar to center"); 
  Serial.println(); 

 
void loop() { 
  // wait for serial input 
  if (Serial.available() > 0) { 
    // read the incoming byte: 
    moveServo = Serial.read(); 
 
    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really) 
    if (moveServo == 44) { pulseWidth = pulseWidth - turnRate; } 
    if (moveServo == 46) { pulseWidth = pulseWidth + turnRate; } 
    if (moveServo == 32) { pulseWidth = centerServo; } 
 
    // stop servo pulse at min and max 
    if (pulseWidth > maxPulse) { pulseWidth = maxPulse; } 
    if (pulseWidth < minPulse) { pulseWidth = minPulse; } 
 
    // print pulseWidth back to the Serial Monitor (uncomment to debug) 
    // Serial.print("Pulse Width: "); 
    // Serial.print(pulseWidth); 
    // Serial.println("us");   // microseconds 
  } 
 
  // pulse the servo every 20 ms (refreshTime) with current pulseWidth 
  // this will hold the servo's position if unchanged, or move it if changed 
  if (millis() - lastPulse >= refreshTime) { 
    digitalWrite(servoPin, HIGH);   // start the pulse 
    delayMicroseconds(pulseWidth);  // pulse width 
    digitalWrite(servoPin, LOW);    // stop the pulse 
    lastPulse = millis();           // save the time of the last pulse 
  } 

Logged

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

Quote
Here are the two codes if someone could please mix them so that they both function together that would excellent.
I could take the odd numbered lines from one file and the even numbered lines from the other file and put them in a new file. I could sort them alphabetically.

Of course, the result wouldn't compile. Nor, would it do what you want. Of course, that last statement is only a supposition, since you didn't bother saying what you want the final program to do.

If you were making the presentation to me, and couldn't explain the code, I'd give you a failing grade, even if the robot could dance the entire Ring Opera in 20 minutes.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My apoligies. And the presentation would be stated allot better than that trust me. What i need to be able to do is control the rover, front, back, left right like it already does in the wasd code. And control the servo to move the mount its upporting left and right like the servo coder can do on its own. I need to be able to do both at the same time.
Logged

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

Quote
I need to be able to do both at the same time.
So, specifically, what is the problem?

What have you tried, in terms of merging the code?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is once I make an attempt at merging the two codes and I can only move te rover not the servo I have setup. Basically I've tried taking each section of servo code and putting it where I believed it belonged in the wasd control code. It all condenses however does not allow me to control servos and rover at same time.
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking at the code right now, and it's soooooo difficult to read without the code tags.

Please, go and Modify that post. Select the code text, hit the # key above the  smiley-sweat smily so that the code gets

Code:
formatted like this.
Logged

The Elders of the Internet know who I am.....

Pages: [1]   Go Up
Jump to: