Project prep: Digital output on the Bluefruit feather!

img_20180110_0809261094712420.jpg

While I continue to prepare for my big project, I still have a few more things to test. Namely, I need to test the digital outputs to make sure that I understand how to use them. So, I wrote this little ditty to get the job done. Note that I just tacked this onto the end of my previous work.

#include <bluefruit.h>

int brightness = 0; // how bright the LED is
int fadeAmount = 10; // how many points to fade the LED by
int highOrNot = 1; // Just a number.

void setup() {
pinMode(19, OUTPUT); //19 is blue LED, 17 is red LED.
pinMode(11, OUTPUT); //19 is blue LED, 17 is red LED.
// initialize serial communication at 9600 bits per second:
Serial.begin(2400);
digitalWrite(11, HIGH);
}

void loop() {
analogWrite(19, brightness);

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

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 50) {
fadeAmount = -fadeAmount;
}
// wait for 500 milliseconds to see the dimming effect
delay(1000);
// To print the brightness // Serial.println(brightness);

// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 – 1023) to a voltage (0 – 3.6V):
float voltage = sensorValue * (3.6 / 1023.0);
// print out the value you read:
if (voltage >= 0.2) {
Serial.print(“Pin A01: “);Serial.println(voltage);
}

if (highOrNot > 0) {
digitalWrite(11, HIGH);
highOrNot = highOrNot -1;
} else {
digitalWrite(11, LOW);
highOrNot = highOrNot +2;
}
}

With the multi-meter hooked up to pins 11 and pin ground on the Bluefruit, I was able to see my program in action. So, during the loop of the program, it checks the number “highOrNot” (sounds like a bad drug joke). If the number is greater than 0, then make pin 11 a HIGH output, or in this case, 3.3 vdc. Also, if it is greater than 0, minus one from the number for the next loop.

As the loop runs again, the number will eventually drop to 0. When it does, add 2 to the number and make pin 11 LOW, or 0 vdc. This turns off the digital pin. Notice that I also increased the loop delay to every second, rather than 1/2 seconds. So, my multi-meter was dancing with 3.3 vdc for two seconds, then 0 for 1 second, on a continuous loop. Spiffy, huh?

Linux – keep it simple.

Advertisements

Fool’s Mate Friday: Chess books ancient and modern….

There are many ancient writings about chess and the various pre-chess games that obviously inspired chess itself. The first recorded European chess writing was a poem called ‘Versus de scachis’, in 997 AD. According to wikipedia, the first book to be completely about chess is the ‘Göttingen manuscript’, written in Latin sometime around 1471. There were several older works that either dealt with chess in a non-modern form, or were not entirely devoted to the play or strategy of the game itself.

This past Valentine’s Day, my lovely wife was kind enough to buy me a book as a gift! I think she knows me pretty well, because it was a chess book: ‘The Complete Book of Chess Strategy’ by IM Jeremy Silman.

cbocs

So far I really like the book. And since it was chess related, I thought I’d mention it here. It is very blunt and to the point. Every topic is covered, but in an almost “cliff notes” style, with only one or two pages per subject. The writer seems to hone in on just what you need to know about any given opening, maneuver, point, etc. He waste’s no time with repetition of each point, nor example after example.

He does, however, seem to have a bit of a sense of humor. It’s a great read thus far, and I look forward to reading all of the book, but I think it will last a long time thereafter as a reference guide as well. Here’s a snippet of his whit:

Zwischenzugs are important for the following reasons:

1) You’ll sound like a genius when you say, “I won by playing and unforeseen zwischenzug.”

If image is everything, huge, foreign, sophisticated, sexy words like this one will go a long way towards changing your personal from “chess nerd” to “that mysterious, super intelligent guy/girl who happens to play chess.”

Of course, he goes on to list “more important” reasons to use a zwischenzug…. I think this book might be very interesting to use to implement an opening book for my Just Chess Engine as well. We’ll see!

Linux – keep it simple.

Project prep: Testing analog input on my Bluefruit Feather!

