Pages: [1] 2   Go Down
Author Topic: serial driven robot  (Read 1449 times)
0 Members and 1 Guest are viewing this topic.
the moon
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Code:
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 Offline
Edison Member
*
Karma: 43
Posts: 1557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Where are the Nick Gammons of yesteryear?

the moon
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

removed that.

Code:
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 Offline
Edison Member
*
Karma: 43
Posts: 1557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and?
Logged

Where are the Nick Gammons of yesteryear?

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

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 Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how about this?

Code:
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');
 
  }
}






Code:
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 Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, you got the GND.

And yes, that code change was what I was talking about. 
Logged

the moon
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 43
Posts: 1557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use this for the controller code:
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

Where are the Nick Gammons of yesteryear?

the moon
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 43
Posts: 1557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yup, sorry. I'm still not up to speed with V1.0 !
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((const uint8_t *)"a",1);
    } else {
        Serial.write((const uint8_t *)"b",1);
    }
}

Pete
Logged

Where are the Nick Gammons of yesteryear?

the moon
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it works sorta.
it keeps sending the signal for like 1 sec and then a blank signal.
Logged

Loading...

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

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:
Code:
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:
Quote
ababababa
Logged

the moon
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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...

Pages: [1] 2   Go Up
Jump to: