Hi,
I have a project that reads setup values from the serial port (from a laptop) which are then used to control the operation of a camera. Everything works fine if i manually set the values in advance but is not going so well when attempting to get the values from the serial port. The values come as a delimited serial stream. A start character is sent to a python program that sends teh serial stream then waits for a stop character. A version of this program works in PIC microcontroller perfectly but i am trying to migrate to AVR and the arduino platform.
A code excerpt showing serial input is below.
At teh moment the issue seems to be that even when the serial port is not connected to the laptop there still seems to be characters in teh input buffer when there should be none - it should be cycling around doing nothing untill it gets input. I would have also posted output from the serial monitor which shows there there are characters appearing in the input buffer that shouldnt be there but it wouldnt allow me to cut and paste.
Any help is much appreciated.
#include <SoftwareSerial.h>
#include <EEPROM.h>
char inByte;
String inString;
int ledOnPeriod, ledDelay, imageInt, numOfImages, startDelay, pumpSpeed;
SoftwareSerial serIO = SoftwareSerial(10, 11, true);
int imageCnt = 0;
int newValues = 0;
void getSetupValues() {
Serial.println("Getting setup values");
int bufferStatus = 0;
int cnt = 0;
inString = "";
serIO.write('
); //send asci start code to pyprog
while (true) {
if (serIO.available() > 0) { //something is in teh serial buffer
bufferStatus = 1;
inByte = serIO.read();
if (inByte == '\n') { //if program recieves a carriage return then break and process teh string
Serial.print("String: ");
Serial.println(inString);
break;
}
Serial.print(inByte); //testing
inString += inByte;
}
delay(40);
cnt++;
if (cnt > 50) {break;} //wait for a total of two seconds for input if none occurs exit
}
Serial.print("bufferStatus> ");
Serial.println(bufferStatus);
if (bufferStatus == 0) { //nothing in the serial buffer
newValues = 0;
}
else {newValues = 1;}
serIO.write('#');
}
void setup(){
Serial.begin(9600);
serIO.begin(9600);
//pinMode(3, OUTPUT);
//pinMode(4, OUTPUT);
//pinMode(5, OUTPUT);
pinMode(10, INPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH); // set the LED on
Serial.println("Camera LED System");
Serial.println("Program: CameraLEDDrive");
Serial.println("Pyprog: CameraLedSystem.py");
delay(2000); // wait for 2 second
digitalWrite(9, LOW); // set the LED off
}
void loop(){
getSetupValues();
if (newValues == 1) {
processString();
delayStart();
startPump();
startCamera();
setupValues();
getImages();
}
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(200);
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(700);
}