Today we are going to attempt to build a rocket launch system with the arduino. Expect some pictures soon!
Code
There is a new variable invert added in. This means that the 7 segment can be active high or active low, if turning the segment high turns it on on turning it low turns it on.
int a = 3;
int b = 4;
int c = 5;
int d = 6;
int e = 7;
int f = 8;
int g = 9;
int seg_one = 13;
int seg_two = 12;
int seg_three = 11;
int seg_four = 10;
int pot = A0;
int button = A1;
int led = 1;
int time;
boolean invert = false;
void setup(){
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}
void loop(){
int tmp = analogRead(pot);
time = tmp * 30 / 1024;
show(time);
if(digitalRead(button)) countDown(time);
}
void countDown(int counter){
for(int i=counter;i>0;i--){
for (int j=0; j<2000; j++) show(i);
}
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
}
void show(int num){
int unit = num % 10;
int tens = (num / 10) % 10;
int hundreds = (num / 100) % 10;
int thousands = (num / 1000) % 10;
displayNumber(unit,3);
delayMicroseconds(1250);
displayNumber(tens,2);
delayMicroseconds(1250);
displayNumber(hundreds,1);
delayMicroseconds(1250);
displayNumber(thousands,0);
delayMicroseconds(1250);
}
void displayNumber(int segment, int val){
switch(segment){
case 0: digitalWrite(seg_one,invert);
digitalWrite(seg_two,!invert);
digitalWrite(seg_three,!invert);
digitalWrite(seg_four,!invert);
break;
case 1: digitalWrite(seg_one,!invert);
digitalWrite(seg_two,invert);
digitalWrite(seg_three,!invert);
digitalWrite(seg_four,!invert);
break;
case 2: digitalWrite(seg_one,!invert);
digitalWrite(seg_two,!invert);
digitalWrite(seg_three,invert);
digitalWrite(seg_four,!invert);
break;
case 3: digitalWrite(seg_one,!invert);
digitalWrite(seg_two,!invert);
digitalWrite(seg_three,!invert);
digitalWrite(seg_four,invert);
break;
default:digitalWrite(seg_one,!invert);
digitalWrite(seg_two,!invert);
digitalWrite(seg_three,!invert);
digitalWrite(seg_four,!invert);
break;
}
digitSelect(val);
}
void digitSelect(int num){
switch(num){
case 0: zero();
break;
case 1: one();
break;
case 2: two();
break;
case 3: three();
break;
case 4: four();
break;
case 5: five();
break;
case 6: six();
break;
case 7: seven();
break;
case 8: eight();
break;
case 9: nine();
break;
}
}
void zero(){
digitalWrite(a,!invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,!invert);
digitalWrite(e,!invert);
digitalWrite(f,!invert);
digitalWrite(g,invert);
}
void one(){
digitalWrite(a,invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,invert);
digitalWrite(e,invert);
digitalWrite(f,invert);
digitalWrite(g,invert);
}
void two(){
digitalWrite(a,!invert);
digitalWrite(b,!invert);
digitalWrite(c,invert);
digitalWrite(d,!invert);
digitalWrite(e,!invert);
digitalWrite(f,invert);
digitalWrite(g,!invert);
}
void three(){
digitalWrite(a,!invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,!invert);
digitalWrite(e,invert);
digitalWrite(f,invert);
digitalWrite(g,!invert);
}
void four(){
digitalWrite(a,invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,invert);
digitalWrite(e,invert);
digitalWrite(f,!invert);
digitalWrite(g,!invert);
}
void five(){
digitalWrite(a,!invert);
digitalWrite(b,invert);
digitalWrite(c,!invert);
digitalWrite(d,!invert);
digitalWrite(e,invert);
digitalWrite(f,!invert);
digitalWrite(g,!invert);
}
void six(){
digitalWrite(a,!invert);
digitalWrite(b,invert);
digitalWrite(c,!invert);
digitalWrite(d,!invert);
digitalWrite(e,!invert);
digitalWrite(f,!invert);
digitalWrite(g,!invert);
}
void seven(){
digitalWrite(a,!invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,invert);
digitalWrite(e,invert);
digitalWrite(f,invert);
digitalWrite(g,invert);
}
void eight(){
digitalWrite(a,!invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,!invert);
digitalWrite(e,!invert);
digitalWrite(f,!invert);
digitalWrite(g,!invert);
}
void nine(){
digitalWrite(a,!invert);
digitalWrite(b,!invert);
digitalWrite(c,!invert);
digitalWrite(d,!invert);
digitalWrite(e,invert);
digitalWrite(f,!invert);
digitalWrite(g,!invert);
}
No comments:
Post a Comment