Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 1 more developer from Canada and 12 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!






Serial connection

Posted by ManTis, in Electronics 16 March 2012 · 675 views

Arduino
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 Posted Image

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:

Posted Image

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!




Nice work, but those digitalWrites give me the fear! Try http://arduino.cc/en/Reference/PortManipulation :-)
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?
There's no inherent danger other than extremely inefficient code. ;-)
Looking awesome! BTW, I came across a video the other day from pycon that I thought you might like, http://pyvideo.org/video/674/militarizing-your-backyard-with-python-computer
jjd, that's really cool, thanks! :D

September 2015 »

S M T W T F S
  1234 5
6789101112
13141516171819
20212223242526
27282930   
PARTNERS