• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    50
  • comments
    92
  • views
    65377

Serial connection

Sign in to follow this  
Followers 0
ManTis

1018 views

Today I continued hacking on the Arduino. After setting up the LCD display, I was roaring to do something 'useful' with it. Or at least something cool. I decided on creating a tool that will display amount of free space on hard drives.

I started from the end - made routine on Arduino that takes ints ( or rather longs ) , and displays the data on the LCD. Lots of typing, lots of zeroes and ones ( or highs and lows ), but nothing really challenging. Then I took the 'Serial Monitor' tool that comes with Arduino IDE and started playing around with the Serial routines. That was easy enough and couple minutes later I had a soft that takes incoming bytes and outputs them on the screen. Finally, I've installed VS 11 ( well, the Beta is out and I have formatted my HD recently ), and wrote a tiny console program in C# that takes the first drive and sends the amount of free space on it to COM3 ( which the USB connects as ).

Here's the server code for those of you curious:

using System;
using System.IO;
using System.IO.Ports;
class Info
{
public static void Main()
{
DriveInfo[] drives = DriveInfo.GetDrives();
// write amount of free space on Drive C:
long freeSpace = drives[0].TotalFreeSpace / 1024 / 1024;
SerialPort port = new SerialPort("COM3");
port.BaudRate = 9600;
Console.WriteLine(@"Establishing connection");
port.Open();
Console.WriteLine(@"Sending data");
port.WriteLine(freeSpace.ToString());
Console.WriteLine(@"Closing connection");
port.Close();
}
}


Neither this nor Arduino code have any kind of error checking, but I don't give a damn right now. I want to LEARN ohmy.png

Right, the Arduino code:


/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/

int LED_PIN = 13;
int RS = 2;
int EN = 4;
int D0 = 5;
int D1 = 6;
int D2 = 7;
int D3 = 8;
int D4 = 9;
int D5 = 10;
int D6 = 11;
int D7 = 12;

long hd_free = 0;
void clear_number()
{
// MOVE TO FIRST DISPLAY
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
digitalWrite(D7, 1);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// MOVE TO FIRST DISPLAY
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
digitalWrite(D7, 1);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
void display_text()
{
// MOVE CURSOR TO RIGHTMOST POSITION
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 1);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// 'M'
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 1);
digitalWrite(D3, 1);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// 'B'
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 1);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// ' '
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
// 'F'
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 1);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// 'R'
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 1);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// 'E'
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);


// 'E'
digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 1);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// MOVE TO FIRST DISPLAY
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
digitalWrite(D7, 1);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
void print_memory()
{

}
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(RS, OUTPUT);
pinMode(EN, OUTPUT);
pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
// 8 BIT LCD INIT
delay(20);
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);
delay(5);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);
delay(2);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);
delay(2);

// SET INTERFACE
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 1);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);


// ENABLE DISPLAY/CURSOR
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 1);
digitalWrite(D3, 1);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

// CLEAR AND HOME
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);


// SET CURSOR MOVE DIRECTION
digitalWrite(EN, LOW);
digitalWrite(RS, 0);

digitalWrite(D0, 0);
digitalWrite(D1, 1);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);

display_text();
clear_number();

Serial.begin(9600);
}
void loop()
{

digitalWrite(LED_PIN, HIGH); // set the LED on
delay(100);
digitalWrite(LED_PIN, LOW); // set the LED on
delay(100);
int incoming = 0;
int power = 0;
hd_free = 0;
if ( Serial.available() > 0 )
{
clear_number();
while ( Serial.available() > 0 )
{
incoming = Serial.read();
if ( incoming >= 0x30 && incoming <= 0x39 )
{
if ( incoming == 0x30 )
{
hd_free = hd_free * pow(10,power) + 0;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x31 )
{
hd_free = hd_free * pow(10,power) + 1;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x32 )
{
hd_free = hd_free * pow(10,power) + 2;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 1);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x33 )
{
hd_free = hd_free * pow(10,power) + 3;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 1);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x34 )
{
hd_free = hd_free * pow(10,power) + 4;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x35 )
{
hd_free = hd_free * pow(10,power) + 5;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x36 )
{
hd_free = hd_free * pow(10,power) + 6;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 1);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x37 )
{
hd_free = hd_free * pow(10,power) + 7;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 1);
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x38 )
{
hd_free = hd_free * pow(10,power) + 8;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 0);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 1);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
else if ( incoming == 0x39 )
{
hd_free = hd_free * pow(10,power) + 9;

digitalWrite(EN, LOW);
digitalWrite(RS, 1);

digitalWrite(D0, 1);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 1);
digitalWrite(D4, 1);
digitalWrite(D5, 1);
digitalWrite(D6, 0);
digitalWrite(D7, 0);

digitalWrite(EN, HIGH);
delay(1);
digitalWrite(EN,LOW);
delay(5);
}
power=1;
}
}
Serial.print(hd_free, DEC);
Serial.println("");


}
}


And here's how it looks running on my machine:

gallery_66033_407_16602.jpg

It has enough space for up to 99 TB of free space ;). Theoretically I could have the text scrolling for WAYYYYYY more space ( LCD takes up to 80 characters ). Anyways, now that I made that prototype on Arduino board, I think I'll actually make AtMega8 - based PCB with that capability, just for kicks. A friend from Hackerspace will show me how to make a PCB, and that seems as good project as any to have for my first device.

See you around!

0
Sign in to follow this  
Followers 0


5 Comments


Nice work, but those digitalWrites give me the fear! Try [url="http://arduino.cc/en/Reference/PortManipulation"]http://arduino.cc/en/Reference/PortManipulation[/url] :-)
0

Share this comment


Link to comment
Hmm, is there any inherent danger to using digitalWrites? I understand that direct manipulation of port registers is more elegant and faster, but is there anything I should be wary of?
0

Share this comment


Link to comment
Looking awesome! BTW, I came across a video the other day from pycon that I thought you might like, [url="http://pyvideo.org/video/674/militarizing-your-backyard-with-python-computer"]http://pyvideo.org/video/674/militarizing-your-backyard-with-python-computer[/url]
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now