(Almost) everything you wanted to know about arrays in ROBOTC but were afraid to ask. RoboDesigners have written up a quick explanation of how arrays work and help you visualise them. It has some ROBOTC specific code in it, but would probably work fine for most programming languages.
Thanks for taking the time to write it up!
Thanks for the mention!
//Andrew
(RoboDesigners member)
hi, i need help, have tow robots NXT 2.0 and 2 Nxbee, how send values of arrays to robot2, because this robot walk with values of sensor of robot1, sorry my english is bad
It would help if you told me what programming language you are using.
programming languaje in robotC for NXT
There might be something you can use here: http://dexterindustries.com/blog/2012/04/02/new-nxtbee-library/
Mark wrote a library that allows you to send a couple of different types across a NXTBee connection.
How can i save light sensor values in an array of 100 elements?
int sensorvals[100];
for (int i = 0; i < 100; i++) { sensorvals[i] = SensorValue[S1]; wait1Msec(100); }
This assumes you have the sensor configured in another part of the program.
What are the quick ways to fill an array with data?
If I have a 2D array that I made with int a[3][6], could I enter it a line at a time? Something like…
a[0] = [20 58 -10 256 11 2];
a[1] = [100 -12 48 24 127 -512 ];
a[2] =[58 76 27 -24 56 23];
thanks
In ROBOTC you cannot initialise sub arrays that way, you’ll have to initialise each element. I could propose it to the devs, though, it would be a nice addition.
I found this method on megacplusplus
int sqrs[10][2] = {
{1, 1},
{2, 4},
{3, 9},
{4, 16},
{5, 25},
{6, 36},
{7, 49},
{8, 64},
{9, 81},
{10, 100}
}; //
I haven’t checked to see if it actually worked (if the array is actually there), but it did compile without errors.
Flint, that is usually how I initialise them, but you can’t initialise them the way you previously mentioned.
That format will work fine for us. I know that really we should read it from a file, but that’s another can of worms.
Thanks.
May I ask you another question? Here?
We’re trying to do line following with the NXT. Last year we were able to do proportional following using the regular color sensor. This year (with RobotC) we’re stuck. We only see how to get a color, not a number.
We used NXC commands
SetSensorColorRed(S1); //to set up the sensor and
redraw=SENSOR_1 ; // to get a numerical value
Can we do it somehow in RobotC?
Never mind. It’s all right there in the sample program for 3.59. Thanks for posting the news.
Looks like we chose the right week to have trouble!
Hello, I’d like to use array(s) for storing distance measurements of rotating ultrasonic sensor (90°left and right from center), compare them and based on largest value point my Lego NXT in the direction where that measurement was taken (degrees of motor rotation in increments of 30°?)
I’m newbee in programming, using RobotC and would really appreciate some help in solving this problem
I think you asked this question on the ROBOTC forums, did you not?
i want to make an 2D array and i want to found the shorter path between two point,
i want a path planning algorithm
can you help me?
Sure, take a look here: http://en.wikipedia.org/wiki/Maze_solving_algorithm
bro i have one path planning algorithm in virtual world and i want in real world, can you tell me your email to send you to help me?
thanks in advance
No, sorry, that’s what the ROBOTC forums are for. You’ve been asking and asking on there and I don’t get the feeling you’re really putting in the amount of work this kind of final programming project requires. It’s a research project, so you should consider doing some research.
This is very helpful! The only trouble I’m having is with 2-D arrays; I don’t know what sort of situation would warrant one and am not sure how a condition could be set up for that. Could you add an example of a 2-D array?
Well, I use them for keeping calibration data for the gyro and compass sensors, for example. Consider this: there are four sensor ports (0-3) and if you have a HiTechnic Sensor MUX attached, each of those can have up to 4 sensors (0-3) attached to it (not likely, but that’s not the point).
So if I had a gyro attached to a HiTechnic MUX on NXT port 2 and MUX port 3, I would store that data at position 2,3. If there was no MUX, I would simply store it at 2,0. The code looks like this:
float HTGYRO_offsets[][] = {{620.0, 620.0, 620.0, 620.0}, /*!< Array for offset values. Default is 620 */ {620.0, 620.0, 620.0, 620.0}, {620.0, 620.0, 620.0, 620.0}, {620.0, 620.0, 620.0, 620.0}};
Another good things to use them for is pixel data. I made an example you can download here: https://dl.dropboxusercontent.com/u/13282461/2darray-example.c
= Xander
I like the example for the one-dimensional arrays, but is it possible for a robot to display the data that has been collected from the array or no?