[NXC] What is wrong with this sound sensor graphing program?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
airplaneguru
Posts: 1
Joined: 16 Sep 2011, 03:06

[NXC] What is wrong with this sound sensor graphing program?

Post by airplaneguru »

Hey,

How come this simple sound sensor graphing program I made in NXC is not working? Nothing is showing up on the screen. It should log the sound level each second and display the sound level on a bar graph that moves backwards. Do you have any suggestions?

Thank you for your help!
-Aaron

Code: Select all

#define MIC SENSOR_1



task main()
{
  SetSensorSound(IN_1);
  int x[];
  ArrayInit(x, 0, 100);
  while(true){
    ClearScreen();
    x[100] = MIC;
    for(int i=99; i>=0; i-=1 ){
    x[i] = x[i++];
    }
    int i=99;
    for(int p=0; p<=100; p++ ){
    LineOut(p,0,p,x[p]);
    }
    int p=0;

    Wait(1000);

  }
}
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] What is wrong with this sound sensor graphing program?

Post by muntoo »

First of all, x[100] doesn't exist. You need to do ArrayInit(x, 0, 101); if you want that. (Not sure why you would, though.)

Your program is stuck on:

Code: Select all

		for (int i = 99; i >= 0; --i) {
			x[i] = x[i++];
		}
i will never equal anything less than 99.


Try:

Code: Select all

#define MIC SENSOR_1


task main() {
	SetSensorSound(IN_1);

	int y[];
	ArrayInit(y, 0, 100);

	while (true) {
		for (int i = 0; i < 99; ++i) {
			y[i] = y[i + 1];
		}
		y[99] = MIC;
		
		ClearScreen();
		
		for (int x = 0; x < 100; ++x) {
			LineOut(x, 0, x, y[x]);
		}

		Wait(1000);
	}
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] What is wrong with this sound sensor graphing program?

Post by afanofosc »

Before trying muntoo's suggestion, please tell us what you want the for loop to accomplish. It is not at all clear from your uncommented code. It would be utter nonsense to iterate through every element in your 100 element array and move the value from the next element into the current element so I am sure that is not what you are trying to do.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] What is wrong with this sound sensor graphing program?

Post by muntoo »

.@John He's trying to make a FIFO stack, push off an old value, add the new sensor value, and display those results on the screen.

I suppose a faster way to do it would be:

Code: Select all

#define MIC SENSOR_1


#define READ(arr,len,start,offset) arr[(start+offset)%len]
#define NEXT(len,start) start = (start + 1) % len;


task main() {
   SetSensorSound(IN_1);

   int y[];
   unsigned int y_start = 0;
   unsigned int y_len = 200;
   ArrayInit(y, 0, 200);

   while (true) {
      READ(y, y_len, y_start, 100) = MIC;
      NEXT(y_len, y_start)
      
      ClearScreen();
      
      for (int x = 0; x < 100; ++x) {
         LineOut(x, 0, x, READ(y, y_len, y_start, x));
      }

      Wait(1000);
   }
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] What is wrong with this sound sensor graphing program?

Post by afanofosc »

muntoo, I do not think it is possible for you to know what someone else was trying to do with the kind of certainty you possess. Why not let him answer my question? If you want to simulate a stack with 100 maximum entries then use two arrays, ArrayBuild, ArraySubset, and ArrayInit.

Code: Select all

task main()
{
  SetSensorSound(S1); // don't use IN_1 in NXC code - it is for NBC use
  int x[100], tmp[];
  while (true) {
    int value = MIC;
//    ArraySubset(tmp, x, 0, 99); // all elements except the last
//    ArrayBuild(x, value, tmp); // put the new value at the front
    ArraySubset(tmp, x, 1, NA); // all elements except the first
    ArrayBuild(x, tmp, value); // put the new value at the end
    ArrayInit(tmp, 0, 0); // free memory

    for(int p=0; p<100; p++ ) { // I disagree with those who recommend using pre-increment - especially in NXC code
      LineOut(p,0,p,x[p]);
    }

    Wait(SEC_1);
  }
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests