Game of Life project for the LPC1768 microcontroller.

Dependencies:   N5110 mbed

Committer:
matirc
Date:
Tue Jul 18 10:05:17 2017 +0000
Revision:
0:909e5e22905c
Final Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
matirc 0:909e5e22905c 1 #include "mbed.h"
matirc 0:909e5e22905c 2 #include "N5110.h"
matirc 0:909e5e22905c 3 #include "tone.h"
matirc 0:909e5e22905c 4
matirc 0:909e5e22905c 5 #define PI 3.14159265359 //constant for the music part of the program
matirc 0:909e5e22905c 6
matirc 0:909e5e22905c 7
matirc 0:909e5e22905c 8 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
matirc 0:909e5e22905c 9 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);//nokia screen connections
matirc 0:909e5e22905c 10 BusOut display(p22,p23,p24,p25,p26,p27,p28,p29);//bus connections for the cell counter display
matirc 0:909e5e22905c 11 char nextstep[84][48];//array for the next screen after applying to the first screen the rules of life
matirc 0:909e5e22905c 12 char step[84][48];//array for the first screen where the rules of life will be applyied
matirc 0:909e5e22905c 13 DigitalIn stopbutton(PTB9);//stop button
matirc 0:909e5e22905c 14 DigitalIn resetbutton(PTD0);//reset button
matirc 0:909e5e22905c 15 DigitalIn startgame(PTC17);//button for starting the game
matirc 0:909e5e22905c 16 AnalogIn brightness(PTC12);//potentiometer connection for controling the nokia LEDs brightness
matirc 0:909e5e22905c 17 AnalogIn speed(PTB2);//potentiometer for controling the speed of the game of life
matirc 0:909e5e22905c 18
matirc 0:909e5e22905c 19 int i;//variable i
matirc 0:909e5e22905c 20 int j;//variable j
matirc 0:909e5e22905c 21 int cellcounter=0;//variable cell counter set to 0.This variable will count the alive cells in the screen
matirc 0:909e5e22905c 22
matirc 0:909e5e22905c 23 char neigbours();//variable that counts the number of neighbours a cell has
matirc 0:909e5e22905c 24
matirc 0:909e5e22905c 25
matirc 0:909e5e22905c 26
matirc 0:909e5e22905c 27
matirc 0:909e5e22905c 28
matirc 0:909e5e22905c 29
matirc 0:909e5e22905c 30
matirc 0:909e5e22905c 31
matirc 0:909e5e22905c 32 //HERE I SET ALL THE PINS AND VARIABLES THAT I WILL USE DURING THE MUSIC AND LEDs FEEDBACK
matirc 0:909e5e22905c 33 BusOut leds(PTA1,PTA2,PTC2,PTC3);//LEDS bus of the mbed that will be synchronized with the music pattern
matirc 0:909e5e22905c 34 Timer noteTimer; // timer for note duration
matirc 0:909e5e22905c 35 AnalogOut aout(PTC10); // DAC on pin 18 for the music output
matirc 0:909e5e22905c 36
matirc 0:909e5e22905c 37
matirc 0:909e5e22905c 38 void init_array(); // function to initialise sample array
matirc 0:909e5e22905c 39 void tone(float frequency,float duration); // play a tone of set frequency for given duration
matirc 0:909e5e22905c 40
matirc 0:909e5e22905c 41
matirc 0:909e5e22905c 42 int n = 32; // number of samples for the music pattern
matirc 0:909e5e22905c 43 float y[32]; // array to store samples in the music pattern
matirc 0:909e5e22905c 44 float BPM = 100.0; // beats per minute of the song
matirc 0:909e5e22905c 45
matirc 0:909e5e22905c 46
matirc 0:909e5e22905c 47 //LEDs specific pattern that will be synchronized with each note of the song
matirc 0:909e5e22905c 48 int leds_values[66]={
matirc 0:909e5e22905c 49 1,2,4,8,4,2,1,
matirc 0:909e5e22905c 50 9,6,9,15,8,12,14,
matirc 0:909e5e22905c 51 15,7,3,1,3,7,
matirc 0:909e5e22905c 52 15,14,12,8,9,6,
matirc 0:909e5e22905c 53 15,9,6,15,9,6,
matirc 0:909e5e22905c 54 15,7,3,1,3,7,15,14,12,
matirc 0:909e5e22905c 55 8,15,7,3,1,3,7,
matirc 0:909e5e22905c 56 15,14,12,8,1,2,4,8,
matirc 0:909e5e22905c 57 0,1,3,7,15,0,
matirc 0:909e5e22905c 58 1,3,7,15};
matirc 0:909e5e22905c 59
matirc 0:909e5e22905c 60 //note array that will be synchronized with the LEDs pattern and with the note duration. The song is the imperial march from StarWars
matirc 0:909e5e22905c 61 float note_array[66] = {
matirc 0:909e5e22905c 62 NOTE_A4,NOTE_A4,NOTE_A4,NOTE_F4,NOTE_C5,NOTE_A4,
matirc 0:909e5e22905c 63 NOTE_F4,NOTE_C5,NOTE_A4,NOTE_E5,NOTE_E5,NOTE_E5,
matirc 0:909e5e22905c 64 NOTE_F5,NOTE_C5,NOTE_GS4,NOTE_D5,NOTE_F4,NOTE_C5,
matirc 0:909e5e22905c 65 NOTE_A4,NOTE_A5,NOTE_A4,NOTE_A4,NOTE_A5,
matirc 0:909e5e22905c 66 NOTE_GS5,NOTE_G5,NOTE_FS5,NOTE_F5,NOTE_FS5,
matirc 0:909e5e22905c 67 NOTE_AS4,NOTE_DS5,NOTE_D5,NOTE_CS5,NOTE_C5,NOTE_B4,
matirc 0:909e5e22905c 68 NOTE_C5,NOTE_F4,NOTE_GS4,NOTE_F4,NOTE_A4,
matirc 0:909e5e22905c 69 NOTE_C5,NOTE_A4,NOTE_C5,NOTE_E5,NOTE_A5,NOTE_A4,
matirc 0:909e5e22905c 70 NOTE_A4,NOTE_A5,NOTE_GS5,NOTE_G5,NOTE_FS5,NOTE_F5,
matirc 0:909e5e22905c 71 NOTE_FS5,NOTE_AS4,NOTE_DS5,NOTE_D5,NOTE_CS5,
matirc 0:909e5e22905c 72 NOTE_C5,NOTE_B4,NOTE_C5,NOTE_F4,NOTE_GS4,NOTE_F4,
matirc 0:909e5e22905c 73 NOTE_C5,NOTE_A4,NOTE_F4,NOTE_C4
matirc 0:909e5e22905c 74 };
matirc 0:909e5e22905c 75 float note_duration[66]={//beats of the song, that is, the duration each note will have
matirc 0:909e5e22905c 76 2,2,2,3,7,2,3,7,1,2,
matirc 0:909e5e22905c 77 2,2,3,7,2,3,7,1,2,3,
matirc 0:909e5e22905c 78 7,2,4,4,8,8,4,4,2,
matirc 0:909e5e22905c 79 4,2,8,8,4,8,2,3,7,
matirc 0:909e5e22905c 80 2,3,8,1,2,3,7,2,4,
matirc 0:909e5e22905c 81 4,8,8,4,4,2,4,4,8,
matirc 0:909e5e22905c 82 8,4,4,2,3,8,2,3,8,1,};
matirc 0:909e5e22905c 83
matirc 0:909e5e22905c 84
matirc 0:909e5e22905c 85
matirc 0:909e5e22905c 86
matirc 0:909e5e22905c 87
matirc 0:909e5e22905c 88
matirc 0:909e5e22905c 89 //here it comes the function for the counter and checker of the neighbours that will have each cell
matirc 0:909e5e22905c 90 //it checks everyone around the cell (i,j), and if the neighbour is alive the number of neighbours is one more (n++)
matirc 0:909e5e22905c 91 char neighbours(int i,int j){
matirc 0:909e5e22905c 92 char n=0;
matirc 0:909e5e22905c 93
matirc 0:909e5e22905c 94 if (lcd.getPixel(i-1,j-1)){
matirc 0:909e5e22905c 95 n++;}
matirc 0:909e5e22905c 96 if (lcd.getPixel(i-1,j)){
matirc 0:909e5e22905c 97 n++;}
matirc 0:909e5e22905c 98 if (lcd.getPixel(i-1,j+1)){
matirc 0:909e5e22905c 99 n++;}
matirc 0:909e5e22905c 100 if (lcd.getPixel(i,j-1)){
matirc 0:909e5e22905c 101 n++;}
matirc 0:909e5e22905c 102 if (lcd.getPixel(i,j+1)){
matirc 0:909e5e22905c 103 n++;}
matirc 0:909e5e22905c 104 if (lcd.getPixel(i+1,j-1)){
matirc 0:909e5e22905c 105 n++;}
matirc 0:909e5e22905c 106 if(lcd.getPixel(i+1,j)){
matirc 0:909e5e22905c 107 n++;}
matirc 0:909e5e22905c 108 if(lcd.getPixel(i+1,j+1)){
matirc 0:909e5e22905c 109 n++;}
matirc 0:909e5e22905c 110 return n;} //it finally returns the final value of the number of neighbours alive
matirc 0:909e5e22905c 111
matirc 0:909e5e22905c 112
matirc 0:909e5e22905c 113
matirc 0:909e5e22905c 114 /*this function implemets the display counter of cells.Considering the counter is done in the code, depending on if the number is between some hundreds the
matirc 0:909e5e22905c 115 display will show one number or other. Therefore, if the display shows 1 it means that there is between 100 and 200 cells alive. If the display shows
matirc 0:909e5e22905c 116 a 0 but with the decimal point on it means that 1000 cells are alive. The decimal point will indicates therefore that the number is higher or equals than
matirc 0:909e5e22905c 117 1000.For instance if the display is 1 with decimal point it means that 1100 cells are alive.If the display shows an E it means that there are more than
matirc 0:909e5e22905c 118 2000 cells alive (EXTREME).*/
matirc 0:909e5e22905c 119 void cell_counter(int cellcounter){
matirc 0:909e5e22905c 120 if(cellcounter>2000){
matirc 0:909e5e22905c 121 display=0x4F;}
matirc 0:909e5e22905c 122 else if(cellcounter>1900){
matirc 0:909e5e22905c 123 display=0xE7;}
matirc 0:909e5e22905c 124 else if(cellcounter>1800){
matirc 0:909e5e22905c 125 display=0xFF;}
matirc 0:909e5e22905c 126 else if(cellcounter>1700){
matirc 0:909e5e22905c 127 display=0x87;}
matirc 0:909e5e22905c 128 else if(cellcounter>1600){
matirc 0:909e5e22905c 129 display=0xFD;}
matirc 0:909e5e22905c 130 else if(cellcounter>1500){
matirc 0:909e5e22905c 131 display=0xED;}
matirc 0:909e5e22905c 132 else if(cellcounter>1400){
matirc 0:909e5e22905c 133 display=0xE6;}
matirc 0:909e5e22905c 134 else if(cellcounter>1300){
matirc 0:909e5e22905c 135 display=0xCF;}
matirc 0:909e5e22905c 136 else if(cellcounter>1200){
matirc 0:909e5e22905c 137 display=0xDB;}
matirc 0:909e5e22905c 138 else if(cellcounter>1100){
matirc 0:909e5e22905c 139 display=0x86;}
matirc 0:909e5e22905c 140 else if(cellcounter>1000){
matirc 0:909e5e22905c 141 display=0xBF;}
matirc 0:909e5e22905c 142 else if(cellcounter>900){
matirc 0:909e5e22905c 143 display=0x67;}
matirc 0:909e5e22905c 144 else if(cellcounter>800){
matirc 0:909e5e22905c 145 display=0x7F;}
matirc 0:909e5e22905c 146 else if(cellcounter>700){
matirc 0:909e5e22905c 147 display=0x07;}
matirc 0:909e5e22905c 148 else if(cellcounter>600){
matirc 0:909e5e22905c 149 display=0x7D;}
matirc 0:909e5e22905c 150 else if(cellcounter>500){
matirc 0:909e5e22905c 151 display=0x6D;}
matirc 0:909e5e22905c 152 else if(cellcounter>400){
matirc 0:909e5e22905c 153 display=0x66;}
matirc 0:909e5e22905c 154 else if(cellcounter>300){
matirc 0:909e5e22905c 155 display=0x4F; }
matirc 0:909e5e22905c 156 else if(cellcounter>200){
matirc 0:909e5e22905c 157 display=0x5B;}
matirc 0:909e5e22905c 158 else if(cellcounter>100){
matirc 0:909e5e22905c 159 display=0x06;}
matirc 0:909e5e22905c 160 else if(cellcounter>0){
matirc 0:909e5e22905c 161 display=0x3F;}}
matirc 0:909e5e22905c 162
matirc 0:909e5e22905c 163
matirc 0:909e5e22905c 164
matirc 0:909e5e22905c 165
matirc 0:909e5e22905c 166
matirc 0:909e5e22905c 167
matirc 0:909e5e22905c 168
matirc 0:909e5e22905c 169
matirc 0:909e5e22905c 170
matirc 0:909e5e22905c 171 //HERE I DEFINE THE MUSIC FUNCTIONS THAT I WILL IMPLEMENT IN THE MAIN CODE
matirc 0:909e5e22905c 172
matirc 0:909e5e22905c 173 //function for initializing the music array
matirc 0:909e5e22905c 174 void init_array()
matirc 0:909e5e22905c 175 {
matirc 0:909e5e22905c 176 // create LUT - loop through array and calculate sine wave samples
matirc 0:909e5e22905c 177 for (int i = 0; i < n ; i++) {
matirc 0:909e5e22905c 178 y[i] = 0.5 + 0.5*sin(i*2*PI/n);
matirc 0:909e5e22905c 179 }
matirc 0:909e5e22905c 180
matirc 0:909e5e22905c 181 }
matirc 0:909e5e22905c 182
matirc 0:909e5e22905c 183 //function for the tone and note duration of the music application.It synchronize both of them.
matirc 0:909e5e22905c 184 void tone(float frequency,float duration){
matirc 0:909e5e22905c 185
matirc 0:909e5e22905c 186 if (frequency > 0) { // a frequency of 0 indicates no note played so only play a note if frequency is not 0
matirc 0:909e5e22905c 187
matirc 0:909e5e22905c 188 float dt = 1.0/(frequency*n) - (1.34e-6 + 1e-6); // calculate time step - take into account DAC time and wait() offset
matirc 0:909e5e22905c 189
matirc 0:909e5e22905c 190 noteTimer.start(); // start timer
matirc 0:909e5e22905c 191
matirc 0:909e5e22905c 192 while(noteTimer.read() < duration) { // keep looping while timer less than duration
matirc 0:909e5e22905c 193
matirc 0:909e5e22905c 194 for (int i = 0; i < n ; i++) { // loop through samples and output analog waveform
matirc 0:909e5e22905c 195 aout = y[i];
matirc 0:909e5e22905c 196 wait(dt); // leave appropriate delay for frequency
matirc 0:909e5e22905c 197 }
matirc 0:909e5e22905c 198 }
matirc 0:909e5e22905c 199
matirc 0:909e5e22905c 200 noteTimer.stop(); // stop the timer
matirc 0:909e5e22905c 201 noteTimer.reset(); // reset to 0
matirc 0:909e5e22905c 202
matirc 0:909e5e22905c 203 } else { // if no note played, have a simple delay
matirc 0:909e5e22905c 204 wait(duration);
matirc 0:909e5e22905c 205 }
matirc 0:909e5e22905c 206 }
matirc 0:909e5e22905c 207
matirc 0:909e5e22905c 208
matirc 0:909e5e22905c 209
matirc 0:909e5e22905c 210
matirc 0:909e5e22905c 211
matirc 0:909e5e22905c 212
matirc 0:909e5e22905c 213
matirc 0:909e5e22905c 214 //HERE STARTS THE MAIN FUNCTION WITH ITS CODEa
matirc 0:909e5e22905c 215
matirc 0:909e5e22905c 216 int main(){
matirc 0:909e5e22905c 217
matirc 0:909e5e22905c 218 init_array(); // fill array with sine samples, initialize the music array
matirc 0:909e5e22905c 219
matirc 0:909e5e22905c 220 // calculate number of notes in array
matirc 0:909e5e22905c 221 //sizeof() returns number of BYTES in array,
matirc 0:909e5e22905c 222 // so divide by size of a float in BYTES to get number of elements
matirc 0:909e5e22905c 223 int notes = sizeof(note_array)/sizeof(float);//set the notes as an integer
matirc 0:909e5e22905c 224
matirc 0:909e5e22905c 225 lcd.init();//initialize the nokia screen
matirc 0:909e5e22905c 226 lcd.inverseMode();//set the nokia screen to an inversmode
matirc 0:909e5e22905c 227 //displays a welcome and shows my name in the screen for five seconds
matirc 0:909e5e22905c 228 lcd.printString("Welcome to",5,1);
matirc 0:909e5e22905c 229 lcd.printString("Game of Life",10,2);
matirc 0:909e5e22905c 230 lcd.printString("Enjoy it :)",5,3);
matirc 0:909e5e22905c 231 lcd.drawRect(0,5,84,0.5,1);
matirc 0:909e5e22905c 232 lcd.drawRect(0,33,84,0.5,1);
matirc 0:909e5e22905c 233 lcd.printString("MateoRandulfe",3,5);
matirc 0:909e5e22905c 234
matirc 0:909e5e22905c 235 lcd.refresh();
matirc 0:909e5e22905c 236 wait(5);
matirc 0:909e5e22905c 237
matirc 0:909e5e22905c 238 /*Shows in the screen the instructions for starting the game whenever the player wants. Meanwhile he does not press the button
matirc 0:909e5e22905c 239 for starting , the music with the LEDs pattern that I created will be playing and displaying.*/
matirc 0:909e5e22905c 240
matirc 0:909e5e22905c 241 lcd.clear();
matirc 0:909e5e22905c 242 lcd.printString("Press the",5,1);
matirc 0:909e5e22905c 243 lcd.printString("red button",5,2);
matirc 0:909e5e22905c 244 lcd.printString("for starting",5,3);
matirc 0:909e5e22905c 245 lcd.printString("Game of Life",5,4);
matirc 0:909e5e22905c 246 lcd.refresh();
matirc 0:909e5e22905c 247
matirc 0:909e5e22905c 248
matirc 0:909e5e22905c 249
matirc 0:909e5e22905c 250
matirc 0:909e5e22905c 251
matirc 0:909e5e22905c 252
matirc 0:909e5e22905c 253
matirc 0:909e5e22905c 254 while(startgame==0){//while the star game button is not pressed execute the music and LEDs feedback
matirc 0:909e5e22905c 255
matirc 0:909e5e22905c 256
matirc 0:909e5e22905c 257
matirc 0:909e5e22905c 258 // loop through notes
matirc 0:909e5e22905c 259 for(int i=0; i < notes; i++) {
matirc 0:909e5e22905c 260
matirc 0:909e5e22905c 261 //whenever the satar button will be pressed, the music will stop and the game will start
matirc 0:909e5e22905c 262 if(startgame==1){
matirc 0:909e5e22905c 263 break;}
matirc 0:909e5e22905c 264
matirc 0:909e5e22905c 265 //keep going with the music function
matirc 0:909e5e22905c 266 // equalizer on LEDs
matirc 0:909e5e22905c 267 leds=leds_values[i];
matirc 0:909e5e22905c 268 // play note
matirc 0:909e5e22905c 269 tone(note_array[i],60.0/(BPM*note_duration[i]));
matirc 0:909e5e22905c 270 // leave a short pause between notes
matirc 0:909e5e22905c 271
matirc 0:909e5e22905c 272 wait(60.0/(BPM*note_duration[i]));
matirc 0:909e5e22905c 273 }
matirc 0:909e5e22905c 274 }
matirc 0:909e5e22905c 275 lcd.normalMode();//set nokia screen to normal mode again
matirc 0:909e5e22905c 276
matirc 0:909e5e22905c 277
matirc 0:909e5e22905c 278
matirc 0:909e5e22905c 279
matirc 0:909e5e22905c 280
matirc 0:909e5e22905c 281 while(1){//infinite loop
matirc 0:909e5e22905c 282
matirc 0:909e5e22905c 283
matirc 0:909e5e22905c 284
matirc 0:909e5e22905c 285
matirc 0:909e5e22905c 286
matirc 0:909e5e22905c 287
matirc 0:909e5e22905c 288 srand( time( NULL ) );//this makes the random function to be different each time it is applyied
matirc 0:909e5e22905c 289
matirc 0:909e5e22905c 290 //this two for loops will be really important along the code because they allow me to check every cell of the screen and to implement to every cell whatever I want
matirc 0:909e5e22905c 291 for(i=0;i<84;i++){
matirc 0:909e5e22905c 292 for(j=0;j<48;j++){
matirc 0:909e5e22905c 293
matirc 0:909e5e22905c 294 step[i][j]= rand()%2;//implement a random function that can be or 1 or 0 in the "x" cell of the screen
matirc 0:909e5e22905c 295 if(step[i][j]==1){//if the point (i,j) of the step array is equals to one, set that pixel on(alive)
matirc 0:909e5e22905c 296 lcd.setPixel(i,j);}
matirc 0:909e5e22905c 297 else{//if it is a 0 in the array, set the pixel off(dead)
matirc 0:909e5e22905c 298 lcd.clearPixel(i,j);}
matirc 0:909e5e22905c 299 }}
matirc 0:909e5e22905c 300 lcd.refresh();//refresh screen
matirc 0:909e5e22905c 301 wait(3);//wait 3 seconds untill the actual game starts in order to be able to check that the random function was implemented properly
matirc 0:909e5e22905c 302
matirc 0:909e5e22905c 303
matirc 0:909e5e22905c 304
matirc 0:909e5e22905c 305
matirc 0:909e5e22905c 306
matirc 0:909e5e22905c 307
matirc 0:909e5e22905c 308
matirc 0:909e5e22905c 309 /*while the reset button is not pressed,perform the game of life.If it is pressed it will finish the code and therefore,it will go the code flow
matirc 0:909e5e22905c 310 to the original infinite loop,starting agalin the random seed so reseting the game*/
matirc 0:909e5e22905c 311 while (resetbutton==0){
matirc 0:909e5e22905c 312 if(stopbutton==0){
matirc 0:909e5e22905c 313 //while stop button is not pressed,game of life will be performing.If it is pressed it will stop the flow of the game of life and then when it will be realised again it will keep flowing
matirc 0:909e5e22905c 314
matirc 0:909e5e22905c 315
matirc 0:909e5e22905c 316
matirc 0:909e5e22905c 317
matirc 0:909e5e22905c 318
matirc 0:909e5e22905c 319
matirc 0:909e5e22905c 320 //apply to all the cells of the screen:
matirc 0:909e5e22905c 321 for ( i=0;i<84;i++){
matirc 0:909e5e22905c 322 for( j=0;j<48;j++){
matirc 0:909e5e22905c 323 //apply the neighbours counter function and stores each value for each cell during each loop in "n" variable
matirc 0:909e5e22905c 324 int n=neighbours(i,j);
matirc 0:909e5e22905c 325 //HERE I APPLY THE GAME OF LIFE RULES
matirc 0:909e5e22905c 326 //I set as a one or a 0 in the next array in order not to affect the change flow to the upcoming cell checks.It stores the values obtained from the evaluation of the array step
matirc 0:909e5e22905c 327 //(original one) in the nextstep array as 0s or 1s
matirc 0:909e5e22905c 328 if(n>3){
matirc 0:909e5e22905c 329 nextstep[i][j]=0;}
matirc 0:909e5e22905c 330 if(n==3){
matirc 0:909e5e22905c 331 nextstep[i][j]=1;}
matirc 0:909e5e22905c 332 if(n==2){
matirc 0:909e5e22905c 333 if(lcd.getPixel(i,j)){
matirc 0:909e5e22905c 334 nextstep[i][j]=1;}
matirc 0:909e5e22905c 335 else{
matirc 0:909e5e22905c 336 nextstep[i][j]=0;}}
matirc 0:909e5e22905c 337 if(n<2){
matirc 0:909e5e22905c 338 nextstep[i][j]=0;}}}
matirc 0:909e5e22905c 339
matirc 0:909e5e22905c 340
matirc 0:909e5e22905c 341
matirc 0:909e5e22905c 342
matirc 0:909e5e22905c 343
matirc 0:909e5e22905c 344 cellcounter=0;//set the counter of cells alive to 0
matirc 0:909e5e22905c 345 //aplying the following for each cell of the screen:
matirc 0:909e5e22905c 346 for(i=0;i<84;i++){
matirc 0:909e5e22905c 347 for(j=0;j<48;j++){
matirc 0:909e5e22905c 348 //Here I translate the 0s and 1s of the arrays into pixels on(1s) or off(0s) and if the pixel is on for "x" cell of the screen the cell counter will add one more.
matirc 0:909e5e22905c 349 if(nextstep[i][j]==1){
matirc 0:909e5e22905c 350 lcd.setPixel(i,j);
matirc 0:909e5e22905c 351 cellcounter++;}
matirc 0:909e5e22905c 352 else{
matirc 0:909e5e22905c 353 lcd.clearPixel(i,j);}
matirc 0:909e5e22905c 354 //I finally, in order to keep the flow of the game of life, change the next array to be the original one so that the array obtained after evaluating and apppying the rules of life to the original
matirc 0:909e5e22905c 355 //array will be now that original array after checking and changing every cell of the original array.
matirc 0:909e5e22905c 356 step[i][j]=nextstep[i][j];
matirc 0:909e5e22905c 357
matirc 0:909e5e22905c 358 }}
matirc 0:909e5e22905c 359 lcd.refresh();
matirc 0:909e5e22905c 360
matirc 0:909e5e22905c 361
matirc 0:909e5e22905c 362
matirc 0:909e5e22905c 363
matirc 0:909e5e22905c 364
matirc 0:909e5e22905c 365
matirc 0:909e5e22905c 366 //here, with this wait I implement the velocity of the game flow that will be controller with the potentiometer
matirc 0:909e5e22905c 367 wait(speed*2);
matirc 0:909e5e22905c 368 //I also set the controller with the potentiometer of the LEDs brightness of the nokia screen
matirc 0:909e5e22905c 369 lcd.setBrightness(brightness*0.5);
matirc 0:909e5e22905c 370 //I finally apply the display counter of cells calling the function already explained
matirc 0:909e5e22905c 371 cell_counter(cellcounter);
matirc 0:909e5e22905c 372 }}}
matirc 0:909e5e22905c 373 }