Jump to content
  • Advertisement
  • entries
    50
  • comments
    92
  • views
    66678

Serial connection

Sign in to follow this  
ManTis

1191 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!
Sign in to follow this  


5 Comments


Recommended 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] :-)

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?

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]

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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!