Go Down

Topic: serial driven robot (Read 1 time) previous topic - next topic

legoaceking

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: [Select]
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: [Select]
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);
 
  }
}
Loading...

el_supremo

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

legoaceking

removed that.

Code: [Select]
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);
  }   
 
}
Loading...

el_supremo


Mac Stevens

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

legoaceking

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

legoaceking

how about this?

Code: [Select]
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: [Select]
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);
  }   
 
}
Loading...

Mac Stevens

Ok, you got the GND.

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

legoaceking

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

el_supremo

Use this for the controller code:
Code: [Select]

#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

legoaceking

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)'
Loading...

el_supremo

yup, sorry. I'm still not up to speed with V1.0 !
Code: [Select]

#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

legoaceking

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

Mac Stevens


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


legoaceking

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: [Select]
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);
}
Loading...

Go Up