Lesson 36 Snake Game

3038

Overview

In this lesson, we will make a Snake Game based on the Processing, and play the game with two buttons.


Components

- 1 * Arduino UNO

- 1 * USB Cable

- 2 * Button

- 1 * Breadboard

- Several jumper wires


Principle

The experiment consists of two parts: first, acquire the data from Arduino; second, process the data.

Play the Snake Game:

1.    When you press the right button, the snake will move to the right.

2.    Press the left button, and the snake will move to the left.

Note:

1. You need to install the Sound library.

2. In this experiment, my Arduino UNO board is connected to my computer port COM26. But it may differ in your case. So please adjust it according to your actual situation.

3. If the Processing does not run normally, you may need to install the related function libraries.


Procedures

Step 1: Build the circuit

a.png


Step 2: Program

_36_Button_Processing.ino

Processing_Code.rar

/***********************************************************
File name:   36_button_Processing.ino
Description: Arduino and processing interactive
             Button controls Snake Game
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2015/05/02 
***********************************************************/
int btnLPin = 2;//Define digital 2 pin connect the left button
int btnRPin = 3;//Define digital 3 pin connect the left button

int Lval = 0;
int Rval = 0;

void setup() { 
  // Start the serial port, baud rate to 9600
  Serial.begin(9600); 
  pinMode(btnLPin,INPUT_PULLUP);//Set btnLPin input and pull up module
  pinMode(btnRPin,INPUT_PULLUP);//Set btnRPin input and pull up module
} 
  
void loop() { 
  // Read the button information
  int Lval = digitalRead(btnLPin);
  int Rval = digitalRead(btnRPin);
  
  Serial.print(Lval);//Send Lval
  Serial.print(',');
  Serial.print(Rval);//Send Rval
  Serial.println(','); 
  delay(100);         //Delay 100ms
}



Step 3: Compile the program and upload to Arduino UNO board

Step 4: Run the Processing software (Snake_Game_Processing_Button.pde)

b.png

c.jpg