Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Thursday, February 28, 2019

Arduino and NRF24L01 - test code



A while back I made a game using some Arduino Nano and NRF24l01 called ButtonFlash (https://www.winkleink.com/2016/08/buttonflash-game-made-with-raspberry-pi.html)

Mnadatory video showing it being played.



It was good fun to make and people seemed to enjoy it but it wasn't in a state for someone else to make easily and wasn't robust so running repairs were needed.

But I really liked it and wanted to do a version that others could make easily.
At the same time EasyEDA was starting to be advertised as a simple web based PCB design tool with integration to a PCB fabrication facility so getting the boards manufactured would be super easy.  So, I took the plunge and designed a PCB and had 10 of them manufactured.

Soldered up

I soldered a pair and started running  code to test them and it kept failing.  I tried different libraries and different setups and every time I couldn't get it to work reliably.  This is part of the pleasure of using code and electronic components created by others.

Today I figured out one of the NRF24l01 that I used can transmit but not receive.  Maybe I damaged it as had power caps in the wrong way around when testing early on.

I soldered up a 3rd but without the switch and LED and ignoring the bad one the pair I used were reliable today.



Now as I tried lots of sample code and had different results with them I have prepared my own tester code.
This code uses the Arduino, NRF24l01, LED attached to Pin2 (through current limiting resistor) and switch on Pin3 using internal pull up.

When uploaded the Arduinos start in transmit mode.
Press the switch on one and it transmits and switches to receive mode.
Now you have one in transmit and one in receive mode.

Pressing the switch on the transmit one and it's LED will flash 5 times and change to receive mode and the receive one will turn it's LED on full and change to transmit mode.

It's amazing how much fun it is to press the switch. See the change. Then press the switch on the other one and see them swap roles.

So, if you are messing with Arduino and NRF24l01 then maybe this code will be useful to help you.

Code is available on GitHub 


Now to actually make ButtonFlash 2.0.



Friday, May 8, 2015

Created a game in Scratch

I came across some videos of Jeff Minter who created some amazingly original games in the 8 bit era and is still going strong developing new games today and it got me thinking of one of his stranger games that I remember playing on the Commodore VIC-20.
Meta Galactic Llama Battle at the Edge of Time.  Here's a video of it on the Commodore 64. I can't remember if I had it for the C64 as well, but definitely played it on the VIC-20


As with most of Jeff Minters games it kind of takes a slightly different approach.
You're a Llama at the bottom of the screen and you can move left and right.  You can exit one side of the screen and come in the other side as well.
You shoot lasers out of your mouth at 45 degrees and you have to shoot the spiders falling from the top of the screen.
If a spider reaches the ground it starts to crawl towards you making evasive action essential. 
All very interesting so far.  To add to the complexity and the control you can also raise and lower a ceiling for your laser to bounce off of to assist in shooting the spiders.

 I had great fun with this game all those years ago and so wanted to see if I could re-create something similar using Scratch.

After some fumbling I put created a little game inspired by the Llamas.

Below is the game and here is a Link to project on Scratch website if you want a peek inside. https://scratch.mit.edu/projects/61239650/




It's been fun developing a game in Scratch. I'm most definitely not an expert and I expect there are a lot of refinements that could be done to the code to improve the game but it plays similar to the original.  I hope you enjoy it.

Friday, May 1, 2015

7th Egham Raspberry Jam - 26th April 2015

What a great Sunday afternoon of Raspberry Pi activity.

40+ people turned up on the day with an ever greater proportion of parents with their kids with a real buzz on the day.

The most popular tables were the ones where a boy and girl were showing other kids how to program in Scratch.  I had a lovely experience at the end of the event where a father and his daughter came of to say they were leaving and the daughter was beaming as she had just written her first program in Scratch.  It was amazing to see the excitement on her face and the please she was experiencing from leaning a new skill.

For those interested in the latest in desktop environments +Martin Wimpress was there showing how fantastic Ubuntu Mate run on the Raspberry Pi 2 with desktop effects enabled. We had a great chat about RPi.GPIO support and what it would take to have it baked into the next build.
Martin also solved one of my questions. When navigating menus in Mate on the Pi2 sometimes they would stick and not respond.  I thought this was an I/O issue but it looks like it is down to OpenOffice have massive icons SVGs so on first opening a menu the Raspberry Pi has to render the icons for display. This happens once when you go to the menu for the first time. For return visits the icons are already renders so the menu is nice and fast.
I'm trying to decide if I will uninstall OpenOffice as Abiword and Gnumeric give me everything I need and then for graphics I use GIMP and Inkscape.
Martin had a build of Ubuntu Mate that used F2FS (http://en.wikipedia.org/wiki/F2FS) This is a file system specifically designed for flash memory and is suppose to improve the life of SD cards as well as improve performance.  I haven't had a chance to test yet but another win for optimisation.

As a side note GIMP and Inkscape are definitely usable on the Pi2.  I have previously used both on hardware of similar performance for commercial website development.  It's not going to match your i5/i7 with 16GB RAM and the latest graphics card, but it is definitely usable.

A second side note not from the Jam. I had my Ubuntu Mate Pi2 set up at home and needed to print.
It found my wifi network printer and installed all the correct drivers without me having to do anything special.  I love that Graphics acceleration just work, wifi (with the right dongle) just works and printing just works. There are the 3 points that I usually have concerns about on Linux and with the Raspberry Pi they just work.

Back to the Jam details.
Among the many items on display were:

Music playing Octopus (see flickr link at the bottom for pictures)
Portable games console,
Robot arm controlled by Scratch,
Crumble board (http://redfernelectronics.co.uk/crumble/) A great little interface board programmed using an block based interface like scratch.
Minecraft fun
Robot with robotic arm and webcam all controlled using PWM to vary speed from an Android table.
Sonic Pi coding +Sonic Pi
Scratch Development
CNC machine controlled by Raspberry Pi by +Stephen Cornes
EEG Brainwave reading using Mindflex, Arduino and Raspberry Pi
7 Segment of Pi board for development
Pitrol - build it yourself controller for the Raspberry Pi

At the end of the event I got the TwitterCam running and it did take pictures and tweeted them
The pictures can be see at the @EghamJam feed

Here is a link to some pictures from the event if you want to get a flavour for what happens at the Egham Jam.

https://www.flickr.com/photos/tictactoepony/sets/72157649918333124/

What a great day and thank you to everyone who came and brought their projects and enthusiasm to share.


We also had some Motorola LapDock troubleshooting going on as well.  These things turn your Raspberry Pi into a laptop


EEG Brainwave reader with Ubuntu Mate Visualisation



Looking forward to the next Egham Jam.


Thursday, March 19, 2015

16x16 LED with Arduino

I think I've mentioned before I have a bit of an eBay habit.  Searching for random electronics parts that can work with Arduino or Raspberry Pi.

On one of these eBay trawls I cam across the following 16x16 LED display.  It's really 4 8x8 LED displays on a single board with what look like electronics to make it easy to control.
Looking at it I kind of thought this should be easy to get working so put down my 


When I finally got around to plugging it in I did the obvious and searched online to see if someone with more electronics and computing ability had played with the 16x16 already and worked it out.
To my pleasure and delight I came across the following page with schematic, datasheets and Arduino sample code.

I wired up the display as directed and uploaded the sketches provided and it worked first time.
The demo code had comments not in English and even then there were very few comments so I kind of figured out that the data for the pixels was held in an array of 32 Bytes. (32 x 8 = 16 x 16, so a Bit for each dot)

The one thing I couldn't figure out from the data was what order the data had to entered into the array in and thought to move this forward I'd figure out the order of the data.
First thing I found was that a 0 in the bit turned the pixel on, so the image is reversed.
Then by putting B11111111 in each position one after another I figured out the order.


TOP LEFT 8x8 
BOTTOM LEFT 8x8 
TOP RGHT 8x8
BOTTOM RIGHT 8x8

Best way to think of it is 
Row 1 / Columns 1-8
Row 2 / Columns 1-8
Row 3 / Columns 1-8
Row 4 / Columns 1-8
Row 5 / Columns 1-8
Row 6 / Columns 1-8
Row 7 / Columns 1-8
Row 8 / Columns 1-8
Row 9 / Columns 1-8
Row 10 / Columns 1-8
Row 11 / Columns 1-8
Row 12 / Columns 1-8
Row 13 / Columns 1-8
Row 14 / Columns 1-8
Row 15 / Columns 1-8
Row 16 / Columns 1-8
Row 1 / Columns 9-16
Row 2 / Columns 9-16
Row 3 / Columns 9-16
Row 4 / Columns 9-16
Row 5 / Columns 9-16
Row 6 / Columns 9-16
Row 7 / Columns 9-16
Row 8 / Columns 9-16
Row 9 / Columns 9-16
Row 10 / Columns 9-16
Row 11 / Columns 9-16
Row 12 / Columns 9-16
Row 13 / Columns 9-16
Row 14 / Columns 9-16
Row 15 / Columns 9-16
Row 16 / Columns 9-16

And just to show I managed to get something displayed here is one I made earlier.



More playing to be done now I can get an image on the display.







Tuesday, February 26, 2013

Raspberry Pi Minecraft and Python - create lots of blank worlds to mess about in.

I've been playing with Python and Minecraft on the Raspberry Pi.  Using the API included you can write Python scripts to build in Minecraft.
I like to work with a blank world rather than an existing landscape because the landscape may not work or if I make a mistake it can be hard to undo.

So, I wanted to quickly create a bunch of disposable worlds that I can run my very rough scripts in to see where I need to tweak them.

NOTE: for some reason some flowers and things are left behind. It might be an attribute thing...

Code Below:

#!/usr/bin/python

import sys
import mcpi.minecraft as minecraft
import mcpi.block as block

mc = minecraft.Minecraft.create()

mc.postToChat("Clearing ALL")

blockId = block.AIR.id
mc.setBlocks(-127,-127,-127,127,127,127,blockId)

mc.postToChat("create floor")

blockId = block.DIRT.id
mc.setBlocks(-127,0,-127,127,0,127,blockId)


There are no indents so should just work if cut and pasted to a text file and saved.  I used clearall.py

Start Minecraft
Create a World

RUN:
python clearall.py

The whole world is made of AIR with a 'ground' of DIRT is added at Y=0.
This will take a while to run so be patient.

Now you have one blank world.

But that would be slow to do that every time you need a blank world.

Well with a bit of jiggery pokery you don't have to.
The method I used is probably a bit manual but it is faster than coding for new worlds every time.
Maybe somebody brighter than me can automate it.

Step 1. Make sure you can see hidden folders when browsing on your Raspberry Pi.
The got to ~/pi (if your username is pi)
Open .minecraft/games/com.mojang/minecraftWorlds
This is the directory where your saved Worlds are stored.
One per folder. For me the blank world I created was called world

Create a bunch of new directories. I used world-blank1, world-blank2 etc....
Make as many as you want.

Go into the original world directory and copy all the files.
Then paste those files into each of your world-blank directories.
Now you have replicated the blank world you created with the above script too all of these world maps.

If you go back into Minecraft they will be there. Only problem is they will all have the same name. Not ideal when you want to know which ones you have broken and which are still clean.

This is where it gets a bit more low level but still quite simple.

Installed ghex a gui based hex editor with sudo apt-get install ghex

Then for each folder open level.dat using ghex and find where it says world
Modify the text to a different name making sure to keep it the same size.  Maybe B0001.
Save the updated level.dat.
Do this for each of your new directories making sure to use a different name (B0002,B0003,...) .

If you run minecraft you will get a list of Worlds all with different names.  For me the order was still a bit mixed up but I can live with that.

Finally, rather than having to do this every time I created a new directory under com.mojang called backup-blank and copied all my newly made blank world into that directory.

Now I can mess about with the blank worlds. Building all kinds of strange shapes not caring about  it being perfect as I can just delete that world and replace it with a new clean one by just copying the saved worlds in backup-blank  back into the minecraftWorlds directory and start again.

Note: For me I have 20(ish) blank worlds created as I mess up a fair bit and this usually allows me to do one good session without having to copy across or create a new blank world.

Happy Coding.



Sunday, February 17, 2013

Quiz Master Raspberry Pi version now working


I copied the Windows code on to my Raspberry Pi last night and it worked perfecty if run from IDLE. But, if I ran it from either a terminal window or double clicked on the icon it failed.

There were 2 problems.

First was that IDLE on Windows put a ^M at the end of every line so the shebang statement on the first line was failing.
To fix this I copied it all into vi and saved it out. This stripped put all the ^M things.  I must admit I didn't expect this from IDLE.
Then after some more searching  and trial and error I eventually figured out that the directory for the program is not set to where the program is run from so when I try to open the players.txt file it fails and dumps me out.

Using os.getcwd() I can find out which folder the program was run from and set it to the path for players.txt and the sound files.

I revised the code and attached a new pack to the forum that now works on the Raspberry Pi.

Make sure to chmod +the .py to make it executable.

Thursday, July 14, 2011

Arduino - Web server controlled LED using serial connection between the Arduino and web server

I was chatting to a friend who was working on home automation.  Controlling lights, heating and other stuff around the house using a touch screen controller and I thought if you used a web site as the interface then connected to the Arduino over a serial link it would be possible to switch things on and off from any devlice that could display a web page and access the web server.

Personally, I would not recommend using a serial link from the server to the Arduino as the Arduino resets itself everytime something is sent over serial,  More correctly an ethernet shield would be great.  Giving more scalability as the web server can talk to mutliple Arduinos with no great extra effort.

I installed XAMPP on my netbook as the webserver and used PHP code on the server to present the web interface and send the command to the Arduino over serial.  The Arduino is waiting for a serial command and then depending on what it is will turn on or off the mounted LED.

So, not the most impressive looking demo, but as a proof of concept if I can turn on a LED I can control a relay to turn on an kind of device.


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


PHP on the server:

<?php

exec('mode com4: baud=9600 data=8 stop=1 parity=n xon=no');

$switch1 = "";

/* Serial script for pan/tilt Camera with servos */
/* Script by Aneal Khimani, 2-12-10 */

//check the GET action SuperGlobal var to see if an
//action is to be performed

if (isset($_GET['action'])) {

$switch1 = $_GET['action'];

//Action required

switch ($switch1) {
case "on":
$fp = fopen("com4", "w");
fwrite($fp, chr(97));
fclose($fp);
break;

case "off":
$fp = fopen("com4", "w");
fwrite($fp, chr(98));
fclose($fp);
break;


}
}

?>

<html>
<body>
<center>

<p>
<font size="4">Flick Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">On</a></td><td><?php if($switch1 == 'on'){echo "<b>ON</b>";} ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">Off</a></td><td><?php if($switch1 == 'off'){echo "<b>OFF</b>";} ?></td></tr>
</table>
</p>

</center>
</body>
</html>


Arduino Code:

/*

  Used with the PHP code
  http://localhost/arduino.php
 */

void setup() {               

  Serial.begin(9600); // initialize serial communication:

  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {

// read the serial port:

if (Serial.available() > 0) {
int inByte = Serial.read();

switch (inByte) {
case 'a':
  digitalWrite(13, HIGH);   // set the LED on
  Serial.println("HIGH");
 
break;

case 'b':
  digitalWrite(13, LOW);   // set the LED off
  Serial.
  println("LOW");

break;
}

}

}