I’m getting ready to use my Bluefruit Feather in a big project, well, big to me anyways. Before I do that, however, I needed to test a few things to make sure that I understand how it works. So, I added to my “cylon eye” sketch that I made last time, to include communication via serial (over USB) and taking an analog reading over pin A0. Here’s my sketch:

 

int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
analogWrite(LED_BUILTIN, brightness);

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

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 80) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(100);
// To print the brightness // Serial.println(brightness);

// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 – 1023) to a voltage (0 – 3.3V):
float voltage = sensorValue * (3.3 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}

I simply modified the Arduino sketch and added it to my already made cylon eye sketch. The user interface on this thing is incredibly easy! All I am doing here is setting up the USB to accept serial input at a baud rate of 9600 (plenty fast enough for the tiny bits of information I am transmitting). Then I take a reading off of pin A0, which comes off of the center tap of a 100k pot. The positive and ground are provided from the boards own 3.3 volt dc supply and ground pin, respectively.

Now I need to test if I can use this as a multi-meter. I grabbed a D cell battery and used my handy dandy alligator clips to hold everything in place. So on my potentiometer, I attached the left side to the negative terminal on the battery, and the right side to the positive side of the battery. The center tap of the pot still goes to the A0 pin, but now, I am not connected to the 3.3 volt dc supply on the board. However, to make this work, you need a common ground, so I still have the board ground hooked to the negative terminal of the battery. Cycling the pot revealed my results, spanning from 1.45 to 0 volts dc!

batt_multi

Try not to laugh too hard at my quick sketch, I just wanted to make sure that you understood what I was saying. Think of pin A0 as the red wire of your multi-meter, and pin ground as the black wire of your multi-meter.

So far, so good. I still need to test a few more things before I can move forward with my project. Perhaps in my next post I can outline the details of what I hope to accomplish.

Linux – keep it simple.

Fool’s Mate Friday: My Favorite FICS Commands

 

fics

Last week we talked about Jin and FICS a bit, but I wanted to make a post to break down some of my favorite FICS commands, which help show why I like to use it for playing online chess. Don’t worry, we’ll be getting back to our chess programming project soon!

  • messages

I like the messages command to send or view messages from other players. One of the things I like to do is ask players who beat me for some advice. Granted, I don’t always get quality advice, sometimes I actually get rather rude responses, but occaisionally I’ll get something useful from another player. I can send a message like so:

messages OtherPlayersName This is my message.

It’s that simple. To check my messages, I just type:

messages

That shows me the list of messages I have, and then I can say:

messages 1

To view message number 1. Pretty simple, I know, but I find this useful.

  • history

Typing the history command shows me all the games I recently played. Like this:

history
History for alaskalinuxuser:
Opponent Type ECO End Date
84: + 1147 W 1153 javikibera [ br 5 3] D00 Res Fri Jan 26, 12:32 AKST 2018
85: + 1393 B 1386 trapik [ sr 15 10] B51 Mat Fri Jan 26, 12:52 AKST 2018
86: – 1125 B 1475 Nedbio [ lr 2 0] B20 Mat Sat Jan 27, 05:30 AKST 2018
87: – 1390 W 1654 ChesstaLaVist [ sr 15 0] A02 Mat Sat Jan 27, 05:52 AKST 2018
88: – 1139 B 1141 CyberXess [ br 12 0] B23 Mat Sat Jan 27, 06:07 AKST 2018
89: – 1132 W 1214 GOROWITCHES [ br 12 0] D00 Res Sat Jan 27, 06:26 AKST 2018
90: – 1125 W 1174 Tordan [ br 5 0] D00 Mat Sat Jan 27, 20:16 AKST 2018
91: – 1118 B 1155 fridayknights [ br 3 0] C41 Fla Sat Jan 27, 20:29 AKST 2018
92: – 1108 W 1063 qrtroimaclic [ br 10 0] A02 Mat Sun Jan 28, 06:06 AKST 2018
93: + 1117 B 1132 Chessonator [ br 10 0] D00 Res Sun Jan 28, 07:36 AKST 2018

You can view past games with history, but only the last 10 games.

  • mailstored

I use mailstored with my history output to email myself my games. The email will include all the moves made during the game, the times, who won, etc. This way I can store and review all of my games in my own email. Use it like this:

