Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Raspberry Pi / 25 Led "Game"

Author
Message
Mikko
14
Years of Service
User Offline
Joined: 25th Sep 2002
Location: Finland
Posted: 12th May 2016 18:32
Here is my first test with the leds:
https://youtu.be/_1POJM5hCWk

Its not a 4k monitor, I did not have enough leds

Mikko

P.S. I really would like to have the simple TCP/IP send/recieve string commands in AGK.
Also I'm missing the AppGameKit player in Pi.

-------------------------------------------------------------------

// Project: MK-LEDS
// Created: 2016-05-08 Mikko Ketonen

// set window properties
SetWindowTitle( "MK-LEDS" )
SetWindowSize( 640, 480, 0 )

// set display properties
SetVirtualResolution( 640, 480)
SetOrientationAllowed( 1, 1, 1, 1 )

global display as integer [6,6]
global outputpin as integer [1,25] // virtual gpios 1-25
global GPO as integer [1,25]
GPO[1]=[0,2,3,4,14,15,17,18,27,22,23,24,10,9,11,8,7,5,6,12,13,19,16,26,20,21] // real gpios

batx=3:baty=5
ex=random(1,5)
ey=0
hurry#=1



for f=1 to 25 // set the initial output pin values
outputPin[1,f] = OpenToWrite( "gpio:"+str(GPO[1,f]) )
WriteByte( outputPin[1,f], 0)
next f


do
inc ey:
if ey>5
ey=0: ex=random(1,5):hurry#=hurry#-0.01
endif

while Timer()<hurry#
if GetRawKeyState(37) then left=1 // check for left key
if GetRawKeyState(39) then right=1 // check for right key
if GetRawKeyState(27) then stop() // check for escape key and exit
endwhile


if left and batx>1 then dec batx
if right=1 and batx<5 then inc batx
left=0
right=0


For x=1 to 5
for y=1 to 5
if x=batx and y=baty or x=ex and y=ey
display[x,y]=1
else
display[x,y]=0
endif
If batx=ex and baty=ey
For x=1 to 5
for y=1 to 5
display[x,y]=1
next
next
ey=0: ex=random(1,5)
endif
next
next

leds()
Sync()
ResetTimer()

loop



function leds()
count=0
ox=270:oy=190
for x=1 to 5
Dx=x*20
for y=1 to 5
inc count
Dy=y*20
if display[x,y]=1
drawbox(ox+Dx,oy+Dy,ox+Dx+10,oy+Dy+10,255,255,255,255,0)
WriteByte( outputPin[1,count], 1 ) // set the pin value
endif
if display[x,y]=0
drawbox(ox+Dx,oy+Dy,ox+Dx+10,oy+Dy+10,55,55,55,55,0)
WriteByte( outputPin[1,count], 0) // set the pin value
endif
next y
next x
endfunction

function stop()
for f=1 to 25
CloseFile( outputPin[1,f] )
next f
end
endfunction
mikko_ketonen( )hotmail.com
CJB
Valued Member
13
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 13th May 2016 09:54
That's SO cool! Good work! Space invaders in 25 dots! Great
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Mikko
14
Years of Service
User Offline
Joined: 25th Sep 2002
Location: Finland
Posted: 13th May 2016 13:00
Thanks CJB, your pinout diagram was very useful in this.
Mikko
mikko_ketonen( )hotmail.com
CJB
Valued Member
13
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 13th May 2016 16:04
I want to see the invaders dropping bombs now, and then I want to see 25 dot Pac Man, and 25 dot Snake!
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Dybing
5
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 20th May 2016 16:14
Just be careful with how many LEDs you power and control directly from your Pi. It will only allow a given amount before you get into trouble and possibly damage your Pi due to overloading its' output power rails.

Play it safe by either multiplexing - shift registers are your friend - or use a separate power source and control the LEDs via some mosfets (if PWM) or transistors (simple on/off).
BatVink
Moderator
14
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th May 2016 20:43
Dybing, can you expand more on the MOSFET for PWM?
I have a need to convert a pulse to different voltage levels. My old electronics head tells me I need a capacitor/resistor setup to smooth the pulse into a voltage level, but maybe I am a long way out of date?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Dybing
5
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 21st May 2016 01:21 Edited at: 22nd May 2016 03:57
@Batvink; No, that is what you'd do. I've read up a bit on the Pi, and as far as I understand it, the GPIO can provide 3.3V and up to 16mA per pin and up to 50mA total over all pins. Which is next to nothing. Your standard 3mm or 5mm LED can draw up to 20mA.

