Arduino Asteroids Game: Start Screen!

Alright! Now that we’ve defined our game, finished the storyboard, and decided what it is we want to do with our game, it’s time to get started. Of course, the start screen is probably the best place to do that! So, I’ve completed game state 0, the start screen.

Not a lot you can do here, but it does flash every second between “Select = Start” and “Right = Info”. If you push one of those buttons, the game state changes to the appropriate number. I had to use this flashing option because the display is actually only 16×2, and I originally thought it was 18×2. Essentially the screen was too short for the information I wanted to present. However, the flashing options is nice because it shows that the display is active.

Makes me want to play it!

You can check out the latest commit on my GitLab, and here is game state 0:

if (gameState == 0) { // start screen state.
// The initial start screen. Let’s set all game variables for a new game.
// Game variables:
gameState = 0; // The state of game play.
gameBullets = 9; // Number of bullets remaining.
gameSpeed = 1; // The speed of the game.
gameScore = 0; // Number of asteroids destroyed or past.
gamePosition = 0; // Your space ships location, 0 for up, 1 for down.
gameAsteroidOne = 14; // First asteroid starting space.
gameAsteroidTwo = 25; // Second asteroid starting space.
gameAsteroidThree = 17; // Third asteroid starting space.
gameAsteroidFour = 23; // Fourth Asteroid starting space.

lcd.setCursor(0,1); // move to the begining of the second line
if (gameLineNum != 0){
lcd.print(” Right = Info “);
gameLineNum = 0;
} else {
lcd.print(” Select = Start “);
gameLineNum = 1;
}

lcd_key = read_LCD_buttons(); // read the buttons

switch (lcd_key){// depending on which button was pushed, we perform an action

case btnRIGHT:{
gameState = 1;
break;
}
case btnSELECT:{
gameState = 3;
break;
}
}
// Add a delay in the loop.
delay(1000);
} // End gameState 0, asteroids start screen.

As you can see, the most important thing game state 0 does is set all the variables back to the default.

Linux – keep it simple.

Advertisements

Arduino Asteroids Game!

The humble beginnings of a simple asteroid avoidance game.

Okay, so I said that I would try making a little game with this LCD keypad, and I really meant it! I give you: “Asteroids Game!”

Well, at least the start of it. As you can see from the pictures, I’ve only really done two things: Storyboard and named the game. Making a storyboard is a very important part of creating a good game. As you can see in my hand written notes, that this entire game’s storyboard can be contained on one page (only because it is so simple).

The key part of storyboard work is showing how the screens will interface with each other, as you move from one game state to another. Consider our simple asteroid avoidance game, we will need 7 game states or screens:

/*
* Game states:
* 0 = Asteroid start screen
* 1 = Credits
* 2 = How to info
* 3 = Play mode
* 4 = Pause mode
* 5 = win screen
* 6 = lose screen
*/

The storyboard also sets the game dynamics, goals, and back story. Essentially, you pilot a fighter space craft and must drive through some sort of asteroid field. You must get past 100 asteroids to get to the other side. You will apparently have a limited amount of ammo with which you may destroy asteroids, the rest you have to move to avoid. Also, it will get faster and faster as you go through the field of asteroids.

Sounds pretty epic on an 18×2 screen!

The general controls are also defined in this storyboard: during menus or information, you can scroll and also start the game or go back to other menus or information. During the game play, you can move your ship up or down, shoot, pause, and quit to the main menu. Also I submitted the layout for the game play:

     ***        *          *         *************

– Bullets  END  Space  Field of asteroids coming at you……….

– Score    END   ship    Field of asteroids coming at you……….

It’s  a little hard to show on this diagram, but the storyboard picture makes it more clear. The first row will comprise of three digits for available bullets, the “end” of the play field, the upper space where your ship can be, and then 13 spaces for asteroids to hurl towards you.

The second row will be three digits to keep track of your score (the asteroids past or destroyed), the “end” of the play field, the lower space where your ship can be, and then 13 spaces for asteroids to hurl towards you.

