TSC3200 on Arduino

###Introduction The detailed information could be seen as in :
http://www.eefocus.com/zhang700309/blog/13-08/296390_6c438.html
###Wiring: Notice we use the interrupt 1.
images/tsc3200d.jpg ###Code

#include <TimerOne.h>
 
#define S0     6   // Please notice the Pin's define
#define S1     5
#define S2     4
#define S3     2
#define OUT    3
 
int   g_count = 0;    // count the frequecy
int   g_array[3];     // store the RGB value
int   g_flag = 0;     // filter of RGB queue
float g_SF[3];        // save the RGB Scale factor
 
 
// Init TSC230 and setting Frequency.
void TSC_Init()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);
 
  digitalWrite(S0, LOW);  // OUTPUT FREQUENCY SCALING 2%
  digitalWrite(S1, HIGH); 
}
 
// Select the filter color 
void TSC_FilterColor(int Level01, int Level02)
{
  if(Level01 != 0)
    Level01 = HIGH;
 
  if(Level02 != 0)
    Level02 = HIGH;
 
  digitalWrite(S2, Level01); 
  digitalWrite(S3, Level02); 
}
 
void TSC_Count()
{
  g_count ++ ;
}
 
void TSC_Callback()
{
  switch(g_flag)
  {
    case 0: 
         Serial.println("->WB Start");
         TSC_WB(LOW, LOW);              //Filter without Red
         break;
    case 1:
         Serial.print("->Frequency R=");
         Serial.println(g_count);
         g_array[0] = g_count;
         TSC_WB(HIGH, HIGH);            //Filter without Green
         break;
    case 2:
         Serial.print("->Frequency G=");
         Serial.println(g_count);
         g_array[1] = g_count;
         TSC_WB(LOW, HIGH);             //Filter without Blue
         break;
 
    case 3:
         Serial.print("->Frequency B=");
         Serial.println(g_count);
         Serial.println("->WB End");
         g_array[2] = g_count;
         TSC_WB(HIGH, LOW);             //Clear(no filter)   
         break;
   default:
         g_count = 0;
         break;
  }
}
 
void TSC_WB(int Level0, int Level1)      //White Balance
{
  g_count = 0;
  g_flag ++;
  TSC_FilterColor(Level0, Level1);
  Timer1.setPeriod(1000000);             // set 1s period
}
 
void setup()
{
  TSC_Init();
  Serial.begin(9600);
  Timer1.initialize();             // defaulte is 1s
  Timer1.attachInterrupt(TSC_Callback);  
  attachInterrupt(1, TSC_Count, RISING);  
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);
 digitalWrite(10,HIGH);
 
  delay(4000);
 
  for(int i=0; i<3; i++)
    Serial.println(g_array[i]);
 
  g_SF[0] = 255.0/ g_array[0];     //R Scale factor
  g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
  g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor
 
  Serial.println(g_SF[0]);
  Serial.println(g_SF[1]);
  Serial.println(g_SF[2]);
 
}
 
void loop()
{
   g_flag = 0;
   for(int i=0; i<3; i++)
    Serial.println(int(g_array[i] * g_SF[i]));
   if(((g_array[0]*g_SF[0])>(g_array[1]*g_SF[1])) && ((g_array[0]*g_SF[0])>(g_array[2]*g_SF[2])))
   {
      digitalWrite(8,HIGH);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
   }
   else if(((g_array[1]*g_SF[1])>(g_array[0]*g_SF[0])) && ((g_array[1]*g_SF[1])>(g_array[2]*g_SF[2])))
   {
      digitalWrite(8,LOW);
      digitalWrite(9,HIGH);
      digitalWrite(10,LOW);
   }     
   else if(((g_array[2]*g_SF[2])>(g_array[1]*g_SF[1])) && ((g_array[2]*g_SF[2])>(g_array[0]*g_SF[0])))
   {
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
   }        
   else
   {
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
   }
   delay(4000);
 
}

###Effect First, the program will caculate the RBG base value out.
If you put the sensor on a red object, red LED will be lighten, turn the sensor facing a green object, green LED will be lighten; blue object for blue LED.

