ROBOTC.net forums
http://robotc.net/forums/

Incrementing array with sensor values at different times?
http://robotc.net/forums/viewtopic.php?f=52&t=10642
Page 1 of 1

Author:  Cyks [ Mon Mar 09, 2015 2:17 am ]
Post subject:  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:

Author:  JohnWatson [ Mon Mar 09, 2015 10:29 am ]
Post subject:  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).

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/