Hello,
I have a problem with gobetwino, specially the command RFLIN.
I think the solution is not difficult, but I don't know how to program or change it.
First I will give you more information about the project.
I write data via matlab into output.txt.
Then I read the data of output.txt via gobetwino and dependent of the value, leds will blink.
Matlab runs continuous and write contiunuous data to output.txt.
Why can't gobetwino read data when output.txt is open?
Do someone know how to fix this?
Thanks a lot.
I got the error below from gobetwino.
Reading line 1 from C:\Users\Joris\Documents\MATLAB\output.txt generated an exception.
Can not access the file C: \ Users \ Documents \ MATLAB \ output.txt, because it is being used by another process.
int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;// Rode led op pin 4
int ledYellow = 6;
char serInString[25];
void setup()
{
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
Serial.begin(9600);
Serial.println("#S|OPENCAMERA|[]#");
delay(2000);
Serial.println("#S|OPNMATLAB|[]#");
}
void loop()
{
char buffer[5]; // long data type has 11 characters (including the minus sign) and a terminating null
int nrOfBlinks;
int lineNr;
int aantalAardbeien;
int teller;
int uitlezenXpixels;
/*
Your buffer must be large enough to hold the maximum number of characters in the string.
For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string);
32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0).
No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms,
because the overflow will corrupt some other part of memory that may be used by your program.
The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.
*/
lineNr=1;
clearSerialBuffer();
Serial.print("#S|LEESWITHE|["); //Leest de waarden binnen van matlab
Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
Serial.println("]#");
readSerialString (serInString, 10000);
aantalAardbeien = atoi(serInString);
Serial.print("#S|LOGTEST|[");
//Serial.print(itoa((nrOfBlinks), buffer, 10));
Serial.print(serInString);
Serial.println("]#");
//delay(200);
//antalAardbeien = 3;
for (teller=1; teller<=aantalAardbeien; teller++)
{
digitalWrite(ledYellow, HIGH);
delay(300);
digitalWrite(ledYellow, LOW);
delay(300);
//Serial.println(teller);
}
uitlezenXpixels = aantalAardbeien * 3 + 1;
for(lineNr=2;lineNr<=uitlezenXpixels;lineNr=lineNr+3)
{
clearSerialBuffer();
Serial.print("#S|LEESWITHE|["); //Leest de waarden binnen van matlab
Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
Serial.println("]#"); //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino
readSerialString (serInString, 10000);
nrOfBlinks = atoi(serInString); //Converteert een sting naar een integer
delay(200);
Serial.print("#S|LOGTEST|[");
//Serial.print(itoa((nrOfBlinks), buffer, 10));
Serial.print(serInString);
Serial.println("]#");
delay(200);
if (nrOfBlinks > 3 && nrOfBlinks <= 160)
{
digitalWrite(ledGreen, HIGH);
digitalWrite(ledRed, LOW);
delay(1000);
digitalWrite(ledGreen, LOW);
}
else if (nrOfBlinks > 160)
{
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, HIGH);
delay(1000);
digitalWrite(ledRed, LOW);
}
}
delay(100);
}
void clearSerialBuffer(){
char dummy;
while (Serial.available()) { //Also check for timeout here, also leave space for '\0' at the end of your C-String
dummy = Serial.read();
delay(10); //slight delay to allow the buffer to fill up
}
}
void readSerialString (char *strArray,long timeOut) {
// Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
long startTime=millis();
int i = 0;
while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < (serInLen-1) && (millis()-startTime) < timeOut) { //Also check for timeout here, also leave space for '\0' at the end of your C-String
strArray[i] = Serial.read();
i++;
delay(10); //slight delay to allow the buffer to fill up
}
strArray[i]='\0';
}