Pages: 1 2 [3] 4 5   Go Down
Author Topic: Sending Full Words from Serial Monitor  (Read 3242 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the loops are right now.

Code:
#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

int rightLEDs = 9;
int leftLEDs = 10;

byte index = 0;
char inData[30];
char *token = NULL;
byte count = 0;
char EOP = ';';



void setup()
{
  pinMode(rightLEDs, OUTPUT);
  pinMode(leftLEDs, OUTPUT);
  digitalWrite(rightLEDs, HIGH);
  digitalWrite(leftLEDs, HIGH);
  Serial.begin(9600);
  Serial.println("RC Car Project Mark I connected.");
}

void loop()
{
  while(Serial.available() > 0)
  {
    char inByte = Serial.read();
    if(index < 30)
    {
      inData[index] = inByte;
      inData[index+1] = '\0';
    }
    if(inByte == EOP)
    {
      break;
    }
  }

  if(inData[index] == EOP)
  {
    token = strtok(inData, ",");
    if(token == "forward")
    {
      count++;

      if(count == 2)
      {
        int inputSpeed = atoi(token);
        motor1.setSpeed(inputSpeed);
        motor2.setSpeed(inputSpeed);
        motor3.setSpeed(inputSpeed);
        motor4.setSpeed(inputSpeed);
        Serial.print("Running forward at ");
        Serial.print(inputSpeed);
        Serial.print(" / 255 speed for ") ;
      }

      else if(count == 3)
      {
        int duration = atoi(token);
        Serial.print(duration / 10);
        Serial.println(" seconds.");
        motor1.run(FORWARD);
        motor2.run(FORWARD);
        motor3.run(FORWARD);
        motor4.run(FORWARD);
        delay(duration);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
        motor3.run(RELEASE);
        motor4.run(RELEASE);
      }

      token = strtok(NULL, ",");
    }


    if(token == "backward")
    {
      count++;

      if(count == 2)
      {
        int inputSpeed = atoi(token);
        motor1.setSpeed(inputSpeed);
        motor2.setSpeed(inputSpeed);
        motor3.setSpeed(inputSpeed);
        motor4.setSpeed(inputSpeed);
        Serial.print("Running backwards at ");
        Serial.print(inputSpeed);
        Serial.print(" / 255 speed for ") ;
      }

      else if(count == 3)
      {
        int duration = atoi(token);
        Serial.print(duration / 10);
        Serial.println(" seconds.");
        motor1.run(BACKWARD);
        motor2.run(BACKWARD);
        motor3.run(BACKWARD);
        motor4.run(BACKWARD);
        delay(duration);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
        motor3.run(RELEASE);
        motor4.run(RELEASE);
      }

      token = strtok(NULL, ",");
    }

    token = strtok(inData, ",");
    if(token == "rightturn")
    {
      digitalWrite(rightLEDs, HIGH);
      delay(750);
      digitalWrite(rightLEDs, LOW);
      delay(750);

      motor1.setSpeed(100);
      motor2.setSpeed(100);
      motor3.setSpeed(100);
      motor4.setSpeed(100);

      Serial.println("Turning right.");

      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay(2000);

      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);

      digitalWrite(rightLEDs, HIGH);

      token = strtok(NULL, ",");
    }

    token = strtok(inData, ",");
    if(token == "leftturn")
    {
      digitalWrite(leftLEDs, HIGH);
      delay(750);
      digitalWrite(leftLEDs, LOW);
      delay(750);

      motor1.setSpeed(100);
      motor2.setSpeed(100);
      motor3.setSpeed(100);
      motor4.setSpeed(100);

      Serial.println("Turning left.");

      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
      delay(2000);

      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);

      digitalWrite(leftLEDs, HIGH);

      token = strtok(NULL, ",");
    }
  }
}
Logged

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

Quote
I think the loops are right now.
If they are, then the code will do what you want. But, it probably won't. The variable count is still not handled correctly.

Use Serial.print() and Serial.println() to see what is going on.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My motorshield hasn't come yet, therefore I am unable to set this up. How would I fix the count? I thought that the forward was handled already. How does that affect the incremented values  smiley-eek?
Logged

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

Look at where you increment count. Print out what value count has after "forward" or "backward" or whatever the first token is. The variable count starts at zero it gets incremented by one. At that point, it can't be 2 or 3.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So it should start out as 1?
Logged

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

Or test that it has reached 1 or 2.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would I initiate that test? Would it still be defined as zero at the beginning of the program? I test for it being greater than one?
Logged

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

Currently, you have:
Code:
byte count = 0;
count++;
if(count == 2)
{
  // Do something
}
if(count == 3)
{
  // Do something
}

You can change that to:
Code:
byte count = 1;
count++;
if(count == 2)
{
  // Do something
}
if(count == 3)
{
  // Do something
}

or:
Code:
byte count = 0;
count++;
if(count == 1)
{
  // Do something
}
if(count == 2)
{
  // Do something
}

Don't you have an Arduino to actually try stuff on? That's way faster than expecting the forum to test/validate your code for you.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot. I am expecting my motor shield to come this week, but I can't test the program otherwise.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have everything now—my xbee shield and motor shield. I uploaded the program to the Arduino, no problem. I tried sending a command packet—but it looked like the XBee was transmitting each letter, and not showing anything in the terminal window. Plus, the motors aren't even doing anything when I pressed the semicolon. I don't know if it's supposed to do this. I imagined it as a command in the terminal window like forward,255,1000; and then pressing enter.
Logged

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

