0
Offline
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #30 on: June 04, 2011, 10:54:14 am » |
I think the loops are right now. #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
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #31 on: June 04, 2011, 06:38:48 pm » |
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
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #32 on: June 04, 2011, 08:52:53 pm » |
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  ?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #33 on: June 04, 2011, 09:02:48 pm » |
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
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #34 on: June 05, 2011, 11:39:30 am » |
So it should start out as 1?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #35 on: June 05, 2011, 06:20:32 pm » |
Or test that it has reached 1 or 2.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #36 on: June 06, 2011, 08:38:50 am » |
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
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #37 on: June 06, 2011, 08:52:29 am » |
Currently, you have: byte count = 0; count++; if(count == 2) { // Do something } if(count == 3) { // Do something } You can change that to: byte count = 1; count++; if(count == 2) { // Do something } if(count == 3) { // Do something } or: 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
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #38 on: June 06, 2011, 02:34:43 pm » |
Thanks a lot. I am expecting my motor shield to come this week, but I can't test the program otherwise.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #39 on: June 08, 2011, 08:55:52 pm » |
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
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #40 on: June 09, 2011, 03:19:39 am » |
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? 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
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #41 on: June 09, 2011, 08:21:34 am » |
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
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #42 on: June 09, 2011, 09:23:13 am » |
I am just using Series 1 modules, there is no need to configure them. Say what? Of course there is. 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
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #43 on: June 09, 2011, 03:42:38 pm » |
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
Jr. Member
Karma: 0
Posts: 76
Obsessed with Apple
|
 |
« Reply #44 on: June 09, 2011, 06:54:15 pm » |
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. #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
|
|
|
|
|
|