Go Down

Topic: Sending Full Words from Serial Monitor (Read 4817 times) previous topic - next topic

andrewf

I think the loops are right now.

Code: [Select]
#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, ",");
    }
  }
}


PaulS

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.

andrewf

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:?

PaulS

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.

andrewf


PaulS

Or test that it has reached 1 or 2.

andrewf

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?

PaulS

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


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


or:
Code: [Select]
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.

andrewf

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

andrewf

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.

PaulS

#40
Jun 09, 2011, 10:19 am Last Edit: Jun 09, 2011, 10:21 am by PaulS Reason: 1
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.

andrewf

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.

PaulS

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.

andrewf

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.

andrewf

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: [Select]
#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, ",");
    }
  }
}





Go Up