KRITSADA

IPST-SE Rock Paper Scisors Games

Aug 30th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.64 KB | None | 0 0
  1. #include <ipst.h>   // IPST-SE Board
  2. //void setup() {
  3. //
  4. //}
  5. //
  6. //void loop() {
  7. //servo(0,90);
  8. //servo(1,90);
  9. //servo(2,90);
  10. //delay(2000);
  11. //servo(0,25);
  12. //servo(1,25);
  13. //servo(2,25);
  14. //delay(2000);
  15. //}
  16.  
  17.  
  18. #define ID_NUM1 0
  19. #define ID_NUM2 1
  20. #define ID_NUM3 2
  21. //const int analogInputPin = A2;
  22. const int anColorPin = A1;
  23. //const int anDistancePin = A3;
  24. const int openCMLEDPin = 16;
  25. const int youLEDPin = 18;
  26. const int Sw1RunPin = 30;
  27. const int Sw2RunPin = 28;
  28. int numRandom = 0, st = 0, se = 0;
  29. //char * results = "";
  30. //char * StatusText = "";
  31. void setup() {
  32.   pinMode(openCMLEDPin, OUTPUT);
  33.   pinMode(youLEDPin, OUTPUT);
  34.   pinMode(Sw1RunPin, INPUT);
  35.   pinMode(Sw2RunPin, INPUT);
  36.   Serial.begin(9600);
  37.   glcdMode(1);
  38.   //setTextSize(3);
  39.   // glcd(3, 0, "You ");
  40.  
  41. }
  42.  
  43. void loop() {
  44.   // set the cursor to column 0, line 1
  45.   // (note: line 1 is the second row, since counting begins with 0):
  46.   //  int analogValue = analogRead(analogInputPin);
  47.   int colorValue = analogRead(anColorPin);
  48.   //  int distanceValue = analogRead(anDistancePin);
  49.   //  analogValue=map(analogValue,0,4025,0,1023);
  50.   //lose//แพ้
  51.   //win//ชนะ
  52.   //draw //เสมอ
  53.   // se=selectStep(colorValue);
  54.   //Serial.print(colorValue);
  55.   // Serial.print(":>");
  56.   // Serial.println(selectStep(colorValue));
  57.   //glcdClear();
  58.    int selectMode= map(knob(),-1,1025,0,4);
  59.   // glcd(3, 4, results);
  60.   se = selectStep(colorValue);
  61.   // if(se>=0){
  62.   if (!digitalRead(Sw1RunPin) || !digitalRead(Sw2RunPin))
  63.   {
  64.      if(selectMode==1){
  65.      // setTextColor(GLCD_RED);
  66.        if (se == 0){
  67.          numRandom=1;
  68.        }else if (se == 1){
  69.          numRandom=2;
  70.        }else if (se == 2){
  71.          numRandom=0;
  72.        }
  73.     }else if(selectMode==2){
  74.       //setTextColor(GLCD_GREEN);
  75.       if (se == 0){
  76.          numRandom=2;
  77.        }else if (se == 1){
  78.          numRandom=0;
  79.        }else if (se == 2){
  80.          numRandom=1;
  81.        }
  82.     }else if(selectMode==3){
  83.       //setTextColor(GLCD_YELLOW);
  84.       if (se == 0){
  85.          numRandom=0;
  86.        }else if (se == 1){
  87.          numRandom=1;
  88.        }else if (se == 2){
  89.          numRandom=2;
  90.        }
  91.     }
  92.     controlMotor(numRandom);
  93.     if (numRandom == 0)
  94.     {
  95.       if (se == 0)
  96.       {
  97.         setTextSize(4);
  98.         glcdFillScreen(GLCD_WHITE);
  99.         setTextBackgroundColor(GLCD_WHITE);
  100.         setTextColor(GLCD_BROWN);
  101.         glcdFillRect(5,80,145,10,GLCD_BROWN);
  102.         glcd(1, 1, "DRAW! ");
  103.         digitalWrite(openCMLEDPin, HIGH);
  104.         digitalWrite(youLEDPin, HIGH);
  105.       }
  106.       else if (se == 1)
  107.       {
  108.         setTextSize(4);
  109.         glcdFillScreen(GLCD_GREEN);
  110.         setTextBackgroundColor(GLCD_GREEN);
  111.         setTextColor(GLCD_VIOLET);
  112.         glcdFillRect(5,80,145,10,GLCD_VIOLET);
  113.         glcd(1, 1, "WIN!! ");
  114.         digitalWrite(openCMLEDPin, LOW);
  115.         digitalWrite(youLEDPin, HIGH);
  116.       }
  117.       else if (se == 2)
  118.       {
  119.         glcdFillScreen(GLCD_RED);
  120.         setTextBackgroundColor(GLCD_RED);
  121.         setTextSize(4);
  122.         glcdFillRect(5,80,145,10,GLCD_YELLOW);
  123.         setTextColor(GLCD_YELLOW);
  124.         glcd(1, 1, "LOSE!");
  125.         digitalWrite(openCMLEDPin, HIGH);
  126.         digitalWrite(youLEDPin, LOW);
  127.       }
  128.     }
  129.     else if (numRandom == 1)
  130.     {
  131.       if (se == 1)
  132.       {
  133.         setTextSize(4);
  134.         glcdFillScreen(GLCD_WHITE);
  135.         setTextBackgroundColor(GLCD_WHITE);
  136.         glcdFillRect(5,80,145,10,GLCD_BROWN);
  137.         setTextColor(GLCD_BROWN);
  138.         glcd(1, 1, "DRAW!");
  139.         digitalWrite(openCMLEDPin, HIGH);
  140.         digitalWrite(youLEDPin, HIGH);
  141.       }
  142.       else if (se == 2)
  143.       {
  144.         setTextSize(4);
  145.         glcdFillScreen(GLCD_GREEN);
  146.         setTextBackgroundColor(GLCD_GREEN);
  147.         setTextColor(GLCD_VIOLET);
  148.         glcdFillRect(5,80,145,10,GLCD_VIOLET);
  149.         glcd(1, 1,"WIN!! ");
  150.         digitalWrite(openCMLEDPin, LOW);
  151.         digitalWrite(youLEDPin, HIGH);
  152.       }
  153.       else if (se == 0)
  154.       {
  155.         glcdFillScreen(GLCD_RED);
  156.         setTextBackgroundColor(GLCD_RED);
  157.         setTextSize(4);
  158.         setTextColor(GLCD_YELLOW);
  159.         glcdFillRect(5,80,145,10,GLCD_YELLOW);
  160.         glcd(1, 1, "LOSE!");
  161.         digitalWrite(openCMLEDPin, HIGH);
  162.         digitalWrite(youLEDPin, LOW);
  163.       }
  164.     }
  165.     else if (numRandom == 2)
  166.     {
  167.       if (se == 2)
  168.       {
  169.         setTextSize(4);
  170.         glcdFillScreen(GLCD_WHITE);
  171.         setTextBackgroundColor(GLCD_WHITE);
  172.         glcdFillRect(5,80,145,10,GLCD_BROWN);
  173.         setTextColor(GLCD_BROWN);
  174.         glcd(1, 1, "DRAW!");
  175.         digitalWrite(openCMLEDPin, HIGH);
  176.         digitalWrite(youLEDPin, HIGH);
  177.       }
  178.       else if (se == 0)
  179.       {
  180.         setTextSize(4);
  181.         glcdFillScreen(GLCD_GREEN);
  182.         setTextBackgroundColor(GLCD_GREEN);
  183.         glcdFillRect(5,80,145,10,GLCD_VIOLET);
  184.         setTextColor(GLCD_VIOLET);
  185.         glcd(1, 1,"WIN!! ");
  186.         digitalWrite(openCMLEDPin, LOW);
  187.         digitalWrite(youLEDPin, HIGH);
  188.       }
  189.       else if (se == 1)
  190.       {
  191.         glcdFillScreen(GLCD_RED);
  192.         setTextBackgroundColor(GLCD_RED);
  193.         setTextSize(4);
  194.         setTextColor(GLCD_YELLOW);
  195.         glcdFillRect(5,80,145,10,GLCD_YELLOW);
  196.         glcd(1, 1, "LOSE!");
  197.         digitalWrite(openCMLEDPin, HIGH);
  198.         digitalWrite(youLEDPin, LOW);
  199.       }
  200.     }
  201.     st = 1;
  202.   }
  203.   else
  204.   {
  205.     glcdFillScreen(GLCD_BLACK);
  206.     setTextBackgroundColor(GLCD_BLACK);
  207.     if(selectMode==0){
  208.       setTextColor(GLCD_WHITE);
  209.     }else if(selectMode==1){
  210.       setTextColor(GLCD_RED);
  211.     }else if(selectMode==2){
  212.       setTextColor(GLCD_GREEN);
  213.     }else if(selectMode==3){
  214.       setTextColor(GLCD_YELLOW);
  215.     }
  216.     setTextSize(2);
  217.     glcd(3, 3, "TAP CARD");
  218.     controlMotor(-1);
  219.     numRandom = random(3);
  220.     digitalWrite(openCMLEDPin, LOW);
  221.     digitalWrite(youLEDPin, LOW);
  222.   }
  223. }
  224.  
  225.  
  226. void controlMotor(int step)
  227. {
  228.   if (step == 0)
  229.   {
  230.     servo(ID_NUM1, 115);
  231.     delay(100);
  232.   }
  233.   else if (step == 1)
  234.   {
  235.     servo(ID_NUM2, 90);
  236.     delay(100);
  237.     // Dxl.goalPosition(ID_NUM2,0);
  238.   }
  239.   else if (step == 2)
  240.   {
  241.     servo(ID_NUM3, 115);
  242.     delay(100);
  243.     // Dxl.goalPosition(ID_NUM3,0);
  244.   }
  245.   else
  246.   {
  247.     if (st)
  248.     {
  249.       servo(ID_NUM1, 25);
  250.       servo(ID_NUM2, 25);
  251.       servo(ID_NUM3, 25);
  252.       delay(1000);
  253.       st = 0;
  254.     }
  255.   }
  256. }
  257.  
  258.  
  259.  
  260. int selectStep(int colorVal) {
  261.   int step = 0;
  262.   if (colorVal > 890 && colorVal < 1090) { // 990
  263.     step = 0;
  264.   } else if (colorVal > 350 && colorVal < 550) { // 450
  265.     step = 1;
  266.   } else if (colorVal >= 160 && colorVal < 360) { // 260
  267.     step = 2;
  268.   } else {
  269.     step = -1;
  270.   }
  271.   return step;
  272. }
Add Comment
Please, Sign In to add comment