mailstored alaskalinuxuser 93
Moves mailed.

Now I emailed all the details from game 93 to my email box, which I can keep forever.

  • finger

This command lets you review another players stats, like reviewing a player named “top”:

finger top
Finger of top:

Last disconnected: Sat Feb 21, 01:19 AKST 2004

rating RD win loss draw total best
Blitz 1908 350.0 122 101 18 241 1917 (06-Nov-1995)
Standard 2074 350.0 14 4 1 19

1: Torben Paving from Copenhagen, Denmark
2: Prefer 2 12 or slower
3: I don’t play computers!
4: I usually autoflag and don’t permit takebacks
5: I don’t request takebacks either!
6: formula time >= 2 & inc >= 12

But typically, I just type finger to review my own stats. Not because I am narcissistic, but because some games or tournaments will only allow you to play if you meet the rating criteria.

  • say

I use this command during a game to “chat” with my opponent. It’s a pretty easy command to use, like so:

say hi!

  • seek

This is the command I use alot when I want to play a game. I usually use this:

seek 15 0 rated

To offer a game of 15 minutes, with no added time per move (increment) and that the game will be rated. Now anyone can click on my game and play against me.

Well, those are the commands that I use the most. In particular, the mailstored command is really handy to store your games offline. Hope you found that interesting, and be sure to look me up on FICS sometime for a game!

Linux – keep it simple.

CompTIA stackable certifications!

CompTIA has come out with a new way to “stack” certifications together. Now you can combine certificates that are current to make several different “stackable” badges. I think the overall idea is to have a single badge to take the place of two or three certifications to simplify conveying your competency.

Previously, while working in the IT industry, you would have to say something like, “I am certified Net+ and Linux+”. But now you can just say that you hold a CLNP instead. At least, that seems to be the goal.

I’m not sure if it is really useful or not, as they invented nearly a dozen stackable certifications, there are about as many combinations as there are certifications.

It does sound cooler though! It kind of reminds me of power ups in old video games (maybe new games too, I just haven’t played many recently). Sort of like in Heavy Barrel, if you get enough pieces, then you can have the super gun!

Makes me wonder what’s at the top of the stackable chain?

Linux – keep it simple.

Fool’s Mate Friday: Jin, FICS, and Correspondence Chess

Apparently, the earliest documented correspondence chess game was played between Henry I of England and Louis VI of France, but the earliest correspondence chess game in which all of the moves were recorded took place sometime in 1804 between F. W. de Mauvillion of Breda and an officer at a city of Holland, The Hague. According to Harry Golombek in his book ”Golombek’s Encyclopedia of Chess”.

Since then, we’ve moved to more modern methods of distance play, especially since the Internet. Now you can play on your computer, your cell phone, via email, or even telephone.

That brings us to Jin, but not rummy. That’s right, I’m talking about Jin, the chess interface. Here’s what the author of this great program says about it:

Jin is an open source, cross platform, graphical client for chess servers, written in Java. Jin can run either as a standalone application (available at this website) or as an applet, directly from your browser (available at the chess server’s website).

I’ve been using Jin for quite a while now on my Linux based machine. Among the many great things about Jin is that it is open source and cross platform. There is not enough of these kinds of good programs going around.

One of the best features, of course, is the ability to hook up to several chess servers, including FICS, the Free Internet Chess Server. I play there regularly myself. Of course, I am a big fan of free, so the price is right, but I also like the ability you have to message other players (somewhat like email), chat with your opponent during the game, keep a journal of your past games, and even email your played games to yourself. Perhaps later I can do an article specific to FICS and some of the great features there.

For now, you are probably wondering if this has anything to do with my Just Chess Engine. Well, I hope to one day have it playable on chess servers (although I doubt it would garner a great score), but it is not there yet. It is, however, available for others to drag and drop into their Android or Java applications to be used as a weak starter engine. No, I want to focus today on correspondence.

My next goal with my chess programming is to stick with Android for now, and in particular, cell phones. I’d like to build an android app that allows you to play other people who are not in the same room. I plan to venture into several different methods of communication, which will help me learn a lot about programming. In all of these methods, I hope to make a playable chess app as the objective.

