NXT Counter

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
expomarker
Posts: 3
Joined: 15 Jul 2011, 23:58

NXT Counter

Post by expomarker »

Hello,

I am new to Mindstorms. I was hoping I could get some help on how to program a counter with variables; similar to "variable++" or "variable+=1". I do not want to use the counter option in the loop. Is there a simple way to increment a variable?

Thank you in advance for your help.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXT Counter

Post by mattallen37 »

What programming language? In NXC, you can do exactly that: "variable++;".
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
expomarker
Posts: 3
Joined: 15 Jul 2011, 23:58

Re: NXT Counter

Post by expomarker »

I am trying to use the language that is provided with the standard NXT kit. The one that is based off labview. I am not sure exactly what it's called.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXT Counter

Post by mattallen37 »

NXT-G. You can create a variable, then in the loop do this: Read it, add it with 1, then write it.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
expomarker
Posts: 3
Joined: 15 Jul 2011, 23:58

Re: NXT Counter

Post by expomarker »

Thanks for that help.

What I'm trying to do is this:

while (true)
if (condition)
counter++
[rest of implimentation]

The problem I am having is putting the added value back into the same variable that is being read. I might not be explaining it well, but I hope you understand what I am trying to get at. Thanks again for the help.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXT Counter

Post by mattallen37 »

You have to set the block to write, so that there is an input wire.
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: NXT Counter

Post by muntoo »

As you seem to be familiar with C-like languages, why don't you use NXC (Not eXactly C) instead?
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXT Counter

Post by HaWe »

if you want to use NXC (or C) some day...
as I have learnt a short time ago, for C (and for NXC because of compatibility reasons) it's like the following:

increase a counter:
counter=counter+1; // is probably the slowest way because it uses temporary variables. Don't use it if you want to write optimized code!

counter+=1; // is the fastest way to increase on "real C compilers for native machine code", because it generally only uses bit operations in the registers, not in RAM, and no temporary variables are needed.

++counter; //++ prefix does the same as +=1

counter++ // postfix ++ can cause problems (or even advantages) because of side effects (operator precendeces). It sometimes is slower because temporary variables may be needed.

The same it's with decrement
counter-=1; --counter; counter--;


No need to understand postfix increment when you are a beginner. In doubt always use prefix increment!
(provided that there are no NXC compiler issues, but for that you should be prepared^^!)

I'm using postfix ++(--) only in special cases like

Code: Select all

C[i++]=A[x]; // side effect!
// is the same as
C[i]=A[x];  // first assign (storing i temporarily)
++i;  // then afterwards increment
// on the other hand

Code: Select all

C[++i]=A[x]; // no side effect!
// is the same as
++i; // first increment
C[i]=A[x]; // then assign 
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXT Counter

Post by muntoo »

NXT-G has a useful little "Help section". You may want to read about Data Wires (which work like parameters), Variable blocks, Conditional blocks (whatever they're called), and Compare blocks. These blocks can be found in the "Data Block" or "Advanced Block" sections.
doc-helmut wrote:increase a counter:
counter=counter+1; // is probably the slowest way because it uses temporary variables. Don't use it if you want to write optimized code!
An optimizing compiler would easily turn counter=counter+1 into the fastest code possible; it's equivalent to ++counter in a "real C compiler". NXC doesn't have the best optimizer in the world, currently, so it might not do that.
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXT Counter

Post by timpattinson »

Something like this?
You will need to edit the "Switch" (read: if statement)
to reflect your custom condition and add your code ;)
The math block does not write to the variable straight away, but stores it in a "data wire" (read: temporary variable)
which is used by the second variable block, that writes to the variable
program.jpg
program.zip
(66.3 KiB) Downloaded 293 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 0 guests