Wednesday, March 31, 2010

Use mysqldump to copy tables from one schema to another

The following command is great for copying tables from one schema to another within the same MySQL database:
mysqldump -u DB_USERNAME_HERE --password="DB_PASSWORD_HERE" --opt SCHEMA1_NAME_HERE TABLE_NAME_HERE | mysql -u root -p SCHEMA2_NAME_HERE
Be aware that this will overwrite existing tables and data which is usually exactly what you want.
To copy to a schema on a different system:
mysqldump -h HOST_IP_HERE -u DB_USERNAME_HERE --password="DB_PASSWORD_HERE" --opt SCHEMA1_NAME_HERE TABLE_NAME_HERE | mysql -u root -p SCHEMA2_NAME_HERE

Find all files with a specific extension and delete them under Linux or Unix

This will search the current directory and recursively look through every folder under the current directory for files with the extension .gz and delete them.

find . -type f -name "*.gz" -exec rm -f {} \;

Monday, March 29, 2010

Upgrading CentOS 5.4 to PHP 5.2.11

rpm --import http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
vi /etc/yum.repos.d/utterramblings.repo

Paste into file:
[utterramblings]
name=Jason's Utter Ramblings Repo
baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka

yum install php

Wednesday, March 3, 2010

Converting military 24 hour time to 12 hour time in PHP

Here is a function I use with array_walk to convert all of the values in the array to 12 hour time from military time.


function convert_to_twelve_hour(&$i) {
if($i >= 1 && $i <= 12) {
$hour = $i;
$ampm = ($i < 12) ? 'am' : 'pm';
} elseif($i < 1) {
$hour = $i + 12;
$ampm = 'am';
} elseif($i > 12) {
$hour = $i - 12;
$ampm = ($i < 12) ? 'am' : 'pm';
}
$i = $hour.$ampm;
}


This of course is an alternative to:


date("g:i a", strtotime("14:30"));

Tuesday, March 2, 2010

Controlling a Servo attached to an Arduino via Visual Basic.NET or C#.NET

Here is an example of using VB.NET to control a servo via serial port that is attached to an Arduino board.

Attach the servo to ground, 5v and digital pin 9.

My Arduino code is also outputting to a parallel LCD. If you do not have an LCD attached you can remove those lines.

As you can see in the picture I also have the WiiChuck adapter attached. I will demonstrate the use of this adapter in a future post.

Visual Basic.NET Code:



Public Class Form1

Private serialPort As New IO.Ports.SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Try
With serialPort
.PortName = "COM3"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With

serialPort.Open()
serialPort.Write("0s")

Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TrackBar1.Scroll
serialPort.Write(TrackBar1.Value & "s")
End Sub

End Class

C#.NET Code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void trackBar1_Scroll(object sender, EventArgs e)
{
serialPort1.Write(trackBar1.Value + "s");
}

private void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
serialPort1.Write("0s");
}
}
}

Arduino C/C++ Code:



#include <servo.h>
#include <liquidcrystal.h>

Servo myservo;

int servoPosition = 1;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
lcd.print("Position Data:");
myservo.attach(9);
Serial.begin(9600);
}

void loop() {

static int val = 0;
lcd.setCursor(0, 1);

if(Serial.available()) {

char ch = Serial.read();

switch(ch) {
case '0'...'9':
val = val * 10 + ch - '0';
break;
case 's':
myservo.write(val);
lcd.print((float)val);
val = 0;
break;
}

}
}

Snowboarding at Mt. Hood Meadows