What means RawY[RawY.length-1]

Dear Processing specialists,

I’m working with this project: https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer

I’d like to change the looks of the Processing window a bit: I’d like to reduce the size and position of the PULSE WAVEFORM.

I think I should change line 129:
RawY[RawY.length-1] = (1023 - Sensor) - 212; // place the new raw datapoint at the end of the array

But I have no idea what RawY[RawY.length-1] means. I tried trial and error to change some values, but no good luck.

I hope someone could shine some light on my beginners issue…
Thanks in advance.
Tom

1 Like

Let’s say you are going shopping, to buy things that you need to make a cake. You take with you a list of the ingredients you will need to get:

INGREDIENTS

  1. Milk
  2. Eggs
  3. Sugar
  4. Chocolate
  5. Flour
  6. Strawberries

Now here are some easy questions!

  • What is the list called?
  • What is the fourth thing on the list?
  • How long is the list?
  • What is the last thing on the list?

The answers are, of course:

  • The list is called “ingredients”.
  • Chocolate is the fourth thing. Did you say flour? No! If you looked at the numbers, and went with the item that came after the number 4, you actually picked the fifth thing on the list!
  • The list is SIX items long. Did you think it was five items long? You’re looking at the numbers again.
  • The last thing is strawberries. Even though the list is six things long, you don’t pick the item that comes after number 6. Because there is no number 6.

Weird, right? That’s because this list starts numbering things with a 0, not a 1.

If you wanted to represent this list in code, you could do so:

String[] ingredients = { "Milk", "Eggs", "Sugar", "Chocolate", "Flour", "Strawberries" };

This is an array. It’s just like the list. The first thing in the array is “Milk”. But to access a thing in this array, we give the index of it in a square bracket ([]) after the name of the array. Like the above list, we start with 0.

println( ingredients[0] ); // "Milk".

How long is the array? Six long. In fact, we can see that:

println( ingredients.length ); // 6

Now, what is the last thing in the array? Hint: It’s not ingredients[ ingredients.length ] !
Like the list above, there is no number 6 in this array either.

println( ingredients[ ingredients.length - 1] ) // The last thing at number 5.

The same thing is going on with your array. It’s name is RawY, and it’s length is RawY.length. But because it starts numbering at zero, there is no RawY[RawY.length] item in the array! The last item in it is RawY[RawY.length-1]

1 Like

Shorter answer: :angel:

1 Like

Thank you very much for the long and shorter explanation, TfGuy44 and GoToLoop.
Now, I understand.

So what you need to change is what is right of the =

so 1023 and 212

I agree, Chrisir. Thanks for your additional input.