To provide more power, you'd just hook the Drain (towards VCC) and Source (towards GND) to circuit, and the Gate to the Pi to control the Mosfet. Using PWM, after the Source you'd add a resistor and then a small smoothing cap to ground - a few uF will do at these low voltages. The adjusted power you draw from where the resistor and cap hook up. Just remember that the Pi and the secondary power-rail need share ground, or else you'll release the magic smoke from something or other and all the electrons will escape!

However it is a bit mute as AppGameKit do not support PWM (yet) as I've understood it?! So for now, just use a standard TO92 (up to 9V) or TO220 (>9V) packaged NPN transistor for simple on or off control of stuff connected to the secondary power rail. Just remember (again) to share ground between secondary power rail and Pi.

edit: got my Drain and Source reversed - fixed.
BatVink
Moderator
14
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st May 2016 16:02 Edited at: 21st May 2016 16:03
Quote: " Just remember (again) to share ground between secondary power rail and Pi."


I hadn't realised that, thanks.

I have made my own PWM using the tween commands. If you create a linear tween, and then apply some sin/cos/tan maths, you can get a nice transition from nothing to full power by switching on/off using MOD().
Using just the linear tween (or Timer) you can use MOD 2,3,4 etc to create constant PWM.

What I still don't understand is why you need a Mosfet rather than any transistor fro PWM?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Dybing
5
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 22nd May 2016 03:06 Edited at: 22nd May 2016 03:26
The main difference is that mosfets are voltage driven, whereas transistors are current driven. This allows for very fast switching as changing state of voltage is faster than pumping more or less current - just remember to put a pull down resistor between the GPIO pin, or better yet, set the GPIO pin to pulldown as default, and the mosfet Gate so that it never floats - it can cause erratic behaviour. On a transistor on the other hand, the amount of current on the base will vary the resistance between the collector and emitter.

In short, mosfets are great for making square waveforms due to how fast they can switch, whereas transistors are great for more 'wavy' waveforms, like sine waves and such due to their greater control of output though slower reaction time. Which makes transistors great for analogue circuits, but for digital work it can be a bit slow and can eat a lot of current - which your Pi don't have much of. With Voltage however, the Pi can deliver as much 3.3V you want anywhere - just as long as the current draw is low. Which favours mosfets. Mosfets also have much lower voltage drop over the source and drain than a transistor over the collector and emitter, which in higher current applications matter quite a bit due to the extra heat the high resistance of a transistor generate.

But at these low voltages and current draws it doesn't matter too much. A good Logic Level mosfet or npn transistor will in nearly all applications do as good a job. It becomes more of an issue when you try and scale things up. Like say try and control 1W or 3W LEDs using a Pi or Arduino.
BatVink
Moderator
14
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd May 2016 13:43
Thanks for the explanation. I bought standard NPN transistors, but I think I will be Ok for my application. I am using them to switch 12V LEDs, which are drawing their power from a secondary power source. A couple of milliseconds extra is fine.

The transistors I bought for the LED strip - where I want to modulate the output - are by sheer luck, mosfets. I got them for the higher voltage and current rating.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Dybing
5
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 22nd May 2016 19:13
I am myself waiting for a shipment of WS2812 LED RGB 'pixels'. I got this little project going where I need control 120 LEDs in full color, so doing it the old fashioned way is right out of the question. That would require 4 shift registers and 12 TLC5940 PWM drivers and countless hours of soldering it all together. The 'all-in-one' LED pixels is much easier to work with

Basically I'm making a 6 digit clock which can either be boring old single-color, change color per pixel in a plasma effect (of old amiga demo-scene fame) or have the digits work as EQ spectrum visualizer for music via either a built in mic or audio input to a jack port.

I've used AppGameKit to prototype the code whilst waiting for the parts - but it'll be a standalone device controlled by an Atmega 328P MCU (and hardware-prototyped using an Arduino UNO), so it'll all have to be translated to C. Though, if it all works out I can release the AppGameKit code. Now it draws 'virtual LEDs' to screen, but a few tweaks here and there and it should be easy enough to have it run the hardware from a Pi. Hmmm, I'll have a look at it after I've gotten myself an actual Pi. Perhaps make a little 'How to' project out of it

Login to post a reply

Server time is: 2017-06-26 03:00:43
Your offset time is: 2017-06-26 03:00:43