NXC Questions

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

NXC Questions

Post by borntoown »

*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.
Last edited by borntoown on 26 Feb 2011, 07:48, edited 6 times in total.
B2O productions are the best for the best.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Questions

Post by mattallen37 »

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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC Questions

Post by muntoo »

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.
Think*. Plan. Design. Build. Test. Build. Test. Build. Test. (1000 "Build. Test."s later...) Test. Test. Test. Publish.

* 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

Re: NXC Questions

Post by borntoown »

First, thank you.
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.
But break; command doesn't show a condition (like pressing a touch sensor), or should i use it like this?

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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Questions

Post by mattallen37 »

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 ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC Questions

Post by muntoo »

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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

Re: NXC Questions

Post by borntoown »

Thanks.

Please answer the rest of the questions :)
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXC Questions

Post by timpattinson »

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

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
}
Good luck...
-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/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC Questions

Post by HaWe »

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.
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXC Questions

Post by timpattinson »

Good tutorial on NXC, see pg 47 for an explanation of the filesystem
NXC_tutorial.pdf
Programming
LEGO NXT Robots
using NXC
(beta 30 or higher)
(Version 2.2, June 7, 2007)
by Daniele Benedettelli
with revisions by John Hansen
(312.53 KiB) Downloaded 496 times
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/
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest