Page 1 of 1

NXT Counter

Posted: 16 Jul 2011, 00:01
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.

Re: NXT Counter

Posted: 16 Jul 2011, 00:16
by mattallen37
What programming language? In NXC, you can do exactly that: "variable++;".

Re: NXT Counter

Posted: 16 Jul 2011, 00:22
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.

Re: NXT Counter

Posted: 16 Jul 2011, 00:24
by mattallen37
NXT-G. You can create a variable, then in the loop do this: Read it, add it with 1, then write it.

Re: NXT Counter

Posted: 16 Jul 2011, 00:30
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.

Re: NXT Counter

Posted: 16 Jul 2011, 00:50
by mattallen37
You have to set the block to write, so that there is an input wire.

Re: NXT Counter

Posted: 16 Jul 2011, 02:32
by muntoo
As you seem to be familiar with C-like languages, why don't you use NXC (Not eXactly C) instead?

Re: NXT Counter

Posted: 16 Jul 2011, 07:49
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 

Re: NXT Counter

Posted: 16 Jul 2011, 21:14
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.

Re: NXT Counter

Posted: 16 Jul 2011, 23:06
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