使用超声波传感器控制LED

###Wiring Pictures The UltraSound sensor is like following picture, it only sold at 8RMB on Taobao:
ultraintaobao.jpg
The Wiring Pictures is listed as following:
/images/UltraSound.jpg ###Working Principle Trigger Pin will emit the ultra-sound, then Echo Pin will receive the reflected ultra-sound. Calculate its fleeting time then plus the speed of sound we can get the distance.
So the working method is:
a. Trig pin emmit a high signal out.
b. Echo pin will wait for receiving the high signal.
c. Calculate the fleeting time.
###The code

const int TrigPin = 2; 
const int EchoPin = 3; 
const int LedPin = 6;

float cm; 

int reverseStatus = 0;
int ledstatus = HIGH;

void setup() 
{ 
Serial.begin(9600); 
pinMode(TrigPin, OUTPUT); 
pinMode(EchoPin, INPUT); 
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, HIGH);
} 
void loop() 
{ 
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin 
delayMicroseconds(2); 
digitalWrite(TrigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm 
cm = (int(cm * 100.0)) / 100.0; //保留两位小数 
if( cm < 10 )
{
  reverseStatus = 1;
}
else
{
  reverseStatus = 0;
}
if(reverseStatus == 1)
{
  if(ledstatus == LOW)
  {
    digitalWrite(LedPin, HIGH);
    ledstatus = HIGH;
  }
  else if(ledstatus == HIGH)
  {
    digitalWrite(LedPin, LOW);
    ledstatus = LOW;
  }
}
Serial.print(cm); 
Serial.print("cm"); 
Serial.println(); 
delay(1000); 
} 

###Critical Code Walk-through

digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin 
delayMicroseconds(2); 
digitalWrite(TrigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm 
cm = (int(cm * 100.0)) / 100.0; //保留两位小数 

First write low, delay 2 microsecond, then write high, continue for 10 microsecond, then switch to low.
pulseIn() will return the time wait the pin switch to High signal. Its output is microseconds. Since the speed of sound is 340m/s, like the following functions:

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

the distance we can caculate like: the time is x microseconds, while in 1 microseconds, the sound can walk:
340m/s = 340x100cm/s = 34cm/ms, so the distance will be:
distance == xms x 34cm/ms /2 == xms x 17
Thus the code could alter to :

  //cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm 
  cm = pulseIn(EchoPin, HIGH) * 17 / 1000;

Both are OK for detecting the distance.
###Effect When you hold your hands or other object to the ultrasound in less than 10cm, the led status will be changed in 1 seconds.

HC-SR501人体感应模块 &amp; Arduino

###连线图 led – pin 6, SR501 pin 7.
/images/wiring1.jpg ###代码

//红外感应
//信号接 7 端口
//LED will be 6 port
int sigpin = 7;
int ledpin = 6;
 
void setup()
{
  pinMode(sigpin, INPUT);
  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, HIGH);
  
  Serial.begin(9600);  // 打开串口,设置波特率为9600 bps
}
 
int storefun = 0;
int ledstatus = HIGH;

void loop()
{
  int in = digitalRead(sigpin); 
  
  
  //Change the led ON/OFF accoriding to the status sensor
  if(in != storefun)
  {
    Serial.println("They are not equal!");
    if(ledstatus == LOW)
    {
      digitalWrite(ledpin,HIGH);
      ledstatus = HIGH;
    }
    else if(ledstatus == HIGH)
    {
      digitalWrite(ledpin, LOW);
      ledstatus = LOW;
    }
  }
  storefun = in; 
  Serial.println(in); //有人的时候输出高电平1 无人0
  Serial.println(storefun);
  Serial.println("***");
  delay(2000);    
}

ArchLinux中文化问题

本文将涉及到ArchLinux下中文化问题,主要是关于终端字符和vim中代码格式的细调工作。
###Vim 配置 ####Colorscheme配置:
下载几个美观的主题: solarized: https://github.com/altercation/vim-colors-solarized
molokai: https://github.com/tomasr/molokai
phd: http://www.vim.org/scripts/script.php?script_id=3139
将其解压开后,拷贝到~/.vim/colors,然后修改~/.vimrc:

	set background=dark
	"set background=bright
	"colorscheme solarized
	colorscheme molokai

####Vim字体配置:
Consolas是一种专门为编程人员设计的字体,这一字体的特性是所有字母、数字与符号均能非常容易辨认,而且所有字符都具有相同的宽度,让编人员看着更舒服。但我们用Consolas在显示程序源码时,不可避免要使用中文注释。而Consolas不支持中文,因此中文默认是使用宋体显示的。当使用10点大小的时候,中文就模糊不清了。如果采用斜体显示注释的话,宋体就更加显得支离破碎。
在中文显示上,雅黑字体确实不错,但雅黑不是等宽字体,不能用于源码显示。
使用字体工具将雅黑和Consolas集成在一起后,程序员就可以在Linux环境下的源码中看到优秀的中文显示效果。
下载地址在 :
http://dl.dbank.com/c01bo3a1eo

https://code.google.com/p/uigroupcode/downloads/detail?name=YaHei.Consolas.1.12.zip&can=2&q=

解压缩后,运行以下命令:

	sudo mkdir -p /usr/share/fonts/vista
	sudo cp YaHei.Consolas.1.12.ttf /usr/share/fonts/vista/

更改权限:

	sudo chmod 644 /usr/share/fonts/vista/*.ttf

安装字体:

	cd /usr/share/fonts/vista/
	sudo mkfontscale
	sudo mkfontdir
	sudo fc-cache -fv

###终端字体配置 更改终端模拟器的字体为Yahei Consolas Hybrid即可 gvim中字体设置:

	set guifont=YaHei\ Consolas\ Hybrid\ 11.5

其他的vim细调:

	set cursorline  "光标线
	set cursorcolumn  "竖线

最后效果如下:
/images/effect.jpg

Konsole Setup

Since konsole’s QT don’t think YaHei Consolas is the fonts, we need manually specify its configuration:

$ cat ~/.kde/share/apps/konsole/Shell.profile
[Appearance]
ColorScheme=Linux
- Font=Monospace,13,-1,2,50,0,0,0,0,0
+ Font=YaHei Consolas Hybrid,11,-1,5,50,0,0,0,0,0

Then you should close all of the opened konsoles, re-launch the konsole and you will get the beautiful views of the new fonts.

In 2015 Aug30, the configuration is changed to:

# vim $HOME/.local/share/konsole/Shell.profile
+ Font=YaHei Consolas Hybrid,11,-1,5,50,0,0,0,0,0

Setup Wordpress on Ubuntu

###Material Just some items on how to setup a wordpress website on Ubuntu12.04 and Ubuntu13.04.
The tutorial for setting up wordpress on Ubuntu12.04 is located at:
https://www.digitalocean.com/community/articles/how-to-install-wordpress-on-ubuntu-12-04

And a tutorial for setting up LAMP server on Ubuntu12.04 is located at:
https://www.digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

###TroubleShotting I encountered some problem during setup. Following is the solutions for them.
####Delete the previous installed wordpress

	# mysqladmin -uXXXX -pXXXXXXXX drop wordpress
	Do you really want to drop the 'wordpress' database [y/N] y

Then you can Re-Create the database.
####Upload file size limitation
Edit the file limitation via:

	# vim /etc/php5/apache2/php.ini
		post_max_size = 64M
		upload_max_filesize = 64M
	# /etc/init.d/apache2 restart

####Reset the default webserver Change from default nginx to apache2 server:

	$ update-rc.d -f nginx disable 
	$ update-rc.d -f apache2 enable 

####Fresh Re-install of apach2 Sometimes you have to re-set the configuration of apache, following steps will let you freshly re-install it.

	$ apt-get remove --purge apache2 apache2-utils php5-cgi php5-fpm libapache2-mod-php5filter libapache2-mod-php5  apache2.2-common
	$ apt-get install  apache2 apache2-utils php5-cgi php5-fpm  apache2.2-common
	$ apt-get install libapache2-mod-php5filter
	$ apt-get install libapache2-mod-php5