I hope you enjoy this little adventure into gaming and Arduino programming. I’m looking forward to it myself. You can check out my work on my Gitlab as we go along, and this work falls under an open source license, so feel free to learn from it and make even better games/use out of it!

Linux – keep it simple.

LCD Keypad Shield for Arduino

I’m not entirely new to the Arduino scene, but I’m certainly not an expert. With that, I’ve decided to pick up a few “toys”, such as shields and attachments for the Arduino with the hopes of using them to learn more about this handy little tool. One of those toys was an LCD keypad shield.

Essentially, it is an LCD display with 6 buttons. One of the buttons is the reset button for the Arduino, and the other 5 are labeled and arranged as up/down/left/right and select. As usual, before making any of my own projects with a new piece of equipment, I like to follow through someone else’s tutorial and see how everything works, and give the hardware a positive test.

That said, I followed the tutorial from dfrobot. It was very informative and worked like a charm. Essentially, when you push a button, it displays the button name on the LCD screen, giving a great hardware test of both the screen and buttons, but also showing how the shield works.

A couple of small notes for the uninformed, like myself, when I first powered it on, the LCD backlighting was bright, but nothing displayed. The potentiometer on the top left of the board is labeled as “brightness” on the diagram I saw, causing me to think it controlled backlight brightness. Rather, it controls the intensity of the LCD itself, and was turned all the way down, not allowing the letters to appear. A quick couple of turns with a screwdriver made short work of this problem.

I’m going to make a small “avoid the asteroid” arcade style game on this shield for the learning value. It should be a lot of fun! I’ll keep you posted!

Linux – keep it simple.

Drawing variable circles with an Arduino Uno and a Nokia 5110 LCD

I’m still trying to catch up on some of my new toys that I bought/got around Christmas time. I know, I’m four months behind! One of those gadgets was a Nokia 5110 LCD screen, and this thing is fun! It looks like something they used to use in old Nokia cell phones, and it works great for a monochrome, low resolution display.

I’m driving it with an Arduino Uno board. At first, to make sure I understood how to use it, I watched InterlinkKnight’s Youtube tutorial, which you also should check out. It was very clear, highly informative, and really well put together.

After watching the tutorial, and loading the library and his sketches, I then set out to make my own. I noticed a couple of things that were noteworthy for the new user, so I thought I’d share them here.

First, you need the library. You can get it here: https://github.com/olikraus/U8glib_Arduino

Just unzip the library, and put it in your Arduino library folder (unzipped, of course). For me, on my Ubuntu Linux machine, that was in /home/alaskalinuxuser/Arduino/libraries/, where I just dropped off the unzipped folder from U8. If you don’t put this in your Arduino libraries, it can’t find the referenced include file, and this wont work.

Then I made my own sketch:

/*
This is an example on how to use the display known as Nokia 5110 SPI with PCD8544 driver using the U8GLIB library.

Nokia 5110 SPI display pins for Arduino Uno/Nano:
* RST = 8
* CE = 10
* DC = 9
* DIN = 11
* CLK = 13
* VCC = 5V
* BL = 3.3V
* GND = GROUND

We use this library: https://github.com/olikraus/U8glib_Arduino

User Reference Manual: https://github.com/olikraus/u8glib/wiki/userreference

List of fonts: https://github.com/olikraus/u8glib/wiki/fontsize

Sketch Originally made by: InterlinkKnight
This sketch was featured on his video tutorial for this display: https://www.youtube.com/watch?v=1ZvY_lb6BoU
Last modification: 11/22/2017

Sketch modified by alaskalinuxuser
Last modification: 04/02/2019
*/

#include “U8glib.h” // Include U8glib library
// Create display and set pins:
U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8); // (CLK=13, DIN=11, CE=10, DC=9, RST=8)

int Variable1; // Create a variable to have something dynamic to show on the display
int fadeAmount = 1; // how many points to change by

