Last active
August 29, 2015 14:17
-
-
Save sj82516/7ec10ff907b9e1a3db82 to your computer and use it in GitHub Desktop.
NCTU_Embedded system_Arduino_Lab02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//NCTU_Embedded system_Arduino_Lab02 | |
//Read and compare sensor | |
//then display on 5*7 LED matrix | |
//show the "HelloWorld" and shift left or right by the comparison | |
//display like this project:http://musicdiver.com/wordpress/2013/01/arduino-scrolling-text-on-5x7-led/ | |
int scan=0; | |
int matrix[7][50]; | |
//transform letter to 5*7 LED matrixs | |
int H[7][5]={ | |
{0,0,0,0,0}, | |
{0,1,0,1,0}, | |
{0,1,0,1,0}, | |
{0,1,1,1,0}, | |
{0,1,0,1,0}, | |
{0,1,0,1,0}, | |
{0,0,0,0,0} | |
}; | |
int e[7][5]={ | |
{0,0,0,0,0}, | |
{0,1,1,1,0}, | |
{0,1,0,1,0}, | |
{0,1,1,1,0}, | |
{0,1,0,0,0}, | |
{0,1,1,1,0}, | |
{0,0,0,0,0} | |
}; | |
int l[7][5]={ | |
{0,0,0,0,0}, | |
{0,1,0,0,0}, | |
{0,1,0,0,0}, | |
{0,1,0,0,0}, | |
{0,1,0,0,0}, | |
{0,1,1,0,0}, | |
{0,0,0,0,0} | |
}; | |
int o[7][5]={ | |
{0,0,0,0,0}, | |
{0,0,0,0,0}, | |
{0,0,0,0,0}, | |
{0,1,1,1,0}, | |
{0,1,0,1,0}, | |
{0,1,1,1,0}, | |
{0,0,0,0,0} | |
}; | |
int r[7][5]={ | |
{0,0,0,0,0}, | |
{0,0,0,0,0}, | |
{0,0,0,0,0}, | |
{0,1,0,1,0}, | |
{0,1,1,0,0}, | |
{0,1,0,0,0}, | |
{0,0,0,0,0} | |
}; | |
int d[7][5]={ | |
{0,0,0,1,0}, | |
{0,0,0,1,0}, | |
{0,0,0,1,0}, | |
{0,0,1,1,0}, | |
{0,1,0,1,0}, | |
{0,1,1,1,0}, | |
{0,0,0,0,0} | |
}; | |
int W[7][5]={ | |
{0,0,0,0,0}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,1,0,1}, | |
{1,1,0,1,1}, | |
{1,0,0,0,1}, | |
{0,0,0,0,0} | |
}; | |
//display | |
void display(){ | |
//scan from row , so at first should clean up the row | |
//my case is that when set ROW to Low and Column to HIGH | |
//LED turn on | |
for(int k=7;k<=13;k++){ | |
digitalWrite(k,HIGH); | |
} | |
for(int i=0;i<7;i++){ | |
//Row "turn on" one by one, control by for loop | |
digitalWrite(i+7,LOW); | |
//current column scan | |
int cur_Col=0; | |
//scan is the sceen range , in order to shift left or right | |
for(int j=scan;j<scan+5;j++){ | |
//check whether LED should light or not | |
if(matrix[i][(j%50)]==1){ | |
digitalWrite(cur_Col+2,HIGH); | |
delayMicroseconds(100); | |
} | |
//clean up column | |
digitalWrite(cur_Col+2,LOW); | |
delayMicroseconds(100); | |
cur_Col++; | |
} | |
//clean up row | |
digitalWrite(i+7,HIGH); | |
} | |
} | |
//setup a big matrix include "Hello World" | |
void setMatrix(){ | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j]=H[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+5]=e[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+10]=l[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+15]=l[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+20]=o[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+25]=W[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+30]=o[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+35]=r[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+40]=l[i][j]; | |
} | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<5;j++) | |
matrix[i][j+45]=d[i][j]; | |
} | |
} | |
// the setup routine runs once when you press reset: | |
void setup() { | |
// initialize serial communication at 9600 bits per second: | |
Serial.begin(9600); | |
for(int i=1;i<=13;i++){ | |
pinMode(i,OUTPUT); | |
} | |
} | |
unsigned long TimerA; | |
// the loop routine runs over and over again forever: | |
void loop() { | |
TimerA= millis(); | |
while(scan<51){ | |
//detect sensor value | |
int sensorValue_L = analogRead(A4); | |
int sensorValue_R = analogRead(A5); | |
//letter move speed | |
if(millis()-TimerA >= 300UL){ | |
//value compare and screen move direction | |
if(sensorValue_L>sensorValue_R){ | |
scan=(--scan)%50; | |
if(scan<0)scan=scan+50; | |
} | |
else{ | |
scan=(++scan)%50; | |
} | |
//reset timer | |
TimerA= millis(); | |
} | |
// main function | |
setMatrix(); | |
display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment