Only reads when Serial Monitor is running.

I’m rebuilding the Ganzbot (http://bit.ly/O4TGr) to be much simpler and am having trouble reading from the USB serial. The code simply changes the eyes and eyebrows of the robot based on the mood character that the computer sends to it. For some reason it works great when I have the serial monitor running in the Arduino IDE, but when I turn off the monitor it seems that the Arduino board doesn’t receive any of the commands.

The original version of the program, which was much more complicated, worked just fine so I don’t the program sending the commands is the problem. Here’s the code:

#include <Servo.h>

/* 
 * Ganzbot v2
 *
 * This ganzbot uses a LM3915 to drive the mouth, which leaves more pins open
 * for other things.
 *
 */
 
int eyes_red    = 5;
int eyes_blue   = 6;
int eyebrows[] = {7, 9}; // left, right

char mood = '|';

Servo eyebrow_left;
Servo eyebrow_right;

void setup(){
  Serial.begin(9600);
  eyebrow_right.attach(eyebrows[1]);
  eyebrow_left.attach(eyebrows[0]);
  
  eyebrow_right.write(90);
  eyebrow_left.write(90);
  
  digitalWrite(eyes_blue, HIGH);
  delay(1000);
  digitalWrite(eyes_blue, LOW);
}

void loop(){
  
  if(Serial.available() > 0) {
    char value = (char)Serial.read();
    
    switch(value){
      
       // Mood = Angry
      case '>':
       digitalWrite(eyes_red, HIGH);
       eyebrow_right.write(70);
       eyebrow_left.write(115);
       break; 
       
      // Mood = Sad
      case '(':
        digitalWrite(eyes_blue, HIGH);
        eyebrow_right.write(140);
        eyebrow_left.write(50);
        break;
      
      // Moode = Neutral
      case '|':
        digitalWrite(eyes_blue, HIGH);
        eyebrow_right.write(90);
        eyebrow_left.write(90);
        break;
      
      // Finished
      case '\r':
        delay(1000); // leave the mood for about 1 second
    
        digitalWrite(eyes_blue, LOW);
        digitalWrite(eyes_red, LOW);
      
        eyebrow_right.write(90);
        eyebrow_left.write(90);
        break;
    }
  }  
  
  Servo::refresh();
}

What am I doing wrong here?

i was just having the same kind of problems with i2c and the serial monitor.. if you are using multiple power supplies, or if you are using i2c, make sure you have a common ground between devices. That's what my problem was. Everytime i unplugged a cable, i lost i2c communication, and serial communication with the other devices.. when i put common ground to all my devices, i could unplug any device, and the i2c communication still work while i viewed it through another serial port.

In short, try common ground.

Everything is running off of a single power supply directly from the USB line. I'll double check the circuit tonight to ensure everything is on a common ground, but I pretty sure it already is.

I'm rebuilding the Ganzbot (http://bit.ly/O4TGr) to be much simpler and am having trouble reading from the USB serial. The code simply changes the eyes and eyebrows of the robot based on the mood character that the computer sends to it. For some reason it works great when I have the serial monitor running in the Arduino IDE, but when I turn off the monitor it seems that the Arduino board doesn't receive any of the commands.

What am I doing wrong here?

Program using "Serial communication" and it need "terminal" for data passing.. Board code didn't grab keyboard input from system directly.. but get from "Serial.read()" that Serial connection (COM port) must be negotiate first, before pass key to board.. then you close Serial Monitor it's also close the way talk to board too..

I wrote a Java program which connects to the serial port using RXTX and then passes the commands, so it should work without the Serial Monitor, right?

Thanks, Jeremy

I wrote a Java program which connects to the serial port using RXTX and then passes the commands, so it should work without the Serial Monitor, right?

yes, that's right..

you may try SerialServer (by Dan O'Sullivan) http://itp.nyu.edu/~dbo3/SerialServer/SerialServer.html it's useful application, check "debug" box on program and see all data passing in and out..