View unanswered posts | View active topics It is currently Wed Jul 30, 2014 2:50 pm






Reply to topic  [ 30 posts ]  Go to page 1, 2  Next
Using Arrays 
Author Message
Rookie

Joined: Thu Mar 22, 2012 12:12 pm
Posts: 6
Post Using Arrays
Hi i'm new here so sorry if i'm posting in the wrong place, I tried searching but couldn't find the answer :breakcomputer:

I'm trying to use an array to record the light sensor every second for 60 seconds and sort it so it displays the highest and lowest value, however I can't find any tutorials online to show me any code of the best way to sort. Does anyone know where I could find something like this? Or have I overlooked something very obvious?!


Thu Mar 22, 2012 12:17 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using Arrays
Did you achieve the writing to the array part? And do you just want the highest and lowest value, or do you want to order the whole array?

_________________
sudo rm -rf /


Thu Mar 22, 2012 12:20 pm
Profile
Rookie

Joined: Thu Mar 22, 2012 12:12 pm
Posts: 6
Post Re: Using Arrays
I havn't yet, I just want the highest and lowest value, the rest aren't needed.


Thu Mar 22, 2012 12:28 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using Arrays
Then using an array is a waste of memory. Just use two variables.
Code:
int highestValue = 0;
int lowestValue = 0;

for(int i = 0; i < 60; i++){
  wait1Msec(1000); //wait for 1 second;
  if(SensorValue[lightSensor] > highestValue){
    highestValue = SensorValue[lightSensor];
  else if(SensorValue[lightSensor] < lowestValue){
    lowestValue = SensorValue[lightSensor];
  }
}

_________________
sudo rm -rf /


Last edited by magicode on Fri Mar 23, 2012 12:39 am, edited 1 time in total.



Thu Mar 22, 2012 12:38 pm
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Using Arrays
Good response magicode!

I've added some things that might make the program run more smoothly:
Code:
int highestValue = 0;
int lowestValue = 0;
int lightValue = 0;
task main()
{
   highestValue = SensorValue[lightSensor];
   lowestValue = SensorValue[lightSensor];

   for(int i = 0; i < 60; i++){
      wait1Msec(1000); //wait for 1 second;
      lightValue = SensorValue[lightSensor];
      if(lightValue > highestValue){
         highestValue = lightValue;
      }
      if(lightValue < lowestValue){
         lowestValue = lightValue;
      }
   }
}


Also, I'm still not sure if this code achieves the Original Poster's wishes. It's not entirely clear if that person wants the program to check for the highest and lowest values every second for 60 seconds and then stop, or if they want to have a running check of the highest and lowest values of any given sixty second window they happen to be in (I.E. if you were at 67 seconds, all data points before 7 seconds would be ignored; the program may then give different results for highs and lows for just the past 60 seconds)


Last edited by sumasmreq on Fri Mar 23, 2012 10:49 am, edited 1 time in total.



Thu Mar 22, 2012 2:34 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using Arrays
Ah, good catch with using the second if. I must have deleted that line instead of copy/pasting. I fixed it now to avoid confusion. I'm curious as to why you made the other modifications though. A valid light sensor reading won't be less than zero, so setting the high and low variables to zero works perfectly. Then, why did you add the extra variable to hold the light value? And I would personally put an else in front of that second if to avoid comparing the value needlessly a second time.

_________________
sudo rm -rf /


Fri Mar 23, 2012 12:42 am
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Using Arrays
Quote:
Ah, good catch with using the second if. I must have deleted that line instead of copy/pasting. I fixed it now to avoid confusion. I'm curious as to why you made the other modifications though. A valid light sensor reading won't be less than zero, so setting the high and low variables to zero works perfectly.

You are correct, the light sensor reading won't ever be less than zero. That is why you have to set the lowestValue to the initial light sensor reading, or else every time you run "else if(SensorValue[lightSensor] < lowestValue)", that statement will never evaluate to true, lowestValue will always equal zero, and it won't be giving an accurate lowest measurement from the light sensor.

Quote:
Then, why did you add the extra variable to hold the light value?

Three related reasons (not terribly important though). One, it basically stores a temporary copy of the light sensor value, so that you aren't having to poll it four separate times. Two, (this is a minor issue) if in the unlikely event that the value changed in between when you executed the if statement and when you set the value variable, you would be setting the value variable to a value different than was evaluated, which could lead to errors. Three, this way if you want to change the sensor input function, you wouldn't have to change it in four different places.

Quote:
And I would personally put an else in front of that second if to avoid comparing the value needlessly a second time.

That would probably be a good idea. For some reason, I was thinking that there would be one case where the value would both be the lowest and highest value (at the start of the program), but that wouldn't be an issue if you set your variables to a initial light sensor reading at the start of the code.

Let me know if I'm wrong on any of this, and thanks for discussing code with me, magicode! I appreciate talking with smart people such as yourself!


Fri Mar 23, 2012 1:00 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using Arrays
ARH, I'm sorry. Ignore my previous comments. I was working with sorting algorithms before this, and my brains all jumbled with different requirements of code. I wasn't aware that reading a sensorvalue caused it to poll. I'm not sure how that works with the mindstorms with digital input. I was just programming a Cortex M3, and the ADC registers as set up as continuous read FIFO buffers. And you're right about the variable as well. As I said, I was working on a sorting algorithm project, and I'm thinking of all the wrong kinds of efficiency.

_________________
sudo rm -rf /


Fri Mar 23, 2012 1:20 am
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Using Arrays
It's all good! :-)

I'm not sure if calling SensorValue[lightSensor] on the NXT or the Cortex polls the ADC, or just grabs a buffered value from memory. I was making an assumption that it would be better to assume it is polled and copy it, than to assume it's not, and potentially run into problems.
What sorting algorithm are you working on -- and is it for the VEX Cortex?

Also, I'm Simmons 2.0 from the VEX forum (so that you don't think I have split personality disorder. :-) I have two different usernames (one for the VEX forum, one for the ROBOTC forum) because it seemed like a good idea at the time...)


