kasperhangard:
what is namecount? the amount of lines, where names can be written?
Yes. If you guessed that from the name, it suggests the name was reasonably well chosen.
kasperhangard:
what is namecount? the amount of lines, where names can be written?
Yes. If you guessed that from the name, it suggests the name was reasonably well chosen.
Ah. finally back at the project.
I have done this.
char inData[17][19];
const byte MAX_INPUT_LENGTH = 16;
char namecheck[MAX_INPUT_LENGTH+1];
byte namecheckindex = 0;
char inChar;
byte index = 0;
byte linenumber = 0;
byte queuenumber = 0;
boolean hit = false;
int done = 0;
void setup() {
Serial.begin(9600);
}
void writer()
{
if ((linenumber<17) && (index < 19))
{
inChar = namecheck[index]; //read from Serial
if (inChar==13) // if character is the enter key
{
inData[linenumber][index] = 0; // trailing null
Serial.println(inData[linenumber]); // show received line
linenumber++;
index = 0;
}
else
{
inData[linenumber][index] = inChar; //store it in inData
index++; //make ready for next time - Next array "downwards"
}
}
}
void namecheckerload()
{
Serial.println("Running namecheckerload");
while(Serial.available() > 0) //only read if there is any data
{
inChar = Serial.read(); //read from Serial
if (namecheckindex < 16)
{
if (inChar==13) // if character is the enter key
{
namecheck[namecheckindex] = 0; // trailing null
Serial.println(namecheck); // show received line
namecheckindex=0;
done = 1;
break;
}
else
{
namecheck[namecheckindex] = inChar; //store it in inData
namecheckindex++; //make ready for next time - Next array "downwards"
}
}
}
}
void namechecker()
{
for(byte i = 0; i < 30; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
Serial.print("is already here at position ");
Serial.println(i);
hit = 1 ;
break; // bail out of the for loop; presumably there's no point searching the rest of the array
}
}
}
void loop() {
namecheckerload();
if (done == 1);
{
namechecker();
}
if (hit == 0)
{
writer();
}
}
though, what happens is that it runs namecheckerload and namechecker at the same time.
My guess would be that since i haven't entered anything, it just says that the "empty" space in inchar, is found in the first line of inData, as the first line is also "empty". is that right?
If i then try to enter anything, it gives me this
which i just don't understand at all.
Help please.
This is a problem:
if (done == 1);
you have' ; ' after the if
, so, is the same that you don't have any if
at all.
If you don't want to have this problem you MUST ALWAYS write the if
like:
if (done == 1) {
if the line of the if
ends with a ' { ' you never will do this error again.
I believe that you have many more errors, but I find this that is obvious.
luisilva:
This is a problem:if (done == 1);
you have' ; ' after the
if
, so, is the same that you don't have anyif
at all.If you don't want to have this problem you MUST ALWAYS write the
if
like:if (done == 1) {
if the line of the
if
ends with a ' { ' you never will do this error again.I believe that you have many more errors, but I find this that is obvious.
ah wow.. that was one rookie mistake! thanks man!
I still can't seem to make this work.. what have i messed up?
I have done this.
char inData[17][19];
const byte MAX_INPUT_LENGTH = 16;
char namecheck[MAX_INPUT_LENGTH+1];
byte namecheckindex = 0;
char inChar;
byte index = 0;
byte linenumber = 0;
byte queuenumber = 0;
boolean hit = false;
int done = 0;
void setup() {
Serial.begin(9600);
}
void writer()
{
if ((linenumber<17) && (index < 19))
{
inChar = namecheck[index]; //read from Serial
if (inChar==13) // if character is the enter key
{
inData[linenumber][index] = 0; // trailing null
Serial.println(inData[linenumber]); // show received line
linenumber++;
index = 0;
}
else
{
inData[linenumber][index] = inChar; //store it in inData
index++; //make ready for next time - Next array "downwards"
}
}
}
void namecheckerload()
{
Serial.println("Running namecheckerload");
while(Serial.available() > 0) //only read if there is any data
{
inChar = Serial.read(); //read from Serial
if (namecheckindex < 16)
{
if (inChar==13) // if character is the enter key
{
namecheck[namecheckindex] = 0; // trailing null
Serial.println(namecheck); // show received line
namecheckindex=0;
done = 1;
break;
}
else
{
namecheck[namecheckindex] = inChar; //store it in inData
namecheckindex++; //make ready for next time - Next array "downwards"
}
}
}
}
void namechecker()
{
for(byte i = 0; i < 30; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
Serial.print("is already here at position ");
Serial.println(i);
hit = 1 ;
break; // bail out of the for loop; presumably there's no point searching the rest of the array
}
}
}
void loop() {
namecheckerload();
if (done == 1)
{
namechecker();
}
if (hit == 0)
{
writer();
}
}
If i then try to enter anything, it gives me this
which i just don't understand at all.
Help please.
[/quote]
First thing:
your array have only space for 17 elements:
char inData[17][19];
but you're looking for a "name" in 30 elements array:
for(byte i = 0; i < 30; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
(...)
So, change the 30 to 17.
So, change the 30 to 17.
Even better would be to use a const variable (is that an oxymoron?) to hold the number of names, and use that variable in the declaration as well as the for statement. That way, only one change is needed if you need to change the number of names.
PaulS:
So, change the 30 to 17.
Even better would be to use a const variable (is that an oxymoron?) to hold the number of names, and use that variable in the declaration as well as the for statement. That way, only one change is needed if you need to change the number of names.
Ah right. i did think about this, i just didn't realise the advantage! thanks alot
Just to ensure that i understood you right,
I would make like this
const int entrynumber = 17;
is that correct ?
is that correct ?
So far, though that name means nothing and the type is not the best choice.
Something along these lines:
const byte entryCount = 17;
const byte entrySize = 19;
char inData[entryCount][entrySize];
for(byte i = 0; i < entryCount; i++)
{
PaulS:
So, change the 30 to 17.
Even better would be to use a const variable (is that an oxymoron?) to hold the number of names, and use that variable in the declaration as well as the for statement. That way, only one change is needed if you need to change the number of names.
Yes, you're right. But what I thought was "1 step at each time". There are many more error/bugs in the code.
Aight, so i am working on making my code easier to understand atm. i have one question.
void namechecker()
{
delay(1000);
Serial.println("Running namechecker");
for(byte i = 0; i < entryCount; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
Serial.print("is already here at position ");
Serial.println(i);
writerReady = 1 ;
break; // bail out of the for loop; presumably there's no point searching the rest of the array
}
}
}
how would i make it stop "looking" for the name? atm. it just keeps running in this loop i think.
How do i make it get out of the loop if the name isn't found ?
Am i right that doing an else like this
void namechecker()
{
delay(1000);
Serial.println("Running namechecker");
for(byte i = 0; i < entryCount; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
Serial.print("is already here at position ");
Serial.println(i);
writerReady = 1 ;
break; // bail out of the for loop; presumably there's no point searching the rest of the array
}
else
{
writerReady = 0;
break;
}
}
}
would just make it break from the loop after the first "look" ?
Also, how can i make it easier for you guys to help me? i mean, if i write comments to all lines of my code, would that help you ? anything else i can do ?
Thanks alot guys!
Starting for your last question: In my opinion, doing a good identation can help. After you post, you must select from the menu Tools the option "Auto-format".
How do i make it get out of the loop if the name isn't found ?
You must do run the namechecker()
function only if you have "new data" and not always.
In your case, I believe that only:
void loop() {
namecheckerload();
if (done == 1)
{
namechecker();
done = 0;
}
if (hit == 0)
{
writer();
hit = 1;
}
}
can help.
luisilva:
Starting for your last question: In my opinion, doing a good identation can help. After you post, you must select from the menu Tools the option "Auto-format".How do i make it get out of the loop if the name isn't found ?
You must do run the
namechecker()
function only if you have "new data" and not always.In your case, I believe that only:
void loop() {
namecheckerload();
if (done == 1)
{
namechecker();
done = 0;
}
if (hit == 0)
{
writer();
hit = 1;
}
}
can help.
Hey man. i did this
void loop() {
if (namecheckerloadReady == 1)
{
namecheckerload();
}
if (namecheckerReady == 1)
{
namechecker();
}
if (writerReady == 1)
{
writer();
}
}
instead. i wanted them to run seperately, and not at the same time.
Here the variables namecheckerloadReady, namecheckerReady, writerReady
goes to zero?
luisilva:
Here the variablesnamecheckerloadReady, namecheckerReady, writerReady
goes to zero?
uuhm.. care to elaborate ? i don't think i understand.
You enter the if
, if you have a variable with a value 1. When you exit that if
the variable must be 0, if don't the mext time you pass that if you will "enter on it" even if your condition was not verified.
EDIT: Look to Reply #31. What I did was add a line that makes done=0
.
I'm starting to have a hard time keeping an overlook at this.
it worked just moments ago, and now i just messes up horribly.
can anyone please take a look, and see if there is any obvious mistakes ?
byte namecheckerloadReady = 1;
byte namecheckerReady = 0;
byte writerReady = 0;
byte linecheckerReady = 0;
byte linenumbersetReady = 0;
byte linenumbersetdirty = 0;
int freeline = 0;
int linetoclear = 0;
const byte entryCount = 17;
const byte entrySize = 19;
char inData[entryCount][entrySize];
const byte MAX_INPUT_LENGTH = 16;
char namecheck[MAX_INPUT_LENGTH+1];
char lineholder[MAX_INPUT_LENGTH+1];
byte namecheckindex = 0;
char inChar;
byte index = 0;
byte linenumber = 0;
byte queuenumber = 0;
boolean hit = false;
void setup() {
Serial.begin(9600);
}
void writer()
{
// delay(1000);
Serial.println("running writer");
if ((linenumber<entryCount) && (index < entrySize))
{
inChar = namecheck[index]; //read from Serial
if (inChar ==0 ) // if character is the enter key
{
inData[linenumber][index] = 0; // trailing null
Serial.print("have written ");
Serial.print(inData[linenumber]); // show received line
Serial.print(" to line: ");
Serial.println(linenumber);
index = 0;
writerReady = 0;
namecheckerloadReady = 1;
Serial.println(inData[linenumber][index]);
Serial.println(linenumber);
Serial.println(index);
linenumber++;
}
else
{
inData[linenumber][index] = inChar; //store it in inData
index++; //make ready for next time - Next array "downwards"
}
}
}
void linenumberset()
{
Serial.println("Running linenumberset");
for(byte i = 0; i < entryCount; i++)
{
if(strcmp(inData[i], 0) == 0)
{
Serial.print(linenumber);
Serial.println("= freeline");
Serial.print("free line at ");
Serial.println(i);
if(linenumbersetdirty == 0)
{
linenumber = i;
}
linenumbersetdirty = 1;
}
if(i == (entryCount-1))
{
Serial.println("linenumberset done");
linenumbersetReady = 0;
break;
}
}
}
void linechecker()
{
Serial.println("running linechecker");
for(byte i = 0; i < entryCount; i++)
{
Serial.println(i);
if(strcmp(inData[i], 0) == 0)
{
Serial.print("the line is free at ");
Serial.println(i);
}
i++;
if((i<entryCount) && (index < entrySize) && inChar == 0)
{
Serial.print("checked line: ");
Serial.println(i);
inData[i][index] = 0; // trailing null
index = 0;
inChar = lineholder[index];
}
else
{
inChar = inData[i][index];
lineholder[index] = inChar;
index++;
}
--i;
index = 0;
inChar = lineholder[index];
if((i<entryCount) && (index < entrySize) && inChar == 0)
{
inData[i][index] = 0; // trailing null
index = 0;
inChar = lineholder[index];
linecheckerReady = 0;
Serial.println("quitting linechecker");
break;
}
else
{
inChar = lineholder[index];
inData[i][index] = inChar;
index++;
}
}
}
void namecheckerload()
{
// Serial.println("Running namecheckerload");
delay(1000);
while(Serial.available() > 0) //only read if there is any data
{
inChar = Serial.read(); //read from Serial
if (namecheckindex < (entryCount-1))
{
if (inChar==13) // if character is the enter key
{
namecheck[namecheckindex] = 0; // trailing null
Serial.println(namecheck); // show received line
namecheckindex=0;
namecheckerReady = 1;
namecheckerloadReady = 0;
index = 0;
break;
}
else
{
namecheck[namecheckindex] = inChar; //store it in inData
namecheckindex++; //make ready for next time - Next array "downwards"
}
}
}
}
void namechecker()
{
Serial.println("Running namechecker");
delay(1000);
for(byte i = 0; i < entryCount; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
Serial.print("is already here at position ");
Serial.println(i);
do
{
Serial.println("clearer line");
inData[i][index] = 0;
index++;
linenumbersetReady = 1;
}
while(index != entryCount );
index = 0;
namecheckerReady = 0;
writerReady = 0;
namecheckerloadReady = 1;
linecheckerReady = 1;
}
if(i == (entryCount-1))
{
Serial.println("Breaking out");
namecheckerReady = 0;
writerReady = 1;
index = 0;
break;
}
}
}
void loop() {
if (namecheckerloadReady == 1)
{
namecheckerload();
}
if (namecheckerReady == 1)
{
namechecker();
}
if (linecheckerReady == 1)
{
index = 0;
linechecker();
}
if (linenumbersetReady == 1)
{
freeline = 30;
index = 0;
linenumberset();
}
linenumbersetdirty = 0;
if (writerReady == 1)
{
writer();
}
}
What is exactly the problem? forget it.
I put the 2 codes side by side, and I found it. Look at this:
void writer()
{
// delay(1000);
Serial.println("running writer");
if ((linenumber<entryCount) && (index < entrySize))
{
inChar = namecheck[index]; //read from Serial
if (inChar ==0 ) // if character is the enter key
{
inData[linenumber][index] = 0; // trailing null
Serial.print("have written ");
Serial.print(inData[linenumber]); // show received line
Serial.print(" to line: ");
Serial.println(linenumber);
index = 0;
writerReady = 0;
namecheckerloadReady = 1;
Serial.println(inData[linenumber][index]);
Serial.println(linenumber);
Serial.println(index);
linenumber++;
}
else
{
inData[linenumber][index] = inChar; //store it in inData
index++; //make ready for next time - Next array "downwards"
}
}
}
void writer()
{
if ((linenumber < 17) && (index < 19))
{
inChar = namecheck[index]; //read from Serial
if (inChar == 13) // if character is the enter key
{
inData[linenumber][index] = 0; // trailing null
Serial.println(inData[linenumber]); // show received line
linenumber++;
index = 0;
}
else
{
inData[linenumber][index] = inChar; //store it in inData
index++; //make ready for next time - Next array "downwards"
}
}
}
void namecheckerload()
{
Serial.println("Running namecheckerload");
while (Serial.available() > 0) //only read if there is any data
{
inChar = Serial.read(); //read from Serial
if (namecheckindex < 16)
{
if (inChar == 13) // if character is the enter key
{
namecheck[namecheckindex] = 0; // trailing null
Serial.println(namecheck); // show received line
namecheckindex = 0;
done = 1;
break;
}
else
{
namecheck[namecheckindex] = inChar; //store it in inData
namecheckindex++; //make ready for next time - Next array "downwards"
}
}
}
}
Why you change the ENTER from 13 to 0?
This is the incorrect:
if (inChar ==0 ) // if character is the enter key
This is the correct:
if (inChar == 13) // if character is the enter key
Sorry. totally forgot to write about that.
aight. lets go a step back..
i got this
byte namecheckerloadReady = 1;
byte namecheckerReady = 0;
byte writerReady = 0;
byte linecheckerReady = 0;
byte linenumbersetReady = 0;
byte linenumbersetdirty = 0;
int freeline = 0;
int linetoclear = 0;
const byte entryCount = 17;
const byte entrySize = 19;
char inData[entryCount][entrySize];
const byte MAX_INPUT_LENGTH = 16;
char namecheck[MAX_INPUT_LENGTH+1];
char lineholder[MAX_INPUT_LENGTH+1];
byte namecheckindex = 0;
char inChar;
byte index = 0;
byte linenumber = 0;
byte queuenumber = 0;
boolean hit = false;
void setup() {
Serial.begin(9600);
}
void writer()
{
// delay(1000);
Serial.println("running writer");
if ((linenumber<entryCount) && (index < entrySize))
{
inChar = namecheck[index]; //read from Serial
if (inChar ==0 ) // if character is the enter key
{
inData[linenumber][index] = 0; // trailing null
Serial.print("have written ");
Serial.print(inData[linenumber]); // show received line
Serial.print(" to line: ");
Serial.println(linenumber);
index = 0;
writerReady = 0;
namecheckerloadReady = 1;
Serial.println(inData[linenumber][index]);
Serial.println(linenumber);
Serial.println(index);
linenumber++;
}
else
{
inData[linenumber][index] = inChar; //store it in inData
index++; //make ready for next time - Next array "downwards"
}
}
}
void linenumberset()
{
Serial.println("Running linenumberset");
for(byte i = 0; i < entryCount; i++)
{
if(strcmp(inData[i], 0) == 0)
{
Serial.print(linenumber);
Serial.println("= freeline");
Serial.print("free line at ");
Serial.println(i);
if(linenumbersetdirty == 0)
{
linenumber = i;
}
linenumbersetdirty = 1;
}
if(i == (entryCount-1))
{
Serial.println("linenumberset done");
linenumbersetReady = 0;
break;
}
}
}
void namecheckerload()
{
writerReady = 0;
// Serial.println("Running namecheckerload");
while(Serial.available() > 0) //only read if there is any data
{
inChar = Serial.read(); //read from Serial
if (namecheckindex < (entryCount-1))
{
if (inChar==13) // if character is the enter key
{
namecheck[namecheckindex] = 0; // trailing null
Serial.println(namecheck); // show received line
namecheckindex=0;
namecheckerReady = 1;
namecheckerloadReady = 0;
index = 0;
break;
}
else
{
namecheck[namecheckindex] = inChar; //store it in inData
namecheckindex++; //make ready for next time - Next array "downwards"
}
}
}
}
void namechecker()
{
Serial.println("Running namechecker");
delay(1000);
for(byte i = 0; i < entryCount; i++)
{
if(strcmp(inData[i], namecheck) == 0)
{
Serial.print("is already here at position ");
Serial.println(i);
do
{
Serial.println("clearer line");
inData[i][index] = 0;
index++;
linenumbersetReady = 1;
}
while(index != entryCount );
index = 0;
namecheckerReady = 0;
writerReady = 0;
namecheckerloadReady = 1;
linecheckerReady = 1;
}
if(i == (entryCount-1))
{
Serial.println("Breaking out");
namecheckerReady = 0;
writerReady = 1;
index = 0;
break;
}
}
}
void loop() {
if (namecheckerloadReady == 1)
{
namecheckerload();
}
if (namecheckerReady == 1)
{
namechecker();
}
if (linenumbersetReady == 1)
{
freeline = 30;
index = 0;
linenumberset();
}
linenumbersetdirty = 0;
if (writerReady == 1)
{
writer();
}
}
this works fine with writing to the first open space.
if the input is already on the list, it deletes the entry.
but if i input, say
a
b
c
d
e
f
and i the input c again,
it removes c.
if i then insert c again, it says that c is already there.
if i say m instead, for example it just writes m onto c's line. does that make sense?
it should just write it onto the empty space, regardless if it is the same character which gets send.
also, i need to be able to "fix" the list..
say i get this list
a
b
c
d
i then input b, which removes it. it now looks like
a
c
d
i then need to "lift" up the other ones, so that it looks like this
a
c
d
I tried to do that, but i failed horribly. and by that, wasted like 8 hours of my day..
luisilva:
What is exactly the problem?forget it.Why you change the ENTER from 13 to 0?
This is the incorrect:
if (inChar ==0 ) // if character is the enter key
This is the correct:
if (inChar == 13) // if character is the enter key
honestly i don't even remember how i made it work ATM. i am too tired. though i am pretty sure i null terminated it somewhere. regardless, it seems to work well, i mean, it does seem to write it.
if i change it to inChar == 13, the writer gets stuck in an infinite loop.
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer
running writer