Hello there Arduino users. Im new on this forum and i hope theres someone here that can help me. Well: i managed to create on my breadboard a 3x3 led matrix. Using some simple for`s instructions in void setup() my matrix looks like these (i will use pins number - like i connected the leds to the arduino). I named the matrix led.
11 12 13
8 9 10
5 6 7
What i want to do: when the program uploads, the led[2][2] (the one in the center) to light up. Using serial monitor, type W (for up) and so on with A S D, that will control the possition of the lighten led. So if i type W, move led UP, and so on... But when i light the next led, turn down the previous led.
I can`t make it work... Any help? This is my code for the letter W.
int i=2; int j=2;
void loop()
{
digitalWrite(led[i][j], HIGH); //turns on led[2][2] the center one
if(Serial.available() > 0)
{
char litera = Serial.read();
if(litera == 'w')
{
digitalWrite(led[i][j], LOW); //should turn the [2][2] led down
delay(100);
digitalWrite(led[i--][j], HIGH);//should turn the upper led
delay(1500);
}
}
}
I need to get it work for at least the letter W (up direction), then i think i can do it for the other letters, using a SWITCH CASE. Thanks for reading, hope there`s someone that can help me
First of all, ALWAYS post your complete sketch. Don't ask us to guess what the rest looks like.
Secondly, ALWAYS use code tags (the # button above the box where you type your post). If you read what you posted, you will see that not using code tags has altered the sketch as you see it on your screen. For example I suspect you typedled[i][j] but this has appeared as "led*[j]".* Paul
So this is my full code. In the setup function i managed to make a matrix like my leds are connected to the arduino:
11 12 13 8 9 10 5 6 7
I assume that the problem is in the IF instruction, but its logical that i turn off the led i turned on before (led[2][2]). and light up the one above (i=i-1 or i-- so its the above row). But it does not work.
As i can see... the result i come up to is the following one:
The matrix of leds starts with led[2][2] turned on, after entering in serial monitor: w, the led above turns on, but the other one (led[2][2]) remains turned on. I think it`s because void loop repeats this in the IF that compares litera with w:
There is something you do not understand yet about arrays in C/C++.
When you make an array like "int a[3]", the indexes you should use are a[0], a[1] and a[2]. You must not use a[3]. If you do, there will be no warning message, but you will get an incorrect/unexpected result.
This is different to BASIC and some other languages.
Using the declaration i made in the setup, led[2][2] is the center one. From what you said, the center one would be a[1][1]....
Let's say i make the matrix declaration like you said, it would be: (i won't use brakets)
a00 a01 a02 a10 a11 a12 a20 a21 a22
I need to make the instruction for letter ("litera" in romanian) 'w'. I just can't turn off the starting led, and turn on the above one, and after turning this 'w' led, it would stay turned on untill i type a new letter (w a s d) in serial monitor.
So now you know about array indexes in C, does your sketch now work correctly?
Another important thing to learn is the difference between saying "--i" and "i--". The former decrements i before its value is used and the latter decrements i after its value is used.
What do you think your loop function does?
When it receives a 'w' it will put the pin low for 0.1 second at all other times it will be high. This will be hard to see.
At first: when i type w, the center led blinks. The second time i type w, the above led turns on but the center one still stays turned on.
Can you help me with the loop function?
It changes the value of the variable I, it decrementing it.
As nothing ever increments it the more you input w the smaller it gets until it is negitave.
What exactly do you want to do and how are the LEDs wired up? Unless we know the circuit the code makes little sense.
Also look at how you initialise that array, you are going round the houses with the way you do it.
You can do it in the same statement that you declair the array.
My setup is this: all the 9 leds have ground(-) in common, so they are wired up in parallel. Next, i will type the matrix as i wired up the positive legs to the arduino:
11 12 13
8 9 10
5 6 7
What i want to do: enter in serial monitor letter: "s" for START, and turn on the center led (pin 9). Next: enter in serial monitor one of the following letters: w (turn on above led), a (turn on left led), s (turn on under led) and d (turn on right led). I know that sometimes the counter i or j will be negative. When i type a letter, turn off the previous led and turn on the next one, depending the dorection ( w a s d ). And at the end, when i want to stop playing with leds, type "t" for STOP, and turn off the last led.
For a start why not have meaningful names? Change i and j to row and column.
Next change the variables when you have inputted data. Remember the index values to use are 0 to 2. Make the index variables wrap round. That is if they exceed the limits of the array in either direction correct them.
An example of doing this is:-
if(row > 2) row = 0;
if(row < 0) row = 2;
When you receive an 's' make row and column both equal to 1, then turn on the LED given by row and column.
Then when you get a 'w', first turn off the LED given by row and column, then decrement column, adjust for wrap round and then turn on the LED given by row and column.
The same thing for the other directions.
When you get 't' for stop just turn off the LED given by row and column.
I didn`t write the instructions for A and D because i wanted to see if what i did untill now works. And it works. Every time i type W or S, the previous led turns off and the next one turns on. Thanks very much.
Last Q: is there any chance to make serial monitor (or using other way) to "auto-enter", so every time i type a letter, "he" hits enter?