Arduino: Learning some more (for loops, arrays and if functions)

We showed the animations we did to the class. We moved on to learn about…

The concept of iterations. iterative loops. I realised that the professor would explain for loops and I had I not already tried it I would have had great difficulty in understanding what’s happening (yes!).

For loop explanation:
We pulled up the same reference for forloops that I had found over the weekend on the Arduinos official website.

initialization – initialize a variable, used as a counter (start at 0 [x value] and count up)

now x value can be changed to ‘i’, since i=0. i becomes placeholder

need to tell it to count up/down and then STOP at something.

i=i+1 >>> add 1 to 0 =1. now i is 1. i=i+1 >>> add 1 to 1 + 2. now i is 2. i++ is shortcut. add 1 to it and reassign

stop counting at 7. when i = 7.

The professor explained the serial monitor which monitored what’s happening with i when we placed 


Serial.println(i);


in the void loop.

With our current function all we got were 7s. We tried using the = sign in the for loop. We thought that one = is an assignment. Assigned i to be 0 AND 7. When we put TWO = it become a check. 1 = is an assignment. 2 = is a check (number that’s being checked against).

This didn’t show anything. == was not a valid command. We ha to use the < or > signs.

cmd+t was another shortcut we learned to keep the code neat and properly indented.

Arrays explanation:
We learnt that an array is a variable with dimension, it can sort multiple values.

datatype colors[ ] <<<<<<<<<< brackets establishes it as an array.

A 2dimensional array has two sets of brackets [][] for every value in one array, there is another array. So it’s like an array inside an array.

pixels[2][4] { {1,2,3,4,5….}, {}, {}……} <<<<<<<<this piece of code means that the program looks inside 2nd container, and then the 4th one in that set

This way we could imagine the arrays as the pixels on the board. While doing this, a lot of us found that we were running out of space since each set (inside the pixels array) was like one frame, and to get an animation we needed many frames.

Going from uint32_t or int to byte halved the storage usage. (Although, changing the colors datatype to byte ruined the colors, so I just changed the ints to bytes). 

We used a for loop to draw the pixels in the arrays:

uint32_t colors[3] {0x000001, 0xaa1010, 0x6e6e6e};

byte pixels[8][8] = {
{1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0},
{2, 0, 1, 1, 1, 1, 0, 0},
{2, 0, 0, 0, 0, 1, 0, 0},
{2, 2, 2, 2, 0, 1, 1, 1},
{0, 0, 0, 2, 0, 0, 0, 0},
{0, 0, 0, 2, 2, 2, 2, 2},
{0, 0, 0, 0, 0, 0, 0, 0},
};

void displayFrame() {

for (int y = 0 ; y < 8; y++) {
for (int x = 0 ; x < 8; x++) {

int i = pixels [y][x];
//i now is equal to?

Rb.setPixelXY(x, y, colors[i]);
}
};
}

void setup() {
Rb.init();
}

void loop() {
displayFrame();
delay(500);
Rb.blankDisplay();
delay(500);
}

The first image is the one frame I drew.
array00001The next 8 images show the loop I made by creating 8 arrays inside

byte pixels [8][8] by adding an extra [8] in the beginning. So

bye pixels [8][8][8]

This just made the for loop play all the frames (arrays) with the delay of 500.


and now we were left to make a cool animation for the rest of the lesson…

Experiment Array (looping snow):
array00002

Experiment Array (snooze):
I successfully made a looping snooze using this technique!
array00003

Experiment IF (revisiting snooze, because I’m stubborn):
Since I got the looping snooze to work using arrays, I wanted to try getting it to work using my older method. The method of shifting the Z using a for loop. My sister didn’t help me because she said I should be able to figure it out by myself now…

I wasn’t able to. The professor suggested that I use an IF function instead of the for loop:

int number = 0;

void loop() {
snoozeScroll(number, blue0);
delay(300);
Rb.blankDisplay();

number++;
if (number > 15) {
number = 9;
}
}

successz

annnd it worked!! The difficult part was finding the correct combination of numbers. I thought it was just trial and error but later realised that it was counting the number of shifts in relation to the pixels.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s