Fri Mar 23, 2012 1:32 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using Arrays
Hah, no, I was working on a modified radix sort. I was programming the M3 (a different microprocessor, not the VEX Cortex) a couple weeks ago for a micromouse competition. And to get the thread back on topic so the mods don't get mad at me:
xiah, if you need the values to be counted while other stuff is going on in a while loop, you can use timers.
Code:

task main(){

  int lightVal = SensorValue[lightSensor];
  int lightHigh = lightVal;
  int lightLow = lightVal;
  int numSamples = 60;
  ClearTimer(T1);
  while(true){
    if(numsamples > 0 && time1[T1] >= 1000){
      lightVal = SensorValue[lightSensor];
      if(lightVal > lightHigh){
         lightHigh = lightVal;
      }
      if(lightVal < lightLow){
         lightLow = lightVal;
      }
      numSamples--;
      ClearTimer(T1);
    }
    //other code
  }
}

_________________
sudo rm -rf /


Fri Mar 23, 2012 1:49 am
Profile
Rookie

Joined: Thu Mar 22, 2012 12:12 pm
Posts: 6
Post Re: Using Arrays
I'll have a go of all of these codes and see what happens, thanks alot for your help :)


Fri Mar 23, 2012 9:59 am
Profile
Rookie

Joined: Thu Mar 22, 2012 12:12 pm
Posts: 6
Post Re: Using Arrays
That last one is almost perfect, I think i've explained the program slightly wrong though. The program is to record and display the highest and lowest light value after 60 seconds then the program will terminate, think i've got it all now thanks to your help!


Thu Mar 29, 2012 10:18 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using Arrays
Do you want the value to be recorded after very second, or continuously? Do you want other functions like motors to run while this is happening?

_________________
sudo rm -rf /


Thu Mar 29, 2012 10:54 am
Profile
Rookie