void setup(void) // Start of setup
{

// Select a font:
u8g.setFont(u8g_font_profont12);

// Change display orientation:
//u8g.setRot90();
u8g.setRot180();
//u8g.setRot270();

} // End of setup

void loop(void) // Start of loop
{
// wait for 25 milliseconds to see the dimming effect
delay(25);

// change the brightness for next time through the loop:
Variable1 = Variable1 + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (Variable1 <= 0 || Variable1 >= 45) {
fadeAmount = -fadeAmount;
}

u8g.firstPage(); // Beginning of the picture loop
do // Include all you want to show on the display:
{
u8g.drawCircle(41, 21, Variable1, U8G_DRAW_ALL); // Draw a circle (x,y,radius,option)

u8g.drawRFrame(59, 10, 25, 13, 3); // Draw a rounded square (x,y,width,height,radius)

// Variable with left text alignment:
u8g.setPrintPos(63, 20); // (x,y)
u8g.print(Variable1); // Value to print
} while(u8g.nextPage()); // End of the picture loop

} // End of loop

The key part of what I was playing with, was using a variable to set the circle size. Every run of the loop, after a slight delay, the circle is increased or decreased in size to make it grow or shrink on the screen. This makes a very nice “pulse” effect. Most of the code here was originally in InterlinkKnight’s sketch, I just modified it for my use, and added a variable changer.

Another thing I noticed was the back lighting. On my Nokia 5110, it is labeled as “BL” which means you apply voltage to it to illuminate the back lighting of the screen. In his tutorial, he rightly mentioned that you could use 5 volts here, which works great, but I found it to be obnoxiously bright. Instead, I used the 3.3 volt output from the Uno, and that was much better on the eyes. The screen will work without the back lighting, but then you need ample light to view it.

I’m seeing some fun potential with this screen!

Linux – keep it simple.

Android meets Arduino!

As you guys know, I don’t really do “product reviews”. But if I find a handy tool, and use it, then it is fun to share that info with others. One such tool is Bluino Loader, an Android app that lets you create Arduino files, upload them to your boards, and even monitor the serial connection over USB. It’s pretty handy!

As with any “code from your phone app”, it is a bit tedious to type a lot of code with the Android keyboard. So I don’t use this app a lot when it comes time to create code. One of the big things that I like to do with it, though, is to monitor the serial output over a USB OTG cable.

Using the OTG cable, I can hook the Arduino Uno board directly to the phone, which also powers the board. Then I can open up Bluino Loader and click the icon at the top to monitor the serial connection. It can’t get much simpler than that. Within seconds I have serial data coming in and being displayed in the terminal. I even used it to test out the 433 MHz transmit and receive functions from my last test project. Worked like a charm!

Linux – keep it simple.

Radio check sat! Using 433MHz transmitter and receiver with Uno board….

transmitAndReceive

Now that picoEngine has reached version 1.0, I’m ready to work on a few other projects. Namely some electronic ones. Since Christmas, I’ve been holding on to a few Arduino Uno boards, and a pile of stuff for them. So, I broke out a few parts.

IMG_20190326_132946

I decided to start with the 433MHz transmitter and receiver.

There were several guides online, and I stole some code from them. I like starting any project with a known good source before stepping out into something I made, to make sure the hardware/software is set up properly. It was a great tutorial, and it worked.

However, there was one unexpected drawback: range. I knew these little transmitters and receivers were weak, but I didn’t realize that they literally only transmit an inch! As you can see in the picture, the Uno boards are almost touching just to transmit the data back and forth. I had purchased a few of these (three sets for myself, and two for my brother) with the intention of doing a few fun projects. Unfortunately, these wont transmit across my desk, let alone the parking lot, so I’ll need something else to work with.

But, these are still a fun learning tool, just a little lacking in the range department! Here are the sketches I used, keep in mind, you need the “radiohead library” installed in your IDE to make this work.

Transmitter:

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

RH_ASK driver;

void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println(“init failed”);
}

