serial driven robot

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);
  
  }
}

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

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);
  }   
  
}

and?

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

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.

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);
  }   
  
}

Ok, you got the GND.

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

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.

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

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

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

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

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.

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

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);
}

Try trapping the button event in a variable then do the comparison. The return from digitalRead() may be being lost between the function return and the comparison.(Parallax boards are notorious for this).

I gather the button is for arming, also a reason to store in a variable, as digitalRead()'s return value will loose scope upon completion of the loop. Using the keyword static will make the variable retain value across calls to loop(). Or declare the variable globally.

the pin isn't being initialized properly. Use this for your setup routine:

void setup() {
    pinMode(button1,INPUT);
    digitalWrite(button1,HIGH);
    Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
}

Pete

Also, which version of the Mini are you using? The pinout changed in a way that affects the reset. See http://www.arduino.cc/en/Main/ArduinoBoardMini

Pete

True, Pete, unless a high output state is written, the internal pullup resistor is tied low. I always use external pullups as I also use debounce caps across switches.