Quote
I tried sending a command packet—but it looked like the XBee was transmitting each letter, and not showing anything in the terminal window.
The XBee is supposed to transmit each letter. Not showing anything in the Serial Monitor could be a problem. What kind of XBees do you have, and how are they configured? Do they work with a simple test to send data back and forth?

By the way, this is the first time I see mentioned that you are using XBees. Did I miss something?

Quote
I imagined it as a command in the terminal window like forward,255,1000; and then pressing enter.
That should work.
« Last Edit: June 09, 2011, 03:21:28 am by PaulS » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am just using Series 1 modules, there is no need to configure them. I don't know if I made it clear what I meant—as I type the letters are not queuing up in the terminal.
Logged

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

Quote
I am just using Series 1 modules, there is no need to configure them.
Say what? Of course there is.

Quote
I don't know if I made it clear what I meant—as I type the letters are not queuing up in the terminal.
Still not clear. Where are you typing? If you are typing in the Serial Monitor, nothing happens until you hit the send button.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was using the screen function in OS X terminal. I am now using the Serial Monitor. I thought that the Series 1 modules required no configuration. They have been working fine for other programs. It is sending and receiving data fine. In the serial monitor it lets me send it as a whole packet, but it doesn't do anything. When I type in forward,255,1000; nothing happens. The hardware is fine—I've already tested it. I just am not sure why it isn't responding.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Obsessed with Apple
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just in case you were wondering, here's my code. The goal of this project is to make a serial controlled RC car.
I can't seem to figure out what's wrong with it.


Code:
#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

int rightLEDs = 9;
int leftLEDs = 10;

byte index = 0;
char inData[30];
char *token = NULL;
byte count = 1;
char EOP = ';';



void setup()
{
  pinMode(rightLEDs, OUTPUT);
  pinMode(leftLEDs, OUTPUT);
  digitalWrite(rightLEDs, HIGH);
  digitalWrite(leftLEDs, HIGH);
  Serial.begin(9600);
  Serial.println("RC Car Project Mark I connected.");
}

void loop()
{
  while(Serial.available() > 0)
  {
    char inByte = Serial.read();
    if(index < 30)
    {
      inData[index] = inByte;
      inData[index+1] = '\0';
    }
    if(inByte == EOP)
    {
      break;
    }
  }

  if(inData[index] == EOP)
  {
    token = strtok(inData, ",");
    if(token == "forward")
    {
      count++;

      if(count == 2)
      {
        int inputSpeed = atoi(token);
        motor1.setSpeed(inputSpeed);
        motor2.setSpeed(inputSpeed);
        motor3.setSpeed(inputSpeed);
        motor4.setSpeed(inputSpeed);
        Serial.print("Running forward at ");
        Serial.print(inputSpeed);
        Serial.print(" / 255 speed for ") ;
      }

      else if(count == 3)
      {
        int duration = atoi(token);
        Serial.print(duration / 10);
        Serial.println(" seconds.");
        motor1.run(FORWARD);
        motor2.run(FORWARD);
        motor3.run(FORWARD);
        motor4.run(FORWARD);
        delay(duration);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
        motor3.run(RELEASE);
        motor4.run(RELEASE);
      }

      token = strtok(NULL, ",");
    }


    if(token == "backward")
    {
      count++;

      if(count == 2)
      {
        int inputSpeed = atoi(token);
        motor1.setSpeed(inputSpeed);
        motor2.setSpeed(inputSpeed);
        motor3.setSpeed(inputSpeed);
        motor4.setSpeed(inputSpeed);
        Serial.print("Running backwards at ");
        Serial.print(inputSpeed);
        Serial.print(" / 255 speed for ") ;
      }

      else if(count == 3)
      {
        int duration = atoi(token);
        Serial.print(duration / 10);
        Serial.println(" seconds.");
        motor1.run(BACKWARD);
        motor2.run(BACKWARD);
        motor3.run(BACKWARD);
        motor4.run(BACKWARD);
        delay(duration);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
        motor3.run(RELEASE);
        motor4.run(RELEASE);
      }

      token = strtok(NULL, ",");
    }

    token = strtok(inData, ",");
    if(token == "rightturn")
    {
      digitalWrite(rightLEDs, HIGH);
      delay(750);
      digitalWrite(rightLEDs, LOW);
      delay(750);

      motor1.setSpeed(100);
      motor2.setSpeed(100);
      motor3.setSpeed(100);
      motor4.setSpeed(100);

      Serial.println("Turning right.");

      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay(2000);

      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);

      digitalWrite(rightLEDs, HIGH);

      token = strtok(NULL, ",");
    }

    token = strtok(inData, ",");
    if(token == "leftturn")
    {
      digitalWrite(leftLEDs, HIGH);
      delay(750);
      digitalWrite(leftLEDs, LOW);
      delay(750);

      motor1.setSpeed(100);
      motor2.setSpeed(100);
      motor3.setSpeed(100);
      motor4.setSpeed(100);

      Serial.println("Turning left.");

      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
      delay(2000);

      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);

      digitalWrite(leftLEDs, HIGH);

      token = strtok(NULL, ",");
    }
  }
}



Logged

Pages: 1 2 [3] 4 5   Go Up
Jump to: