Johan_Ha
Published © MIT

Conway's Game of Life

Create life on a touch screen.

BeginnerFull instructions provided1 hour58
Conway's Game of Life

Things used in this project

Hardware components

Adafruit 2.8" TFT Touch Shield for Arduino with Capacitive Touch
×1
XMC4200 Platform2Go
Infineon XMC4200 Platform2Go
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

conway.ino

C/C++
Create a new sketch in Arduino IDE. Copy-paste this code into the main file.
/***************************************************
  This is our touchscreen painting example for the Adafruit ILI9341
  captouch shield
  ----> http://www.adafruit.com/products/1947

  Check out the links above for our tutorials and wiring diagrams

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

/**************************
Modified by Johan Halmén
for Conway's Game of Life
***************************/


#include <Adafruit_GFX.h>    // Core graphics library
#include <SPI.h>       // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h>      // this is needed for FT6206
#include <Adafruit_FT6206.h>

// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();

// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// Two fields for cells. f1 is the active, visible one. f2 is the next generation
// being built according to the rules. When it is ready, it is copied to f1.
bool f1[32][24];
bool f2[32][24];

// Time stamp for last generation. Next to be created 500 ms later.
uint32_t last_gen;

void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10);     // pause the serial port
 
  Serial.println(F("Cap Touch Paint!"));
  
  tft.begin(8000000);
  tft.setRotation(3);

  if (! ctp.begin(40, &Wire)) {  // pass in 'sensitivity' coefficient and I2C bus
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1) delay(10);
  }

  Serial.println("Capacitive touchscreen started");
  
  tft.fillScreen(ILI9341_BLACK);
  for (int i = 0; i < 32; i++)
    for (int j = 0; j < 24; j++)
    {
      // Uncomment the next three lines, if you want a random 1st generation.
      // If not, you get a blank screen and the fun begins at your fingertip.

      // if ((rand() & 0xffff) < 16000)
      //   f1[i][j] = true;
      // else
        f1[i][j] = false;
  
    }

  last_gen = millis();
}

void loop() {

  // Check for a touch
  while (ctp.touched()) 
  {
    int16_t xp, yp;
    // Retrieve a point  
    TS_Point p = ctp.getPoint();

    yp = (239 - p.x) / 10;
    xp = p.y / 10;
    f1[xp][yp] = true;
    tft.drawCircle(10*xp+5, 10*yp+5, 4, 0xffff);
    Serial.println("touched");
    Serial.println(xp);
    Serial.println(yp);
    last_gen = millis();
  }

  // Do the next generation, if it's time
  if (millis() - last_gen > 500)
  {
    //Serial.println("gen");
  
    last_gen = millis();
    for (int16_t i = 0; i < 32; i++)
      for (int16_t j = 0; j < 24; j++)
      {
        int16_t count, nx, ny;
        count = 0;

        // Count all living cells in a 3*3 grid around the actual cell
        // (we're counting the actual cell, too, for sake of simplicity)
        for (int16_t dx = -1; dx < 2; dx++)
          for (int16_t dy = -1; dy < 2; dy++)
          {
            nx = i + dx;
            if (nx < 0) nx += 32;
            if (nx > 31) nx -= 32;
            ny = j + dy;
            if (ny < 0) ny += 24;
            if (ny > 23) ny -= 24;

            if (f1[nx][ny])
              count++;            
          }
        if (f1[i][j])
        {
          // A living cell dies if it has less than 2 
          // or more than 3 neighbours
          if (count < 3 || count > 4)
            f2[i][j] = false;
          else
            f2[i][j] = true;

        }
        else
        {
          // A cell gets born in an empty spot, if there are
          // exactly three neighbours
          if (count == 3)
            f2[i][j] = true;
          else
            f2[i][j] = false;
        }

      }
    // f2 is now generated
    // Copy it to f1 and print it
    for (int i = 0; i < 32; i++)
      for (int j = 0; j < 24; j++)
      {
        f1[i][j] = f2[i][j];
        if (f1[i][j])
          tft.drawCircle(10*i+5, 10*j+5, 4, 0xffff);
        else
          tft.fillRect(10*i, 10*j, 10, 10, 0);
      }
  }
}

Credits

Johan_Ha
19 projects • 29 followers
Music teacher Composer Coding for fun
Thanks to Limor Fried/Ladyada.

Comments