void loop()
{
const char *msg = “Hi World!”;
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(1000);
}

Receiver:

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;

void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println(“init failed”);
}

void loop()
{
uint8_t buf[12];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
Serial.print(“Message: “);
Serial.println((char*)buf);
}
}

I certainly like the simple code, since the radio head library does all the heavy lifting for you. Have any of you ever tried these out?

Linux – keep it simple.

Finished, picoEngine?

picoEngineComplet

Today I released version 1.0 of picoEngine.

That’s right! My UCI compatible chess engine is now released as picoEngine version 1.0! You can view the tag on my GitLab. Now you too can download and compile picoEngine for yourself! It comes with a generic 64 bit version that I tested on Ubuntu 18.04 with both Arena and PyChess interfaces. Here’s the information on this release:

Version 1.0

Features:

  • UCI compatible engine!
  • Accepts both FEN and MovePos standard inputs.
  • Up to 8 ply weighted moves.
  • Has an optional random move mode for easy chess players.
  • Fast, even at 8 ply only takes a second on a modern 64 bit machine.
  • Light weight, it only takes 2 MB of ram to run this engine.
  • Small size, the (Linux 64 bit) compiled engine is only 364.2 KB in size.
  • Small source code, the entire source repository is less than 1.3 MB in size.
  • Can output moves and engine evaluation (only displayed by interfaces that support it).

I hope you enjoy it! If the prebuilt doesn’t work, just review the read me for simple compiling instructions, involving just one command. (I like to keep things simple.)

Linux – keep it simple.

picoEngine: Fixing FEN + Moves!

fenplusmoves

Today I was able to finally put the nail in the coffin of the FEN + Moves bug in picoEngine! It felt great to close that long standing issue. You can even check out the commit for all the details, but as usual, I’ll talk about the pertinent part here.

The reason this is important is because many chess programs allow you to set up a position on the board, and then seek the engine’s advice on a next best move. Initially, while meeting the UCI (Universal Chess Interface) standard, I enabled picoEngine to accept either “movepos” ( a move by move insertion from the program to my chess engine ), or “FEN” ( a string of numbers and letters that represents the current board ). What I didn’t realize is that some chess programs did not do what I expected per the UCI guidelines.

The way I understood it by reading the UCI guidelines, was that the controlling chess program (say Arena, in our case) would choose before hand to either send “movepos” with a list of moves to get the board set up, or it would send “FEN” with a string of the board setup for each move. I understood that if you set up the board in an unusual position, it would need to send FEN commands to my engine. That made sense.

What I thought would happen for further moves, however, did not. I thought, since it started as a FEN command game, then every turn, it would send the FEN string of the new board. Some chess programs do this. In the case of many programs, such as Arena, it did something quite unexpected to me. For further moves, it would send the FEN to set the board, then a string of the moves after that setup. So it was doing both movepos and FEN all at the same time!

Knowing that now was an easy fix. Simply read the FEN to get the current position, and if you have follow on moves, then apply the movepos rules on them. What gave me the biggest hiccup, however, was a slight miscalculation on my part. I was feeding the FEN into the engine in such a way that the board came out mirrored! Here was the code I used to fix it:

From this:

	int j = stringBoard.length();	
        for (int i = 0; i < j; i++) {
	if (i < 64){ m_theBoard[63 - i] = stringBoard.at(i); }
	}

To this:

 

        for (int i = 0; i < 8; i++) {
		m_theBoard[i+0] = stringBoard.at(i+56);
		m_theBoard[i+8] = stringBoard.at(i+48);
		m_theBoard[i+16] = stringBoard.at(i+40);
		m_theBoard[i+24] = stringBoard.at(i+32);
		m_theBoard[i+32] = stringBoard.at(i+24);
		m_theBoard[i+40] = stringBoard.at(i+16);
		m_theBoard[i+48] = stringBoard.at(i+8);
		m_theBoard[i+56] = stringBoard.at(i+0);
	}

