the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« on: December 27, 2011, 08:59:02 pm » |
i am trying to make a serial driven robot. i have connected the tx pin of a Arduino mini (it controls the robot) to the rx pin of a Arduino UNO (the robot) but i cant get it to work. heres the code i was using for the robot int button1State = 0; const int button1 = 2; int command = 0;
void setup() { const int button1 = 2;
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(13,OUTPUT); pinMode(3,OUTPUT); pinMode(12,OUTPUT); pinMode(11,OUTPUT); pinMode(button1, INPUT);
}
void loop() { command = Serial.read();
button1State = digitalRead(button1); if (command == -1) { // turn LED on: digitalWrite(12,0); //start motor-B analogWrite(11,255);
digitalWrite(13,0); //stop motor-A analogWrite(3,0);
} if (command == 1) { digitalWrite(12,0); //start motor-B analogWrite(11,0);
digitalWrite(13,0); //stop motor-A analogWrite(3,255); }
if (command == 0) { digitalWrite(12,0); //start motor-B analogWrite(11,0);
digitalWrite(13,0); //stop motor-A analogWrite(3,0); } Serial.flush(); }
and for the controler void setup() { pinMode(10,INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // Pin 13 has an LED connected on most Arduino boards: }
void loop() { if (10 == HIGH) { Serial.write((byte)1); } else{ Serial.write((byte)0); } }
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
Offline
God Member
Karma: 15
Posts: 965
|
 |
« Reply #1 on: December 27, 2011, 09:04:44 pm » |
The loop() in the robot will never receive anything because you keep throwing it away with Serial.flush(). Remove that statement and try again.
Pete
|
|
|
|
|
Logged
|
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #2 on: December 27, 2011, 09:05:49 pm » |
removed that. int button1State = 0; const int button1 = 2; int command = 0;
void setup() { const int button1 = 2;
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(13,OUTPUT); pinMode(3,OUTPUT); pinMode(12,OUTPUT); pinMode(11,OUTPUT); pinMode(button1, INPUT);
}
void loop() { command = Serial.read();
button1State = digitalRead(button1); if (command == -1) { // turn LED on: digitalWrite(12,0); //start motor-B analogWrite(11,255);
digitalWrite(13,0); //stop motor-A analogWrite(3,0);
} if (command == 1) { digitalWrite(12,0); //start motor-B analogWrite(11,0);
digitalWrite(13,0); //stop motor-A analogWrite(3,255); }
if (command == 0) { digitalWrite(12,0); //start motor-B analogWrite(11,0);
digitalWrite(13,0); //stop motor-A analogWrite(3,0); } }
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
Offline
God Member
Karma: 15
Posts: 965
|
 |
« Reply #3 on: December 27, 2011, 09:08:01 pm » |
and?
|
|
|
|
|
Logged
|
|
|
|
|
Oregon
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #4 on: December 27, 2011, 09:10:52 pm » |
Hi Lego Ace King,
I'm new to Arduino, but I've programmed C for a long time. I have a tiny comment, a question, and a suggestion.
Tiny insignificant comment: You have an extra "const int button1 = 2;" statement.
Question: Did you also connect GND between the two Arduinos? You need some sort of return path for the signal, not just a single wire.
Suggestion. Instead of using 0 and 1 for the signal, you could use two visible characters, say 'a' and 'b'. Then, you could plug the robot directly into your computer via the USB and control it using the serial monitor, which not only receives messages, but can send them using the text entry field on top. That way, you could debug the robot end. Similarly, you could debug the controller Arduino by connecting it directly to the computer.
Hope this helps, Mac
|
|
|
|
|
Logged
|
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #5 on: December 27, 2011, 09:20:38 pm » |
Question1 the extra "const int button1 = 2;" statement is some old code in case i want to go back to the old way i controled it.
Question2 yes i do have GND connect between the two Arduinos.
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #6 on: December 27, 2011, 09:22:26 pm » |
how about this? void setup() { pinMode(10,INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // Pin 13 has an LED connected on most Arduino boards: }
void loop() { if (10 == HIGH) { Serial.write('a'); } else{ Serial.write('b'); } } int button1State = 0; const int button1 = 2; int command = 0;
void setup() { const int button1 = 2;
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(13,OUTPUT); pinMode(3,OUTPUT); pinMode(12,OUTPUT); pinMode(11,OUTPUT); pinMode(button1, INPUT);
}
void loop() { command = Serial.read();
button1State = digitalRead(button1); if (command == 'a') { // turn LED on: digitalWrite(12,0); //start motor-B analogWrite(11,255);
digitalWrite(13,0); //stop motor-A analogWrite(3,0);
} if (command == 'b') { digitalWrite(12,0); //start motor-B analogWrite(11,0);
digitalWrite(13,0); //stop motor-A analogWrite(3,255); }
if (command == 'c') { digitalWrite(12,0); //start motor-B analogWrite(11,0);
digitalWrite(13,0); //stop motor-A analogWrite(3,0); } }
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
Oregon
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #7 on: December 27, 2011, 09:42:01 pm » |
Ok, you got the GND.
And yes, that code change was what I was talking about.
|
|
|
|
|
Logged
|
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #8 on: December 27, 2011, 09:47:01 pm » |
i can now control the robot using the computer but i still cant get the controler to control the robot. i looked in the serial viewer and the controler seems to be sending sorta blank signals.
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
Offline
God Member
Karma: 15
Posts: 965
|
 |
« Reply #9 on: December 27, 2011, 09:51:25 pm » |
Use this for the controller code: #define MOTOR_PIN 10
void setup() { pinMode(MOTOR_PIN,INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps }
void loop() { if (digitalRead(MOTOR_PIN)) { Serial.write("a",1); } else { Serial.write("b",1); } }
But note that this will send a continuous stream of 'a' or 'b' depending upon whether MOTOR_PIN is high or low. Pete
|
|
|
|
|
Logged
|
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #10 on: December 27, 2011, 09:55:08 pm » |
when i try to upload the code i get this
sketch_dec26a.cpp: In function 'void loop()': sketch_dec26a:11: error: invalid conversion from 'const char*' to 'const uint8_t*' sketch_dec26a:11: error: initializing argument 1 of 'virtual size_t Print::write(const uint8_t*, size_t)'
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
Offline
God Member
Karma: 15
Posts: 965
|
 |
« Reply #11 on: December 27, 2011, 10:00:35 pm » |
yup, sorry. I'm still not up to speed with V1.0 ! #define MOTOR_PIN 10
void setup() { pinMode(MOTOR_PIN,INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps }
void loop() { if (digitalRead(MOTOR_PIN)) { Serial.write((const uint8_t *)"a",1); } else { Serial.write((const uint8_t *)"b",1); } }
Pete
|
|
|
|
|
Logged
|
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #12 on: December 27, 2011, 10:10:14 pm » |
it works sorta. it keeps sending the signal for like 1 sec and then a blank signal.
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
Oregon
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #13 on: December 27, 2011, 10:10:32 pm » |
i can now control the robot using the computer but i still cant get the controler to control the robot. i looked in the serial viewer and the controler seems to be sending sorta blank signals.
I don't have a button connected to pin 10, so I modified your code a little and fed this to my Arduino Uno: void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // Pin 13 has an LED connected on most Arduino boards: }
void loop() { Serial.write('a'); delay(1000); Serial.write('b'); delay(1000); } The serial monitor displayed this, just as expected: ababababa
|
|
|
|
|
Logged
|
|
|
|
|
the moon
Offline
Jr. Member
Karma: 0
Posts: 94
|
 |
« Reply #14 on: December 27, 2011, 10:25:09 pm » |
i added a delay to your code and it fixed the reseting. the reason i cant get a 'a' signal is that the arduino mini keeps reseting when i press the trigger button. i dont know how to fix that. heres the moded code. const int button1 = 10;
void setup() { pinMode(button1,INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps }
void loop() { if (digitalRead(button1) == HIGH ) { Serial.write((const uint8_t *)"a",1); } else { Serial.write((const uint8_t *)"b",1); } delay(100); }
|
|
|
|
|
Logged
|
Loading...
|
|
|
|
|