2010年8月6日金曜日

Program for LED controller Ver 1.0(Updated from Ver 0.11 and checked)

/*
Program for Art Work
Version 1.0
Huge 7 segment controller with Shift registor.
Made by Shotaro N 2010

note:
Somehow,you need Serial.print(); in a function of block();.
It does not do anything, but without having it, the program does not work properly.
*/

//reguler variable to store data
int c;
int rec1, rec2, rec3, rec4;
// error checking counter
int checkND=0;
//controller variables
int vTime=25000;
int amount7S =4;
//Shift Registor
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int latchPin2 = 2;
int clockPin2 = 4;
int dataPin2 = 3;


void setup()
{
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
}
void loop(){
if(Serial.available()>0){
//reading validity of words
block1();
block2();
block3();
block4();
Serial.print("this is first");
Serial.println(checkND);
//to ignore words with EXtra
EXchecker();
Serial.print("this is second");
Serial.println(checkND);
//main checking process
if (checkND ==amount7S){
Serial.println("start light LEDs");
//main LED
digitalWrite(latchPin, LOW);
c = rec4;
process();
c = rec3;
process();
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin2, LOW);
c = rec2;
processSEC();
c = rec1;
processSEC();
digitalWrite(latchPin2, HIGH);
delay(vTime);
checkND = 0;
reset();
}
//checkND = 0;
//reset();
}//main if end
//protection reset
checkND = 0;
reset();
}

//EDN OF VOID LOOP


//first block
void block1(){
rec1=Serial.read();
c = rec1;
Cprocess();//check prosecc
Serial.print("block1");
}
//second block
void block2(){
rec2=Serial.read();
c = rec2;
Cprocess();
Serial.print("block2");
}
//third block
void block3(){
rec3=Serial.read();
c = rec3;
Cprocess();
Serial.print("block3");
}
//Fourth block
void block4(){
rec4=Serial.read();
c = rec4;
Cprocess();
Serial.print("block4");
}

// checker
void Cprocess(){
if(c=='8'){
checkND = checkND + 1;
}
else if(c=='0'){
checkND = checkND + 1;
}
else if(c=='1'){
checkND = checkND + 1;
}
else if(c=='2'){
checkND = checkND + 1;
}
else if(c=='3'){
checkND = checkND + 1;
}
else if(c=='4'){
checkND = checkND + 1;
}
else if(c=='5'){
checkND = checkND + 1;
}
else if(c=='6'){
checkND = checkND + 1;
}
else if(c=='7'){
checkND = checkND + 1;
}
else if(c=='9'){
checkND = checkND + 1;
}
else{
}
}

//judgement & lighting
void process(){
if(c=='8'){
shiftOut(dataPin, clockPin, MSBFIRST, 254);
}
else if(c=='0'){
shiftOut(dataPin, clockPin, MSBFIRST, 250);
}
else if(c=='1'){
shiftOut(dataPin, clockPin, MSBFIRST, 96);
}
else if(c=='2'){
shiftOut(dataPin, clockPin, MSBFIRST, 220);
}
else if(c=='3'){
shiftOut(dataPin, clockPin, MSBFIRST, 244);
}
else if(c=='4'){
shiftOut(dataPin, clockPin, MSBFIRST, 102);
}
else if(c=='5'){
shiftOut(dataPin, clockPin, MSBFIRST, 182);
}
else if(c=='6'){
shiftOut(dataPin, clockPin, MSBFIRST, 62);
}
else if(c=='7'){
shiftOut(dataPin, clockPin, MSBFIRST, 226);
}
else if(c=='9'){
shiftOut(dataPin, clockPin, MSBFIRST, 230);
}
}
//judgement & lighting
void processSEC(){
if(c=='8'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 254);
}
else if(c=='0'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 250);
}
else if(c=='1'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 96);
}
else if(c=='2'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 220);
}
else if(c=='3'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 244);
}
else if(c=='4'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 102);
}
else if(c=='5'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 182);
}
else if(c=='6'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 62);
}
else if(c=='7'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 226);
}
else if(c=='9'){
shiftOut(dataPin2, clockPin2, MSBFIRST, 230);
}
}


// EXchecker. protect system from invalid value
void EXchecker(){
if(Serial.available()>0){
int reader = Serial.read();
if(reader == -1){
}
else{
checkND = checkND +10;// protect from hack
}
}
}


void reset(){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
digitalWrite(latchPin2, HIGH);
}

0 件のコメント:

コメントを投稿