https://www.youtube.com/watch?v=CD7c5qWti5M
โค้ดส่งฟรีShopee https://shope.ee/6ze2MNCRiS
PJ#3 เกมทายตัวเลข สุ่มตัวเลข 0-9 แบบยุติธรรม
Random 0-9 Fair
8/4/2565 SONGCHAI PRAPATRUNGSEE
/*songDIY
* 27/3/2022
* Radom 0-9
*/
//int num[]={0x3F, 0x06, 0x5B, 0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//For proteus
int num[]={0x77, 0x42, 0x6D, 0x6B,0x5A,0x3B,0x1F,0x62,0x7F,0x7B};//For 5611A Common Cathode
int i,ib,j,time_n=30,x,swL,swT,blinkT,blinkL,blinkF;
int numL;
void beep();
void setup() {
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // TCCR1A register = 0
TCCR1B = 0; // TCCR1A register = 0
TIMSK1 |= (1 << TOIE1);// enable Timer1 overflow interrupt:
TCNT1=64286;// Preload 20mS
TCCR1B |= (1 << CS12); // Sets bit CS12 in TCCR1B
sei();// enable global interrupts:
// initialize digital pin .
DDRD = 0xFF; // 0-7 OUTPUT
DDRB = 0xFF;
DDRC = 0xFF;
pinMode(A4, INPUT_PULLUP);
pinMode(A5, OUTPUT);
}
//Timer1 overflow interrupt
ISR(TIMER1_OVF_vect){
TCNT1=64286;// reload 20mS
if(digitalRead(A4)==0&&swL==0){
if(swT++>=2){
swL=1;
swT=0;
blinkF=1;
numL=1;
}
}else if(digitalRead(A4)==1){
swL=0;
swT=0;
}
if(blinkF==0){
blinkT++;
if(blinkT==25&&blinkL==0){
blinkL=1;
blinkT=0;
PORTD=255;
}
if(blinkT==25&&blinkL==1){
blinkL=0;
blinkT=0;
PORTD=0;
}
}
}
// the loop function runs over and over again forever
void loop() {
//ib=0;
x=0;
while(numL==0){
delay(100);
time_n++;
if(time_n>30)time_n=21;
}
for(i=0;i<time_n;i++){
PORTD = num[ib];
beep();
delay(x);
x=x+10;
ib++;
if(ib>9)ib=0;
numL=0;
}
}
void beep(){
int i_b;
for(i_b=0;i_b<5;i_b++){
digitalWrite(A5,HIGH);
delayMicroseconds(1000);
digitalWrite(A5,LOW);
delayMicroseconds(1000);}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น