NXC Questions
NXC Questions
*I really want to program in this language and sorry if I ask alot *
**I will keep asking questions in this topic so stay tuned **
OK:
1) What should I write to make a:
A) Conditional loop (If something happened, loop finishes). ANSWERED
B) To know the existence of a file (If YES, skip a part of the program. If NO, go to that part, which might be the creation of the file) ANSWERED.
2) I should close a file first in order to read it later (Like NXT-G)? ANSWERED
3) I asked this before but, if you please, explain the NXT file system (This is important to me) ANSWERED.
4) Why, when i start writing a program, i feel like a newbie?! I know most of the commands but there seems to be anthor problem?! What should i do? ANSWERED
ALL QUESTIONS ARE ANSWERED, THANKS.
No more questions for now.
Thanks.
**I will keep asking questions in this topic so stay tuned **
OK:
1) What should I write to make a:
A) Conditional loop (If something happened, loop finishes). ANSWERED
B) To know the existence of a file (If YES, skip a part of the program. If NO, go to that part, which might be the creation of the file) ANSWERED.
2) I should close a file first in order to read it later (Like NXT-G)? ANSWERED
3) I asked this before but, if you please, explain the NXT file system (This is important to me) ANSWERED.
4) Why, when i start writing a program, i feel like a newbie?! I know most of the commands but there seems to be anthor problem?! What should i do? ANSWERED
ALL QUESTIONS ARE ANSWERED, THANKS.
No more questions for now.
Thanks.
Last edited by borntoown on 26 Feb 2011, 07:48, edited 6 times in total.
B2O productions are the best for the best.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC Questions
1.A To make a loop that exits under certain circumstances, you can use a break; command. You could instead, have the loop in a function (void), and use the return; command to go back to the task that called the function.
2 IIRC, you should close it when you are done with it for the entire program. Basically, you should only close it once, just before the program ends (or once you know you are done using it).
4 You mean, a feeling of not knowing where to start? You have a lot of ideas, but you have a hard time typing them into code? For me, it helped to just practice a lot (make a lot of programs that were sort of functionally pointless). I first have to think of a flow that I want (sequence of events), then I make the tasks/functions do accomplish them, and then I "string" them together to form the proper sequence.
2 IIRC, you should close it when you are done with it for the entire program. Basically, you should only close it once, just before the program ends (or once you know you are done using it).
4 You mean, a feeling of not knowing where to start? You have a lot of ideas, but you have a hard time typing them into code? For me, it helped to just practice a lot (make a lot of programs that were sort of functionally pointless). I first have to think of a flow that I want (sequence of events), then I make the tasks/functions do accomplish them, and then I "string" them together to form the proper sequence.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC Questions
Think*. Plan. Design. Build. Test. Build. Test. Build. Test. (1000 "Build. Test."s later...) Test. Test. Test. Publish.mattallen37 wrote:4 You mean, a feeling of not knowing where to start? You have a lot of ideas, but you have a hard time typing them into code? For me, it helped to just practice a lot (make a lot of programs that were sort of functionally pointless). I first have to think of a flow that I want (sequence of events), then I make the tasks/functions do accomplish them, and then I "string" them together to form the proper sequence.
* Of course you think the entire time... I hope.
Plan - In your head.
Design - Write it down. (On paper?)
Building - It depends on your style. Top-Down/Bottom-Up/Modular/OO7**/etc...
** James Bond does OO7 programming.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXC Questions
First, thank you.
But break; command doesn't show a condition (like pressing a touch sensor), or should i use it like this?mattallen37 wrote:1.A To make a loop that exits under certain circumstances, you can use a break; command. You could instead, have the loop in a function (void), and use the return; command to go back to the task that called the function.
Code: Select all
int x = 10;
task main () {
SetSensorTouch(S1);
while (x = 10)
{
NumOut(8,32,"x = 10");
if (Sensor(S1) == 1) {
break; // look here
}
}
}
B2O productions are the best for the best.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC Questions
Indeed, you can use it with an if statement.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC Questions
break is used to break out of while and for loops, and switch statements. It has no effect on any other control structures (not that I know of). This might change in C++0x, when foreach may be introduced, but you don't need to worry about that; it's just while, for, and switch for now.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXC Questions
Thanks.
Please answer the rest of the questions
Please answer the rest of the questions
-
- Posts: 224
- Joined: 30 Oct 2010, 04:10
- Location: 127.0.0.1
- Contact:
Re: NXC Questions
There may be a better way to find if a file exists but if not try this...
It will return 1(same as true) if the file exists, 0 (same as false) if if doesn't and -1 if there was an error
Good luck...
-Tim
It will return 1(same as true) if the file exists, 0 (same as false) if if doesn't and -1 if there was an error
Code: Select all
int IfFileExists(string fname)
{
int fsize;
byte handle;
if(OpenFileRead(fname,fsize, handle) == LDR_SUCCESS) // All loader module functions return error codes
{
//File exists
CloseFile(handle);
return 1;
}
else if(OpenFileRead(fname,fsize, handle) == LDR_FILENOTFOUND)
{
// File does not exist
return 0;
}
else
{
//Other error
return -1;
}
}
Code: Select all
if(IfFileExists("myfile.txt"") == 1)
{
//do something if it exists
}
else
{
//do something if it doesn't
}
-Tim
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Re: NXC Questions
OpenFileRead will return if the file already exists - or not!
Code: Select all
IOresult = OpenFileRead(sFileName, nFileSize, fHandle);
if (IOresult == LDR_SUCCESS) {
for (i=0; i<RecLen; i++) {
ReadLn (fHandle, ibuf);
//...*SNIP*
Last edited by HaWe on 25 Feb 2011, 21:37, edited 1 time in total.
-
- Posts: 224
- Joined: 30 Oct 2010, 04:10
- Location: 127.0.0.1
- Contact:
Re: NXC Questions
Good tutorial on NXC, see pg 47 for an explanation of the filesystem
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Who is online
Users browsing this forum: No registered users and 0 guests