My plan thus far is to make an app that starts with the option to play against the Just Chess engine, as a single player game. Then, I hope to move to a local, player verses player game, and for kicks, I’d like to progress to a game in which two players may play remotely, with the moves being sent via text, and the game app automatically importing those text moves into play.

From there, who knows? If the text games work well, I may branch out to how to have multiple games going at once, distinguished via some tag in the text messages. Or perhaps I’ll try my hand at a Bluetooth chess app. I’m not really sure. Either way, I have some work ahead of me, and hope you enjoy following along with my journey.

Linux – keep it simple.

Getting permission to program the Bluefruit Feather board in Ubuntu

bf_f_1st program

I am learning more every day by simply trying to play with my new toy: a Bluefruit Feather board! As I mentioned before, my brother got this little gadget for me as a Christmas present, and it has been a lot of fun to play with!

I was having a bit of trouble uploading my program to it, though, until I figured out my problem. Since this took me a while, I thought I would share it with you, so perhaps you can save a little bit of time on this issue.

TL,DR: Make sure you add your username to the dialout group, so you will have permission to use the ttyUSB0 interface.

I was attempting to upload a little program (the default beginner program, slightly tweaked to change the blink time) to the board. I had the drivers installed, I could hook to the board and pull the boards data, but I couldn’t upload the new program to it.

I kept getting an error that suggested I needed to put the device in DFU mode, but that didn’t turn out to help me. The issue was simply one of permission. My username did not have permission to use the ttyUSB0 interface. So, usermod myself into the dialout group, and after logging out and back in, I was good to go!

Here is the little program that I uploaded to the board:

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

It just causes the red led to flash for 5 seconds, turn off for 1 second, and repeat endlessly. It worked great! But, that wasn’t cool enough…. So I looked over some examples and made it fade in and out with this code:

 

int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
analogWrite(LED_BUILTIN, brightness);

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

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

Now I have my own mini Cylon with a glowing, fading red eye saying, “By your command!” (Okay, so I may be having way too much fun with this….)

Linux – keep it simple.

Fool’s Mate Friday: Touching up the User Interface….

JustChessEngine

My uncle once told me that a good paint job will sell a bad airplane. He’s right. As an airplane mechanic, he knew the value of the planes that he worked on. At the same time, he saw customer after customer get “swindled” on a plane that looks nice on the outside.

The thing is, it’s true for apps and games as well. A really good program that doesn’t look good just isn’t as fun. Truth is, this engine app needed a little touch up in the gui department. So, while I didn’t make it pretty, I did add features to try to make it more useful.

Similar to the Beginning Chess Engine, I added a commit to allow the user to make a move by pressing the button. Simple stuff really, but up ’till now, I was doing all of my move making from the command line through the app code itself. Of course, if you can make a move, you need to be able to take it back to, so I added a commit to give the undo move method.

The flow of these move methods are pretty simple. For most moves, it checks the from space and the to space and puts an “*” (asterisk) at the from, and puts the piece that was there in the to space. The process is reversed for the undo move method.

There are a few exceptions, though, like the additional moves that are special, en passant, castle, and promotion. Those are handled by reading the input move first to see if it matches any of those scenarios, if not, then a regular move is performed.

Obviously, making moves in chess is a pretty important part of the game. The longest chess game theoretically possible is 5,949 moves, and that would be impossible to do if you couldn’t actually make any of those moves!

Linux – keep it simple.

Setting up the Arduino-IDE for the Bluefruit Feather in Ubuntu

arduino1.8.5

There are a lot of neat tools available to program/hack/reprogram/work with this nifty little Bluefruit Feather board! One of them is the Arduino-IDE, a development environment that allows you create and then flash your new programs to the board. I was faithfully following along in the Bluefruit Feather Arduino setup instructions, which are here:

https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/arduino-bsp-setup

But I ran into an issue right out of the gate with step #1: Install the Arduino-IDE. In the instructions it states that it must be version 1.6.12+, something that I missed the first go around. Using Ubuntu’s regular package manager, I just used apt-get install to download  and install the Arduino-IDE.

