NXT Counter
-
- Posts: 3
- Joined: 15 Jul 2011, 23:58
NXT Counter
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.
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.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXT Counter
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 3
- Joined: 15 Jul 2011, 23:58
Re: NXT Counter
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.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXT Counter
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 3
- Joined: 15 Jul 2011, 23:58
Re: NXT Counter
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.
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.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXT Counter
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXT Counter
As you seem to be familiar with C-like languages, why don't you use NXC (Not eXactly C) instead?
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXT Counter
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
// on the other hand
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
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
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.
An optimizing compiler would easily turndoc-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!
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.Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 224
- Joined: 30 Oct 2010, 04:10
- Location: 127.0.0.1
- Contact:
Re: NXT Counter
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
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
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: Semrush [Bot] and 5 guests