View unanswered posts | View active topics It is currently Fri Nov 16, 2018 3:16 am






Reply to topic  [ 2 posts ] 
Incrementing array with sensor values at different times? 
Author Message
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 16
Post Incrementing array with sensor values at different times?
Hello everyone,
So we want our robot to move a small distance, take a sensor reading, and add it to the next unused array slot many times throughout our program, then we want to compare it to the last value. How can you use the previous or next array slot without naming the number of it exactly? All I can find is how to make a for loop that increments it but stays in the loop until it is done. Does anyone have experience with this sort of thing? Any help is greatly appreciated! :biggrin:


Mon Mar 09, 2015 2:17 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
Post Re: Incrementing array with sensor values at different times
You may want to simply use a variable to keep track of what index of the array you are currently on, and add/subtract from that value as needed:

Code:
task main()
{
   int foo = 0;
   int bar[40];

   //Move forward (foo has a value of 0)
   bar[foo] = SensorValue(sensorName);
   foo = foo + 1;

   //Do something else (foo has a value of 1)
   bar[foo] = SensorValue(sensorName);
   foo = foo + 1;

   //Do a third thing (foo has a value of 2)
   bar[foo] = SensorValue(sensorName);
   foo = foo + 1;

   //Etc
}


You will have to be very careful that you don't lose track anywhere along the way, or go over the predefined number of array indices (in this case, 40).

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our updated help documentation and the ROBOTC Forums.


Mon Mar 09, 2015 10:29 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.