When I moved on to the next step, where you are to put in the “Additional Board Manager URL”, I ran into a snag. There was no place to do that. The block that should have been displayed there in the preferences didn’t exist. So I re-read the instructions. Turns out that the Ubuntu repository only has Arduino-IDE version 1.0.5! So it was a little out of date.

Fortunately, on the bsp setup page above, there was a direct link to download and install the newer version of the software. A quick download, unzip/untar, and a quick $ sh ./install.sh command allowed me to install the latest version in minutes. Hopefully you can avoid that same pitfall. Now, back to those instructions….

Linux – keep it simple.

Fool’s Mate Friday: Keeping the king safe!

bk

The word “Checkmate” in Chess comes from the Persian phrase “Shah Mat,” which means “the King is dead.”

While in the starting position, there are eight different ways to checkmate in two moves and 355 different ways to checkmate in three moves. Not to mention the countless other ways to be checkmated. Kind of sounds like one of those television series of “1000 terrible ways to die.”

We’ve looked before at code for making the king move, but now we want to break down how we check if the king is safe. Here’s the kingSafe commit, and we can reference several things from that code as we try to understand what I set out to accomplish.

First, let’s take a look back at the flow chart for determining available moves:

  • For each piece, check for valid moves that they can make, where the spaces are empty, or where they attack an opponent’s piece.
  • Add that move to the temporary list.
  • Once we have a temporary list of valid moves, try each move, 1 at a time, and check if the king is safe.
  • If the king is safe, add that move to the “available moves” list.
  • Put the piece back, and try the other moves on the temporary list.

With that in mind, up until now, our engine has not actually checked if the king is safe. We had a isKingSafe() method that always returned true, or “yes, he is”. Now we need to flesh that out and have it really check if it is safe. For the most part, we can combine all the pieces move options into one for the king, and just ask what pieces are at the “end” of those move lines. Here’s the flow chart:

  • Start with the king’s space number, and get the row/column number as well.
  • Using the bishop’s move table, check the next diagonal space until it is not empty. If it is not empty, is it either a bishop or a queen? Then the king is not safe.
  • Using the rook’s move table, check the up/down/left/right spaces until they are not empty. If it is not empty, is it either a rook or a queen? Then the king is not safe.
  • Using the knight’s move table, check the eight “L” spaces to see if they are empty. If it is not empty, is it either a knight? Then the king is not safe.
  • Using the king’s move table, check the surrounding spaces until it is not empty. If it is not empty, is it a king? Or in two cases, a pawn? Then the king is not safe.
  • The king must be safe!

As you can see from the flow chart, it is checking to see if the king is “not safe”. At any juncture that the king is not safe, the rest of the checks are ignored, and a “false” boolean is returned from the isKingSafe0 method. If there are no returns of “false”, then the method returns “true”, for the king must be safe.

In my mind, there are three main ways to handle the king safe issue:

  1. After each piece is moved, have it flag that the king is unsafe (e.g., you check the king, so he is unsafe).
  2. After every move, check from all pieces to see if they are attacking the king.
  3. After every move, check from the king if he is threatened by any pieces.

Method one seems hard to implement, when moving you own piece may illegally threaten the king by no longer “blocking” an attacker. Conversely, method two is way more resource intensive. You would waste time checking both bishops in all directions to see if they “find” the king. Method three seems the most logical, check from the king in all directions for the enemy, rather than from all enemies for the king.

Of the method we chose, there seem to me to be two sub-methods, that of checking if the king is safe, or checking if the king is not. I like to assume the king is safe, unless we find that he is not. I think this is faster, because the moment we find he is not safe, then we quit checking. E.g., if the bishop check reveals the king is not safe, we don’t do the rook, king, knight, pawn checks, we just return “false” – not safe.

Conversely, if we were to assume the king is not safe, and prove he is, we would have to complete all of the checks every time. This would be a waste of resources. Although, with the method I chose, most of the time, the king is safe, so we end up checking them all anyways. But, since we are trying to use this engine on cell phones, we need to save on every resource that we can.

Linux – keep it simple.