Joined: Thu Mar 22, 2012 12:12 pm
Posts: 6
Post Re: Using Arrays
I'll explain fully and show you the code I have so far.
1. Robot to roam inside an area defined by walls of an arena, the robot shouldn't touch the outside of the walls.
2. If the robot comes close to the wall it should back up, turn around randomly and head off in another direction.
3. The robot must avoid obstacles at all times (Pretty much same as above i think?)
4. The robot must do all of these operations for 1 minute, after 1 minute stop and display the results.
5. The readings must be taken at 1 second intervals unless the robot is manoeuvering, in which case stop recording until the robot has finished manoeuvering.
6. While roaming the robot must take a recording of -
i. Light is reflected by the surface area
ii. Background noise there is
iii. The distance from the walls to the robot
7. On stopping the robot must display a screen stating the maximum and minimum, light, distance from the walls and the maximum background noise.
All of my code I think/hope so far covers all of this! I have 2 other parts which are below which I havn't had a look at yet, i'm just hoping to get the first parts correct first of all.
Code:
File "ass2.c" compiled on Mar 29 2012 16:00:49
#pragma config(Sensor, S2,     soundSensor,         sensorSoundDB)
#pragma config(Sensor, S3,     lightSensor,         sensorLightActive)
#pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{   
  //for inial movement
  int sonarValue = 0;
  int sonarHigh = sonarValue;
  int sonarLow = sonarValue;
  //for light value statistic
  int lightVal = SensorValue(lightSensor);
  int lightHigh = lightVal;
  int lightLow = lightVal;
  // for sound statistic
  int soundVal = SensorValue(soundSensor);
  int soundHigh = soundVal;
  // timer
  time10(stopWatch);
  ClearTimer(stopWatch);
 
 
  while(stopWatch < 6000)       
  {
    sonarValue = SensorValue(sonarSensor);
          nxtDisplayCenteredTextLine(0, "Sonar Reading");
        nxtDisplayCenteredBigTextLine(2, "%d", sonarValue);
          wait1Msec(100);
          if(sonarValue > sonarHigh)
    {
      sonarHigh = sonarValue;
    }
    if(sonarValue < sonarLow)
    {
      sonarLow = sonarValue;
    }
           
 
           lightVal = SensorValue[lightSensor];
      if(lightVal > lightHigh){
         lightHigh = lightVal;
      }
      if(lightVal < lightLow){
        lightLow = lightVal;
      }
 
      soundVal = SensorValue(soundSensor);
      if(soundVal > soundHigh)
      {
        soundHigh = soundVal;     
      }
     
    motor[motorC] = 30;
    motor[motorB] = 30;
 
    if(sonarValue < 30)
    {
     motor[motorC] = -30;
     motor[motorB] = -30;
     wait1Msec(1000);
     motor[motorC] = random(200)-100;
     motor[motorB] = random(200)-100;
     wait1Msec(1000);
   }
  }
  else{
    do stats()
  }
}

void stats()
{
nxtDisplayCenteredTextLine(0, "Light Value");
nxtDisplayCenteredTextLine(1, "%d Lowest", lightLow);
nxtDisplayCenteredTextLine(2, "%d Highest", lightHigh);
nxtDisplayCenteredTextLine(3, "Sonar Reading");
nxtDisplayCenteredTextLine(4, "%d Highest", sonarHigh);
nxtDisplayCenteredTextLine(5, "%d Lowest", sonarLow);
nxtDisplayCenteredTextLine(6, "Sound value");
nxtDisplayCenteredTextLine(7, "d Highest", soundHigh);
wait10Msec(100);
}


8. The statistics must then be displayed at as a bar chart (simple vertical lines are sufficient) with the reading type displayed in text across the middle of the chart.
9. On pressing the left arrow key the screen must change to display the graphs one at a time. The displays must be in the order:-
i. As read during the run, Light, Sound, Distance
ii. Sort ascending, Light, Sound, Distance


Thu Mar 29, 2012 11:09 am
Profile
Rookie

Joined: Wed Apr 04, 2012 11:50 pm
Posts: 6
Post Re: Using Arrays
I was wondering how you would store the light, ultrasound and sound readings all in one array and how you would recall the data, there is hardly any documentation on how to set up this kind of array.

I think it is called a 2d array


Wed Apr 04, 2012 11:53 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 30 posts ]  Go to page 1, 2  Next

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.