This is a example of how to show a digit to a 7-segment.
This example display a digit, which is sent from computer on one of three 7-segments.
To do it, you need
Arduino
Arduino program
Computer
Three 7-segments
Eight 220 Ohm resistors
Lot of wires (more than 20)
Circuit
Basically 7-segment is a bunch of LEDs. You need to put a resistor as the same as regular LED.
You have to connect Anode pins of the 7-segments to digital pin number from 2 to 13, no 1 or2 .
You need to connect each 7-segment in parallel.
In the case of my 7-segment, there is two common cathodes on the 7-segment.
We do not need two, but one of them and connect it to a ground for the case of mine.
If you have a different 7-segment you may have to connect differently. If so, you cannot use program on this tutorial.
However, instead of using a regular ground we will use regular digital pin as a ground.
We will turn several digital pins to ground with a program. It will enable us to control three 7-segments respectively.
Program
you may need to change some cord for your circuit.
I messed up my circuit so that this program is little bit messed up.
//reguler variable
int LEDpin = 13;
int c;
int seg1=10;
int seg2=11;
int seg3=12;
//controller variables
int refleshT=10;
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){
c=Serial.read();
Serial.print("get Serial");
Serial.println(c,BYTE);
if(c=='8'){
reset();
pinMode(seg3,OUTPUT);
eight();
}
else if(c=='0'){
reset();
pinMode(seg2,OUTPUT);
zero();
}
else if(c=='1'){
reset();
pinMode(seg1,OUTPUT);
one();
}
else if(c=='2'){
reset();
pinMode(seg1,OUTPUT);
two();
}
else if(c=='3'){
reset();
pinMode(seg3,OUTPUT);
three();
}
else if(c=='4'){
reset();
pinMode(seg2,OUTPUT);
four();
}
else if(c=='5'){
reset();
pinMode(seg1,OUTPUT);
five();
}
else if(c=='6'){
reset();
pinMode(seg2,OUTPUT);
six();
}
else if(c=='7'){
reset();
pinMode(seg2,OUTPUT);
seven();
}
else if(c=='9'){
reset();
pinMode(seg3,OUTPUT);
nine();
}
else{
for (int i=10; i<13; ++i){
Serial.print("reset");
pinMode(i,INPUT);
Serial.print(i);
Serial.println("pin");
}
nothing();
}
}
}
//The following commends are needed to modify for your circuit
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);
}
}
0 件のコメント:
コメントを投稿