Please help me with arrays

Hi all,

I've been working this last week to understand as much as possible by myself the arduino. And I am feeling that I now need to attack the understanding of ARRAYS for which I haven't found a clear tutorial for what I am looking for. Your guidance help would be much appreciated.

I am simplifying as much my project so let's say to simplify I have 3 leds on 3 analog outputs + 1 pot + 2 switches.

What I would like to do is my pot to determine the led I am "working on" - if my analog read gives me from 0 to 341 I'm on ledPin1 , if from 341 to 682 on ledPin2 else on ledPin3

if i'm working on ledPin1 - i wan't my switches to either increment or decrement the voltage value given to the ledPin1 by changing the value in the ARRAY and to turn off the other 2 ledPins.

I have written this code without ARRAYS but I'm just trying to understand the principle and how to change values within an array and how to get to work with them (read, write) to simplify my codes.

Therefore I would like to work with ARRAYS : 1) If I'm working on led1 - i want to read and change the voltage of each 3 with the switchs 2) if I go to led2 - I want to do the same 3) If I go back to led1 I want to get back to the same states for each led than when I stopped working previously on 1)

Help would be appreciated. Once again i haven't found a clear beginner's guide to ARRAYS and the tutorial on Arduino's website doesn't really help me.

Thanks in advance, m

This one ?

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

hi,
what do you mean about “3 analog outputs” ?

And here is a simply description:

int array (1,2,3,4,5,6,7,8,9,0,10,12,456354,);

int = type
array = name
= which number you want to read( leaf empty at declare and note that the list start at zero)
() = your numbers

I hope it is helpful

( I am from Germany so please take care of my language )

There are plenty of online C++ tutorials which describe the basic concepts of arrays and how to use them.

If you're looking for contrived ways to use arrays to solve your problem then I'd suggest something like:

An array of pin numbers to hold the output pin number of each LED. An array of ints to hold the analog input threshold associated with each LED. You can compare the analogRead() return value against the array values to decide which LED is currently selected. Record which LED is currently selected. Each time the input selects a different LED, turn on the newly-selected LED and turn off the other ones. Record the current brightness level of the currently-selected LED. Record the current input state of each input switch. If any switch input changes state then increment or decrement the brightness of the currently-selected LED accordingly.

You mention of varying the 'voltage value given to the ledPin1' but most Arduinos don't have that capability - perhaps you mean varying the PWM duty cycle to control the LED brightness?

legotechnicus: And here is a simply description:

The 'code' in this description is not actually valid 'C'/C++.

PeterH: There are plenty of online C++ tutorials which describe the basic concepts of arrays and how to use them.

If you're looking for contrived ways to use arrays to solve your problem then I'd suggest something like:

An array of pin numbers to hold the output pin number of each LED. An array of ints to hold the analog input threshold associated with each LED. You can compare the analogRead() return value against the array values to decide which LED is currently selected. Record which LED is currently selected. Each time the input selects a different LED, turn on the newly-selected LED and turn off the other ones. Record the current brightness level of the currently-selected LED. Record the current input state of each input switch. If any switch input changes state then increment or decrement the brightness of the currently-selected LED accordingly.

You mention of varying the 'voltage value given to the ledPin1' but most Arduinos don't have that capability - perhaps you mean varying the PWM duty cycle to control the LED brightness?

/C++.

yes for the PWM - my mistake. I fully understand what you meant and it was quite what I was thinking for but I don't know how to write it as code.

Erni: This one ?

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

I spent 2 hours trying to understand that explanation. I understand well until the "Getting a bit Fancy" part.

I think I'm about to understand the concept but i'm missing the info that would make everything clear... I've tried looking for loads of C++ tutorials.. but still... doesn't clear my mind.

If you have any clear tips/tutorials/ code/ examples... it would be much appreciated. tx

int array [] (1,2,3,4,5,6,7,8,9,0,10,12,456354,);

You're going to have considerable difficulty squeezing "456354" into an "int" on an Arduino, unless it's a Due.

meshellmesh

If you think of a variable as a box that holds a number then a 1 dimension array is a row of boxes. You can then say which box to get or put numbers, the first box is number zero as that makes calculating box numbers easier when you get that far. But right off just know to start at zero.

You can have many dimensions, they are just ways to group the boxes.

Once you have that basic understanding, try and see all the business with brackets and numbers as ways to write about the grouping of boxes. Having something to make sense with is half the battle.

The box analogy is good, but maybe think of them as old-fashioned pigeonholes in mailroom.

A row of pigeon holes is a one-dimensional array, and the address of the pigeonhole is its distance from the left-most pigeonhole. So, the left-most pigeonhole has an address of zero, the next is address one and so on. Now imagine two rows of pigeonholes. You now have two left-most holes, so you need some means of identifying the rows, so say the top-most row is row zero, and the one underneath is row one. So top row left-most is [0][0], bottom row left-most is [1][0]. So, the address of the third hole on the bottom row is [1][2].

A three-dimensional array would be another bank of two rows behind the first.

Four dimensions would be rooms of identical banks of pigeonholes, five dimensions buildings comprising rooms and so on.

The 3rd dimension can be thought of like space or like extra sets of pigeonholes. That helps when going to more dimensions or when understanding file records.

It makes much more sense to use the switch to select the LED and the pot to control it's brightness.

The coding explanation for what PeterH said looks something like this:

int potarray ; int ledarray ;

whenever you use potarray0 it finds the first value and uses that just like a regular int variable (in this case 0). Potarray[1] uses the second value (341) and potarray[2] uses the third (682).

likewise, if you are using pins 1,2, and 3 for your leds, ledarray[0] will will use 1, ledarray[1] will use 2, and so on.

so a psuedo-code way of selecting pin 3 when the pot value is high would look like:

if potvalue > potarray[2] then ledarray[0] = low ledarray[1] = low ledarray[2] = high

The advantage of this is if you change the led pins to, say, 3, 7, and 13, all you have to do is change the ledarray[] to: int ledarray ;

and now [0] = pin 3, [1] = 7, and [2] = 13

You can also add more leds and pot positions by just adding more numbers to the array.

let me know if that helps

The coding explanation for what PeterH said looks something like this:

int potarray ; int ledarray ;

More like:

int potarray [] = {0, 341, 682};
int ledarray [] = {1, 2, 3};

The compiler is very fussy about this sort of thing - you should be too.

Ah yes, indeed.

I've been jumping around in languages lately. Hard to remember which syntax is which.

That's why I avoided learning Java while working in C++. I didn't need the confusion.

Hi All,

thanks so much for your help. I haven't stopped trying to figure it out since I posted and I think I finally understood how to start working with these bloody arrays ! :cold_sweat:

Again : tx !

Meshell

A bit late on picking up on this thread but I wonder if these pages are of any use to you:- http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

http://www.thebox.myzen.co.uk/Tutorial/PWM.html

and

http://www.thebox.myzen.co.uk/Workshop/LED_Matrix.html