/*controlprog.c Shiels, Cook, Brewer Team 25 Mech 395 - Introduction to Mechatronics Control Software for Final Project Mechatronic Roulette Table */ /*Port Summary: Analog 5 - Wheel Voltage Input Analog 0 - Home Encoder Analog 1 - Shaft Encoder */ /*declare global variables - note that IC is stupid and doesn't always allow declarations midstream :( */ int i; int EncoderCount; int temp; int BETLIMIT = 5; int PhysicalPosition; int efflag; /* we use efflag to overcome the lack of elseif compatibility*/ int pid; /*process id for checking wheel position*/ int WinningPosition; int P1Score=0; int P2Score=0; int P3Score=0; int P4Score=0; int P1BetNum; int P2BetNum; int P3BetNum; int P4BetNum; int P1BetLocals[5]; int P2BetLocals[5]; int P3BetLocals[5]; int P4BetLocals[5]; int P1BetVals[5]; int P2BetVals[5]; int P3BetVals[5]; int P4BetVals[5]; /*note; get_button_press MUST HAVE a call to clear_ff on it*/ void Clear_ffs(void) { bit_clear(0x1000,0x80); sleep(.2); bit_set(0x1000,0x80); /*set clr input to 0 and then 1 to get rid of current flip-flop state*/ } void Clear_LEDs(void) { poke(0x1008, 3); } void Enable_LEDs(void) { poke(0x1008, 14); } void Monitor_Wheel_PS(void) { while(1) { if(analog(1)>100) { while(analog(1)>100) { } EncoderCount += 1; } printf("encoder count = %d\n",EncoderCount); if(analog(0) >100) { while(analog(0)>100) { } EncoderCount = 0; } } } void Send_Wheel_Home(void) { motor(0,50); while (analog(0) > 100) { } ao(); EncoderCount = 0; } int Get_Button_Press(void) { temp=0; while(temp==0) { temp=(digital(14)*16 + digital(13)*8 + digital(12)*4 +digital(11)*2 +digital(10)*1); } Clear_ffs(); sleep(1.0); return temp; } int Get_Wheel_Position(void) { /*RETURN WHEEL POSITION (1-10)*/ return EncoderCount+6; } int Get_Ball_Position(void) { temp = 0; temp = analog(5); if ((temp>=181) && (temp<=200)) PhysicalPosition=0; if ((temp>=201)&&(temp<=220)) PhysicalPosition=1; if ((temp>=221)&&(temp<=245)) PhysicalPosition=2; if ((temp>=246)&&(temp<=255)) PhysicalPosition=3; if((temp>=1) && (temp <= 20)) PhysicalPosition=4; if((temp>=31) && (temp <=70)) PhysicalPosition=5; if((temp>=71) && (temp <=100)) PhysicalPosition=6; if((temp>=101)&&(temp<=130)) PhysicalPosition=7; if((temp>=131)&&(temp<=155)) PhysicalPosition=8; if((temp>=156)&&(temp<=180)) PhysicalPosition=9; /*return Get_Wheel_Position()+6+PhysicalPosition;*/ temp = Get_Wheel_Position() + PhysicalPosition; if (temp <10) { /*printf("%d %d %d", Get_Wheel_Position(),PhysicalPosition,temp); sleep(5.0);*/ return temp; } if (temp >= 10) { temp -= 10; /*printf("%d %d %d", Get_Wheel_Position(),PhysicalPosition,temp); sleep(5.0);*/ return temp; } } void main(void) { printf("Welcome to MS Roulette!\n"); /*cause digital in 9 to be a digital out*/ bit_set(0x1026, 0x80); /*cause MOSI to be a digital out*/ poke(0x1009, 0x3c); while (!stop_button()) { /*1) Initialize Hardware: Set Reset Line to Turn off LED's Set Reset Line to clear ff's Send Wheel Home */ printf("clear leds\n"); Clear_LEDs(); printf("clear ffs\n"); Clear_ffs(); printf("home wheel\n"); Send_Wheel_Home(); /*2) Print Welcome Message */ beep(); sleep(.1); beep(); sleep(.1); beep(); printf("Welcome to MS Roulette!\n"); sleep(3.0); /*3) Take Bets Player n Decides on Number of Bets Player n Chooses Ammount for each bet Player n chooses square for each bet loop for 1<=n<=4 */ /* Player n Decides on Number of Bets */ printf("Player 1, How Many Bets?\n"); P1BetNum = Get_Button_Press(); while((P1BetNum < 1) || (P1BetNum > 5)) { printf("Sorry - Bad Selection!\n"); sleep(.75); printf("Player 1, How Many Bets?\n"); P1BetNum = Get_Button_Press(); } printf("Player 2, How Many Bets?\n"); P2BetNum = Get_Button_Press(); while((P2BetNum < 1) || (P2BetNum > 5)) { printf("Sorry - Bad Selection!\n"); sleep(.75); printf("Player 2, How Many Bets?\n"); P2BetNum = Get_Button_Press(); } printf("Player 3, How Many Bets?\n"); P3BetNum = Get_Button_Press(); while((P3BetNum < 1) || (P3BetNum > 5)) { printf("Sorry - Bad Selection!\n"); sleep(.75); printf("Player 3, How Many Bets?\n"); P3BetNum = Get_Button_Press(); } printf("Player 4, How Many Bets?\n"); P4BetNum = Get_Button_Press(); while((P4BetNum < 1) || (P4BetNum > 5)) { printf("Sorry - Bad Selection!\n"); sleep(.75); printf("Player 4, How Many Bets?\n"); P4BetNum = Get_Button_Press(); } /* Player n Chooses Ammount for each bet */ printf("Player 1, How Much Ya Bettin\'?\n"); for(i=0;i10) && (P1BetLocals[i]<14)) { if((P1BetLocals[i]-10)==1) { if(WinningPosition==1||4||7) { P1Score+=10*P1BetVals[i]; efflag=1; } } if((P1BetLocals[i]-10)==2) { if(WinningPosition==2||5||8) { P1Score+=10*P1BetVals[i]; efflag=1; } } if((P1BetLocals[i]-10)==3) { if(WinningPosition==3||6||9) { P1Score+=10*P1BetVals[i]; efflag=1; } } if(efflag==0) P1Score-=10*P1BetVals[i]; } } efflag=0; for(i=0;i10) && (P2BetLocals[i]<14)) { if((P2BetLocals[i]-10)==1) { if(WinningPosition==1||4||7) { P2Score+=10*P2BetVals[i]; efflag=1; } } if((P2BetLocals[i]-10)==2) { if(WinningPosition==2||5||8) { P2Score+=10*P2BetVals[i]; efflag=1; } } if((P2BetLocals[i]-10)==3) { if(WinningPosition==3||6||9) { P2Score+=10*P2BetVals[i]; efflag=1; } } if(efflag==0) P2Score-=10*P2BetVals[i]; } } efflag=0; for(i=0;i10) && (P3BetLocals[i]<14)) { if((P3BetLocals[i]-10)==1) { if(WinningPosition==1||4||7) { P3Score+=10*P3BetVals[i]; efflag=1; } } if((P3BetLocals[i]-10)==2) { if(WinningPosition==2||5||8) { P3Score+=10*P3BetVals[i]; efflag=1; } } if((P3BetLocals[i]-10)==3) { if(WinningPosition==3||6||9) { P3Score+=10*P3BetVals[i]; efflag=1; } } if(efflag==0) P3Score-=10*P3BetVals[i]; } } efflag=0; for(i=0;i10) && (P4BetLocals[i]<14)) { if((P4BetLocals[i]-10)==1) { if(WinningPosition==1||4||7) { P4Score+=10*P4BetVals[i]; efflag=1; } } if((P4BetLocals[i]-10)==2) { if(WinningPosition==2||5||8) { P4Score+=10*P4BetVals[i]; efflag=1; } } if((P4BetLocals[i]-10)==3) { if(WinningPosition==3||6||9) { P4Score+=10*P4BetVals[i]; efflag=1; } } if(efflag==0) P4Score-=10*P4BetVals[i]; } } efflag=0; /*Third, Compute Rows*/ for(i=0;i 13) && (P1BetLocals[i]<17)) { if((P1BetLocals[i]-13)==1) { if(WinningPosition==1||2||3) { P1Score+=10*P1BetVals[i]; efflag=1; } } if((P1BetLocals[i]-13)==2) { if(WinningPosition==4||5||6) { P1Score+=10*P1BetVals[i]; efflag=1; } } if((P1BetLocals[i]-13)==3) { if(WinningPosition==7||8||9) { P1Score+=10*P1BetVals[i]; efflag=1; } } if(efflag==0) P1Score-=10*P1BetVals[i]; } } efflag=0; for(i=0;i 13) && (P2BetLocals[i]<17)) { if((P2BetLocals[i]-13)==1) { if(WinningPosition==1||2||3) { P2Score+=10*P2BetVals[i]; efflag=1; } } if((P2BetLocals[i]-13)==2) { if(WinningPosition==4||5||6) { P2Score+=10*P2BetVals[i]; efflag=1; } } if((P2BetLocals[i]-13)==3) { if(WinningPosition==7||8||9) { P2Score+=10*P2BetVals[i]; efflag=1; } } if(efflag==0) P2Score-=10*P2BetVals[i]; } } efflag=0; for(i=0;i 13) && (P3BetLocals[i]<17)) { if((P3BetLocals[i]-13)==1) { if(WinningPosition==1||2||3) { P3Score+=10*P3BetVals[i]; efflag=1; } } if((P3BetLocals[i]-13)==2) { if(WinningPosition==4||5||6) { P3Score+=10*P3BetVals[i]; efflag=1; } } if((P3BetLocals[i]-13)==3) { if(WinningPosition==7||8||9) { P3Score+=10*P3BetVals[i]; efflag=1; } } if(efflag==0) P3Score-=10*P3BetVals[i]; } } efflag=0; for(i=0;i 13) && (P4BetLocals[i]<17)) { if((P4BetLocals[i]-13)==1) { if(WinningPosition==1||2||3) { P4Score+=10*P4BetVals[i]; efflag=1; } } if((P4BetLocals[i]-13)==2) { if(WinningPosition==4||5||6) { P4Score+=10*P4BetVals[i]; efflag=1; } } if((P4BetLocals[i]-13)==3) { if(WinningPosition==7||8||9) { P4Score+=10*P4BetVals[i]; efflag=1; } } if(efflag==0) P4Score-=10*P4BetVals[i]; } } efflag=0; /*Fourth, Compute Red*/ for(i=0;i