The picoEngine is really coming along nicely! I think it is playable now, and I’m just about done working on it. It’s still not very bright yet, but it is getting better!

Linux – keep it simple.

Choice by chance, picoEngine?

choiceply2

Today I added another set of options to picoEngine. Since it can now think through up to 8 ply, I needed to give it an option to choose how many ply you want it to think through. The commit is simple enough, which you can check out, but here is the gist of it:

	cout << "option name Ply type combo default 8 var 1 var 2 var 3 var 4 var 5 var 6 var 7 var 8" << endl;
	// End of Options
	cout << "uciok" << endl;
}
                styleRandom = false;
                cout << "Normal Mode." << endl;
            }
    if (std::string::npos != setString.find("1"))
            {
                chosenPly = 1;
                cout << "Ply 1." << endl;
            }

With further blocks for 1 through 8. I could have used case, but this worked well enough. Now just to make it smarter….

Linux – keep it simple.

Think deep, picoEngine!

8Ply

Okay, so I’ve been going on and on about making picoEngine think through multiple ply, and it really has been a challenge. I think this latest commit finally fixed it with the ability to think through up to 8 ply. That might seem like a lot, but the grandmaster level chess player is thinking through 16 ply.

A ply, for those who are wondering, is a move, so 8 ply is 4 complete turns of 8 moves. I’m certainly no grandmaster, but I’ve been beating this thing at 8 ply like it’s nobody’s business, so, despite thinking deep, it is not thinking well.

Either way, be sure to check out the commit, as I completely rewrote the ply thought process today. Here is some highlights with notes:

string makeMoves (string proposedMove, string boardPositions){
	string boardReturned = boardPositions;
	try{
		int first =((proposedMove.at(0) - 'a' + 1) + (((proposedMove.at(1) - '1') * 8) - 1));
		int second =((proposedMove.at(2) - 'a' + 1) + (((proposedMove.at(3) - '1') * 8) - 1));
		boardPositions[second] = boardPositions[first];
		boardPositions[first] = '-';
		boardReturned = boardPositions;
	} catch (...) {
	cout << " Exception " << endl;
	} // End try/catch block
	return boardReturned;
}

The above code allows the engine to use the board and make a move with it.

playBoard = makeMoves(proposedMove, originalBoard);
				currentEval = evaluations.getEval(playBoard);
				cout << " current eval and ply " << currentEval << " " << plyMoves << endl;
				if (whoseTurn) {
					if (currentEval > 9000) {cout << " ply  ends 10000." << endl;
					chosenMove = proposedMove;
					return chosenMove;
					}} else {
						if (currentEval < -9000) {cout << " ply  ends -10000." << endl;
					chosenMove = proposedMove;
					return chosenMove;
					}} 
				if (plyMoves-1 < 1){ 
						cout << " ply  ends 1." << endl;
					if (whoseTurn){
						// White's turn, higher is better.
						if ( currentEval > bestEval ) {
							chosenMove = proposedMove; 
							bestEval = currentEval;
							cout <<"info depth " << plyMoves << " score cp "<< currentEval << " pv " << chosenMove  << endl; //<< " comment " << boardPositions 
							} else if ( currentEval == bestEval && (int)time(0)%2 == 0) {
							chosenMove = proposedMove; 
							bestEval = currentEval;
							cout <<"info depth " << plyMoves << " score cp "<< currentEval << " pv " << chosenMove  << endl; //<< " comment " << boardPositions 
						}
						}

Here we see the general concept. We look at each available move, we determine the evaluation of that board after the move, we check if that is better or worse than what we had previously. With the weighted system, the “best” move wins.

The shortcomings I’ve seen while playing against picoEngine is probably related to poor evaluation criteria. It can only gauge the moves based on two things right now: movement and material. Hopefully I can get it smarter.

I did test it out, and it will checkmate itself if you have it play against itself in Arena. So that is good, it knows *how* to win, even if it is not very good at it.

Linux – keep it simple.