This program is for three 7 segments controlling.
You can display three different digits, such as 4 1 2,7 9 8,0 0 9 and so on.
the program can ignore wrong amount of digits such as 1 2 3 4 5, 2 3, 1 , 0987654 and so on.
the program can ignore wrong combinations of words such as 0 g 0, t k 3 4 4, 1 a 2 s 3 d, and so on.
//reguler variable
int LEDpin = 13;
int c;
int rec1, rec2, rec3;
// error checker
int checkND=0;
//loop counter to change 7 segment
int L=10;
int superL;
int superLL;
int superLLL;
//controller variables
int refleshT=1;
int vTime=500;
int amount7S =3;
void setup()
{
Serial.begin(9600);
for (int i=2; i<10; ++i){
Serial.print("set OUTPUT to digital pin");
Serial.println(i);
pinMode(i,OUTPUT);
}
for (int i=10; i<13; ++i){
Serial.print("set INPUT to digital pin");
Serial.println(i);
pinMode(i,INPUT);
}
pinMode(LEDpin,OUTPUT);
Serial.println("LED is ready to use");
for (int i=10; i<13; ++i){
Serial.print("set pin");
Serial.print(i);
Serial.println("to Ground");
digitalWrite(i,HIGH);
digitalWrite(i,LOW);
}
Serial.println("OK pins are ready");
}
void loop()
{
if(Serial.available()>0){
//reading validity of blocks
block1();
block2();
block3();
//extra protection
EXchecker();
//main protection
if (checkND ==amount7S){
//super high speed loop 60Hz
//main LED loop
for(int FT=0; FT < vTime; ++FT){
L = superL;
c = rec1;
process();
L = superLL;
c = rec2;
process();
L = superLLL;
c = rec3;
process();
}
}
//reset
nothing();
reset();
}//main if end
//protection reset
checkND = 0;
}
//LED controller
void zero(){
nothing();
digitalWrite(2,HIGH);
//digitalWrite(3,HIGH); disabled
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(refleshT);
}
void one(){
nothing();
//digitalWrite(2,HIGH); disabled
//digitalWrite(3,HIGH); disabled
//digitalWrite(4,HIGH); disabled
//digitalWrite(5,HIGH); disabled
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
//digitalWrite(9,HIGH); disabled
delay(refleshT);
}
void two(){
nothing();
//digitalWrite(2,HIGH); disabled
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
//digitalWrite(6,HIGH); disabled
//digitalWrite(7,HIGH); disabled
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(refleshT);
}
void three(){
nothing();
//digitalWrite(2,HIGH); disabled
digitalWrite(3,HIGH);
//digitalWrite(4,HIGH); disabled
digitalWrite(5,HIGH);
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(refleshT);
}
void four(){
nothing();
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
//digitalWrite(4,HIGH); disabled
//digitalWrite(5,HIGH); disabled
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
//digitalWrite(9,HIGH); disabled
delay(refleshT);
}
void five(){
nothing();
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
//digitalWrite(4,HIGH); disabled
digitalWrite(5,HIGH);
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
//digitalWrite(8,HIGH); disabled
digitalWrite(9,HIGH);
delay(refleshT);
}
void six(){
nothing();
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
//digitalWrite(8,HIGH); disabled
//digitalWrite(9,HIGH); disabled
delay(refleshT);
}
void seven(){
nothing();
digitalWrite(2,HIGH);
//digitalWrite(3,HIGH); disabled
//digitalWrite(4,HIGH); disabled
//digitalWrite(5,HIGH); disabled
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(refleshT);
}
void eight(){
nothing();
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(refleshT);
}
void nine(){
nothing();
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
//digitalWrite(4,HIGH); disabled
//digitalWrite(5,HIGH); disabled
//digitalWrite(6,HIGH); disabled
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(refleshT);
}
void nothing(){
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(refleshT);
}
//special loop
//reset pins
void reset(){
for (int e=10; e<13; ++e){
pinMode(e,INPUT);
}
}
//judgement process
void process(){
if(c=='8'){
reset();
pinMode(L,OUTPUT);
eight();
}
else if(c=='0'){
reset();
pinMode(L,OUTPUT);
zero();
}
else if(c=='1'){
reset();
pinMode(L,OUTPUT);
one();
}
else if(c=='2'){
reset();
pinMode(L,OUTPUT);
two();
}
else if(c=='3'){
reset();
pinMode(L,OUTPUT);
three();
}
else if(c=='4'){
reset();
pinMode(L,OUTPUT);
four();
}
else if(c=='5'){
reset();
pinMode(L,OUTPUT);
five();
}
else if(c=='6'){
reset();
pinMode(L,OUTPUT);
six();
}
else if(c=='7'){
reset();
pinMode(L,OUTPUT);
seven();
}
else if(c=='9'){
reset();
pinMode(L,OUTPUT);
nine();
}
else{
nothing();
reset();
return;
}
}
//first block
void block1(){
rec1=Serial.read();
Serial.print("got Serial");//confrmation
Serial.print(rec1,BYTE);
c = rec1;
Cprocess();//check prosecc
superL = 10;
L = superL;
}
//second block
void block2(){
superLL = superL+1;
L = superLL;
rec2=Serial.read();
Serial.print("got Serial");
Serial.print(rec2,BYTE);
c = rec2;
Cprocess();
}
//third block
void block3(){
superLLL = superLL+1;
L = superLLL;
rec3=Serial.read();
Serial.print("got Serial");
Serial.println(rec3,BYTE);
c = rec3;
Cprocess();
}
// 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
}
}
}
// 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{
nothing();
reset();
return;
}
}
0 件のコメント:
コメントを投稿