当前位置:首页 > 科学研究 > 电子编程 > 正文内容

Arduino Project 022 - LED Dot Matrix Display - Pong Game

RonWang7个月前 (10-15)电子编程246

This project was hard going and a lot to take in. So, for Project 22 you are going to create a simple game with simple code using the dot matrix display and a potentiometer. This time you are going to use one of the many available libraries for controlling LED dot matrix displays to see how much easier it can make your life when coding.

Project 22 – LED Dot Matrix Display – Pong game

/* Coding Ron Wang
   Oct.15th 2024
   Autaba support for coding hardware
   Project 22 LED Dot Matrix – Pong Game 
 */
 
#include "LedControl.h"

LedControl myMatrix = LedControl(11, 12, 8, 1); // create an instance of a Matrix
/*
DIN connects to pin 11
CLK connects to pin 12
CS  connects to pin 8
*/
 
int column = 1, row = random(8)+1; // decide where the ball will start
int directionX = 1, directionY = 1; // make sure it heads from left to right first
int paddle1 = 5, paddle1Val; // Pot pin and value
int speed = 300;
int counter = 0, mult = 10;

void setup()
{
 myMatrix.shutdown(0, false); // enable display
 myMatrix.setIntensity(0, 8); // Set the brightness to medium
 myMatrix.clearDisplay(0); // clear the display
 randomSeed(analogRead(0));
}

void loop()
{
 paddle1Val = analogRead(paddle1);
 paddle1Val = map(paddle1Val, 200, 1024, 1,6);
 column += directionX;
 row += directionY;
 if (column == 6 && directionX == 1 && (paddle1Val == row || paddle1Val+1 == row || paddle1Val+2 == row)) {directionX = -1;}
 if (column == 0 && directionX == -1 ) {directionX = 1;}
 if (row == 7 && directionY == 1 ) {directionY = -1;}
 if (row == 0 && directionY == -1 ) {directionY = 1;}
 if (column == 7) { oops();}
 myMatrix.clearDisplay(0); // clear the screen for next animation frame
 myMatrix.setLed(0, column, row, HIGH);
 myMatrix.setLed(0, 7, paddle1Val, HIGH);
 myMatrix.setLed(0, 7, paddle1Val+1, HIGH);
 myMatrix.setLed(0, 7, paddle1Val+2, HIGH);
 if (!(counter % mult)) {speed -= 5; mult * mult;}
 delay(speed);
 counter++;
}

void oops() {
 for (int x=0; x<3; x++) {
 myMatrix.clearDisplay(0);
 delay(250);
 for (int y=0; y<8; y++) {
 myMatrix.setRow(0, y, 255);
 }
 delay(250);
 }
 counter=0; // reset all the values
 speed=300;
 column=1;
 row = random(8)+1; // choose a new starting location
}

Arduino Pong Game MAX7219 Circuit

Arduino Pong Game MAX7219 Schematic

版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://www.parentscn.com/?id=272

标签: Arduino

相关文章

Arduino Project 018 - Dual Shift Register 8-Bit Binary Counter

Arduino Project 018 - Dual Shift Register 8-Bit Binary Counter

In Project 18, you will daisy chain (or cascade) another 74HC595 IC onto the one used in Project 17...

Arduino Programming Basic - Funcation

Arduino Programming Basic - Funcation

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写了10个例子,关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静...

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

C语言调试运行环境TurboC的安装

C语言调试运行环境TurboC的安装

上大学时学习《C语言程序设计》(第二版)作者谭浩强,大部分编程时间是在学校机房度过,每次点开桌面的TurboC图标,就开始在里面敲代码,然后保存程序Abc.C,下一步进行编译,如果编译成功的话,就可以...

Arduino Project 002 - LED SOS Morse Code Singal

Arduino Project 002 - LED SOS Morse Code Singal

Arduino 电子编程--灯项目及控制,主要使用Arduino编程控制LED灯,实现基本控制Project 2 LED闪烁S.O.S信号。项目2 Project 2 S.O.S...

Arduino Project 001 - LED Blink

Arduino Project 001 - LED Blink

Arduino 电子编程--灯项目及控制,主要使用Arduino编程控制LED灯,实现基本控制Project 1 LED闪烁,基本的应用Project 3和4红绿灯项目项目1 Project...