# How to read an element of a specified array?

Hello everyone!

I'm re-getting started with Arduino after quite a long time, and have forgotten a few things...

I have a simple seven-segment display, and I want to type a number (in word form) into the serial monitor, and have that number be displayed for a second on the display.

The code is going fine and well, using arrays for each number... but I can't seem to make it read the array I say.

I store the response from the serial monitor in a variable, "recNum", and move on in my code.

When it comes time to read the specified array, this is the command I use:

y = recNum[loopCount];

Where y and loopCount are previously defined.

This is the error I get:

SevenSegmentDisplay:61: error: invalid types 'int[int]' for array subscript

So what do I do? Is there anyway to read an element of a varying array?

Post the whole of your program and make sure that you use code tags.

Alright, here we go:

``````int recNum = 0;
int One[2] = {12, 11};
int Two[5] = {13, 12, 10, 9, 7};
int Three[5] = {13, 12, 11, 10, 7};
int Four[4] = {12, 11, 8, 7};
int Five[5] = {13, 11, 10, 8, 7};
int Six[6] = {13, 11, 10, 9, 8, 7};
int Seven[3] = {13, 12, 11};
int Eight[7] = {13, 12, 11, 10, 9, 8, 7};
int Nine[6] = {13, 12, 11, 10, 8, 7};
int Zero[6] = {13, 12, 11, 10, 9, 8};

/*
13 is Top
12 is Top-Right
11 is Bottom-Right
10 is Bottom
9 is Bottom-Left
8 is Top-Left
7 is Middle
6 is Decimal Point
*/

int x = 0;
//The given size of the current Array being read
int y = 0;
//The value of the current element being read

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
}

void loop()
{
if(Serial.available() > 0)
{
Number();
delay(1000);
Reset();
delay(1000);
}
}

int Number()
{
int loopCount = 0;
x = sizeof(recNum) / 2;
while(loopCount < x)
{
y = recNum[loopCount];
//Error is given in the line above
digitalWrite(y, HIGH);
loopCount = loopCount + 1;
}
}

int Reset()
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
}
``````

`  y = recNum[loopCount];`recNum is not defined as an array in your program so this line is invalid.

Alright, point taken. Then how would I make it so that what I type corresponds to an existing array?

(A given is that I will type One, Two, Three, etc.)

The names of arrays (and functions vars constants and anything else) are lost during the build process so you need to stop thinking in the way you are!.

Mark

I want to type a number (in word form)

You are currently only reading 1 character though, so do you really mean "One", "Two" etc.

Rob

Yes, that's what I mean. I suppose alternatively I could input a number, and use a switch will cases spanning all the arrays... do you have any suggestions?

ManOfAMillionParts:
Alright, point taken. Then how would I make it so that what I type corresponds to an existing array?

(A given is that I will type One, Two, Three, etc.)

The simplest way to do that would be to put your string constants in an array of char pointers, and when you have read in the whole token you would loop through the array of pointers looking for a string that matched your input token. If you find a match, the array index would give you the corresponding numeric value. Remember to deal with the non-matching case too.

do you have any suggestions?

Assuming you change the code to read commands and not just characters, I would have a couple of arrays, one for the command ("One", "Two" etc) and another for the pins level for each segment.

Try this to get you started.

``````byte pins[] = {3,4,5,6,7,8,9,10};    // you add the right numbers, should be in order A thru to G and DP

char * cmds[] = {"One", "Two", "etc"};  // you ad the rest of the commands

byte pinVals [][8] = {
// A    B     C    D    E    F     G    dp
{LOW, HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW},   // Pin states for the "One" command
{LOW, HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW}    // Pin states for the "Two" command
// you fill in the rest and mod the above to be the right values
};

int getCommand() {
return 3;   // just for testing, you will have to write this but you can test everything before doing that
}

void setup() {
for (int i = 0; i < 8; i++) {
pinMode (pins[i], OUTPUT);
digitalWrite (pins[i], LOW);
}
}

void loop () {
int cmd = getCommand(); // this waits for a CR/LF and then scans the cmds array looking for a match
// if found it returns the index of the match
if (cmd != -1) {
for (int i = 0; i < 8; i++) {
digitalWrite(pins[i], pinVals[cmd][i]);

}
delay(1000);
}
}
``````

BTW the segments are known as A thru G and DP, not upper left etc.

Rob

Very nice sir, I like it a lot. But I've still not worked with Arduino for quite a while, so I need some assistance on the getCommand() function...

I remember if(Serial.available > 0) , and Serial.read(), but I don't know where to go from there unfortunately... I do not remember what I am reading, and what I should be reading if I am inputting full words...

On top of that, what data format should I use in the switch statement, and should I even use a switch statement?

I apologize for my lack of applicable knowledge, and it was sweet of you to write out the code for me... I am a visual learner by far (or rather a visual "re-learner" in this case!).

I remember if(Serial.available > 0) , and Serial.read(), but I don't know where to go from there unfortunately

Do one thing at a time, try to get that working it can be 100% tested with no getCommand() func.

should I even use a switch statement?

Not required as it currently stands.

Rob

If you are trying to use one piece of code to refer to multiple arrays, that is possible. An array name is just a pointer to int. This
should be legal, for example

``````int First[]={ 0,1,2,3,4,5 } ;
int Second[]={ 11,12,13,14,1,2,3 };

void printList( int* ptr, int len )
{
for ( int i=0 ; i<len ; i++ )
{
Serial.print( ptr[i] );
Serial.print(" ");
}
Serial.println();
}

void loop()
{
int* p ;
int len ;

p=First ;
len=sizeof(First);
printList(p,len);

p=Second ;
len=sizeof(Second);
printList(p,len);
}
``````