system
May 30, 2011, 11:27am
1
Hey everybody,
I'm trying to print out somekind of boot process during the execution of my setup function.
It doesn't seem to work when there is nothing connected to the USB.
Can it be avoided that my code "hangs" and that characters, printed to the serial port, are "ignore" when there is no PC connected?
What I try to do:
void setup() {
Serial.begin(9600);
Serial.println("Starting up...");
Serial.println("Version 0.1");
}
void loop() {
// do other stuff here.
}
Thanks a lot for the help!
Wim
system
May 30, 2011, 11:57am
2
It doesn't seem to work when there is nothing connected to the USB.
The code you posted works just fine when the Arduino is not connected to the PC.
Your real code must have more to it than that, or you must have some other evidence that the code hangs when not connected to the PC. Care to share that?
Thans for the swift reply Paul.
I figured out that it does work. But... Everytime I connect my laptop, it seems to reset the counters and rerun the setup function.
The code:
String inputBuffer;
unsigned long oldmillis = 0;
unsigned int counter = 0;
// Network configuration.
String ipAddress = "192.168.1.5";
String netMask = "255.255.255.0";
String gateway = "192.168.1.1";
// Tank size in liter.
int tankSize = 10000;
// Hysteresis in liter.
int hysteresis = 10;
void setup(){
// Init serial port
Serial.begin(9600);
pinMode(2, OUTPUT);
// Show running configuration.
Serial.println();
cmdShow();
Serial.println();
Serial.println("Ready.");
Serial.println();
Serial.print(">");
}
// MAIN LOOP
void loop(){
CheckInput();
if ((millis() - oldmillis) > 2000) {
if (counter % 2) {
digitalWrite(2, HIGH);
Serial.print("High ");
} else {
digitalWrite(2,LOW);
Serial.print("Low ");
}
oldmillis = millis();
counter++;
Serial.println(counter);
}
}
char CheckInput(){
if (Serial.available() > 0) {
// Read a character from the Serial.
int byteBuffer = Serial.read();
Serial.print(byteBuffer, BYTE);
if (byteBuffer > 31 and byteBuffer < 127) {
inputBuffer += char(byteBuffer);
} else if (byteBuffer == 13) {
Serial.flush();
if (inputBuffer.length() > 0) {
DoCmd();
} else {
Serial.print("> ");
}
} else if (byteBuffer == 127) {
// Backspace, remove last character from string.
inputBuffer = inputBuffer.substring(0, inputBuffer.length() - 1);
}
}
}
// Command processor
void DoCmd() {
Serial.println();
if (inputBuffer == "show") {
// Show running configuration.
cmdShow();
} else {
Serial.println("Invalid command.");
}
// Empty input buffer
inputBuffer = "";
Serial.print("> ");
}
// Commands go here.
void cmdShow() {
Serial.println("Running configuration:");
Serial.print("IP Address: ");
Serial.println(ipAddress);
Serial.print("Netmask: ");
Serial.println(netMask);
Serial.print("Gateway: ");
Serial.println(gateway);
Serial.println();
Serial.print("Tank size : ");
Serial.println(tankSize);
Serial.print("Fill hysteresis : ");
Serial.println(hysteresis);
Serial.print("Counter: ");
Serial.println(counter);
}
Everytime I connect my laptop, it seems to reset the counters and rerun the setup function.
Not only seems to, it does. That's a function of the way the serial port is opened. There are ways to modify the Arduino to prevent it.
Thanks Paul,
Hmmm, and how can I easily do this?
Cheers!
Wim
Well, the simplest way would be to tell us which board you've got.
Or you could check the schematics yourself.
Hey Awol,
Tnx, I already found it!
I'm using an Arduino Uno. It can be done via a small hardware change...
Cheers!
Wim