Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ipst.h> // IPST-SE Board
- //void setup() {
- //
- //}
- //
- //void loop() {
- //servo(0,90);
- //servo(1,90);
- //servo(2,90);
- //delay(2000);
- //servo(0,25);
- //servo(1,25);
- //servo(2,25);
- //delay(2000);
- //}
- #define ID_NUM1 0
- #define ID_NUM2 1
- #define ID_NUM3 2
- //const int analogInputPin = A2;
- const int anColorPin = A1;
- //const int anDistancePin = A3;
- const int openCMLEDPin = 16;
- const int youLEDPin = 18;
- const int Sw1RunPin = 30;
- const int Sw2RunPin = 28;
- int numRandom = 0, st = 0, se = 0;
- //char * results = "";
- //char * StatusText = "";
- void setup() {
- pinMode(openCMLEDPin, OUTPUT);
- pinMode(youLEDPin, OUTPUT);
- pinMode(Sw1RunPin, INPUT);
- pinMode(Sw2RunPin, INPUT);
- Serial.begin(9600);
- glcdMode(1);
- //setTextSize(3);
- // glcd(3, 0, "You ");
- }
- void loop() {
- // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- // int analogValue = analogRead(analogInputPin);
- int colorValue = analogRead(anColorPin);
- // int distanceValue = analogRead(anDistancePin);
- // analogValue=map(analogValue,0,4025,0,1023);
- //lose//แพ้
- //win//ชนะ
- //draw //เสมอ
- // se=selectStep(colorValue);
- //Serial.print(colorValue);
- // Serial.print(":>");
- // Serial.println(selectStep(colorValue));
- //glcdClear();
- int selectMode= map(knob(),-1,1025,0,4);
- // glcd(3, 4, results);
- se = selectStep(colorValue);
- // if(se>=0){
- if (!digitalRead(Sw1RunPin) || !digitalRead(Sw2RunPin))
- {
- if(selectMode==1){
- // setTextColor(GLCD_RED);
- if (se == 0){
- numRandom=1;
- }else if (se == 1){
- numRandom=2;
- }else if (se == 2){
- numRandom=0;
- }
- }else if(selectMode==2){
- //setTextColor(GLCD_GREEN);
- if (se == 0){
- numRandom=2;
- }else if (se == 1){
- numRandom=0;
- }else if (se == 2){
- numRandom=1;
- }
- }else if(selectMode==3){
- //setTextColor(GLCD_YELLOW);
- if (se == 0){
- numRandom=0;
- }else if (se == 1){
- numRandom=1;
- }else if (se == 2){
- numRandom=2;
- }
- }
- controlMotor(numRandom);
- if (numRandom == 0)
- {
- if (se == 0)
- {
- setTextSize(4);
- glcdFillScreen(GLCD_WHITE);
- setTextBackgroundColor(GLCD_WHITE);
- setTextColor(GLCD_BROWN);
- glcdFillRect(5,80,145,10,GLCD_BROWN);
- glcd(1, 1, "DRAW! ");
- digitalWrite(openCMLEDPin, HIGH);
- digitalWrite(youLEDPin, HIGH);
- }
- else if (se == 1)
- {
- setTextSize(4);
- glcdFillScreen(GLCD_GREEN);
- setTextBackgroundColor(GLCD_GREEN);
- setTextColor(GLCD_VIOLET);
- glcdFillRect(5,80,145,10,GLCD_VIOLET);
- glcd(1, 1, "WIN!! ");
- digitalWrite(openCMLEDPin, LOW);
- digitalWrite(youLEDPin, HIGH);
- }
- else if (se == 2)
- {
- glcdFillScreen(GLCD_RED);
- setTextBackgroundColor(GLCD_RED);
- setTextSize(4);
- glcdFillRect(5,80,145,10,GLCD_YELLOW);
- setTextColor(GLCD_YELLOW);
- glcd(1, 1, "LOSE!");
- digitalWrite(openCMLEDPin, HIGH);
- digitalWrite(youLEDPin, LOW);
- }
- }
- else if (numRandom == 1)
- {
- if (se == 1)
- {
- setTextSize(4);
- glcdFillScreen(GLCD_WHITE);
- setTextBackgroundColor(GLCD_WHITE);
- glcdFillRect(5,80,145,10,GLCD_BROWN);
- setTextColor(GLCD_BROWN);
- glcd(1, 1, "DRAW!");
- digitalWrite(openCMLEDPin, HIGH);
- digitalWrite(youLEDPin, HIGH);
- }
- else if (se == 2)
- {
- setTextSize(4);
- glcdFillScreen(GLCD_GREEN);
- setTextBackgroundColor(GLCD_GREEN);
- setTextColor(GLCD_VIOLET);
- glcdFillRect(5,80,145,10,GLCD_VIOLET);
- glcd(1, 1,"WIN!! ");
- digitalWrite(openCMLEDPin, LOW);
- digitalWrite(youLEDPin, HIGH);
- }
- else if (se == 0)
- {
- glcdFillScreen(GLCD_RED);
- setTextBackgroundColor(GLCD_RED);
- setTextSize(4);
- setTextColor(GLCD_YELLOW);
- glcdFillRect(5,80,145,10,GLCD_YELLOW);
- glcd(1, 1, "LOSE!");
- digitalWrite(openCMLEDPin, HIGH);
- digitalWrite(youLEDPin, LOW);
- }
- }
- else if (numRandom == 2)
- {
- if (se == 2)
- {
- setTextSize(4);
- glcdFillScreen(GLCD_WHITE);
- setTextBackgroundColor(GLCD_WHITE);
- glcdFillRect(5,80,145,10,GLCD_BROWN);
- setTextColor(GLCD_BROWN);
- glcd(1, 1, "DRAW!");
- digitalWrite(openCMLEDPin, HIGH);
- digitalWrite(youLEDPin, HIGH);
- }
- else if (se == 0)
- {
- setTextSize(4);
- glcdFillScreen(GLCD_GREEN);
- setTextBackgroundColor(GLCD_GREEN);
- glcdFillRect(5,80,145,10,GLCD_VIOLET);
- setTextColor(GLCD_VIOLET);
- glcd(1, 1,"WIN!! ");
- digitalWrite(openCMLEDPin, LOW);
- digitalWrite(youLEDPin, HIGH);
- }
- else if (se == 1)
- {
- glcdFillScreen(GLCD_RED);
- setTextBackgroundColor(GLCD_RED);
- setTextSize(4);
- setTextColor(GLCD_YELLOW);
- glcdFillRect(5,80,145,10,GLCD_YELLOW);
- glcd(1, 1, "LOSE!");
- digitalWrite(openCMLEDPin, HIGH);
- digitalWrite(youLEDPin, LOW);
- }
- }
- st = 1;
- }
- else
- {
- glcdFillScreen(GLCD_BLACK);
- setTextBackgroundColor(GLCD_BLACK);
- if(selectMode==0){
- setTextColor(GLCD_WHITE);
- }else if(selectMode==1){
- setTextColor(GLCD_RED);
- }else if(selectMode==2){
- setTextColor(GLCD_GREEN);
- }else if(selectMode==3){
- setTextColor(GLCD_YELLOW);
- }
- setTextSize(2);
- glcd(3, 3, "TAP CARD");
- controlMotor(-1);
- numRandom = random(3);
- digitalWrite(openCMLEDPin, LOW);
- digitalWrite(youLEDPin, LOW);
- }
- }
- void controlMotor(int step)
- {
- if (step == 0)
- {
- servo(ID_NUM1, 115);
- delay(100);
- }
- else if (step == 1)
- {
- servo(ID_NUM2, 90);
- delay(100);
- // Dxl.goalPosition(ID_NUM2,0);
- }
- else if (step == 2)
- {
- servo(ID_NUM3, 115);
- delay(100);
- // Dxl.goalPosition(ID_NUM3,0);
- }
- else
- {
- if (st)
- {
- servo(ID_NUM1, 25);
- servo(ID_NUM2, 25);
- servo(ID_NUM3, 25);
- delay(1000);
- st = 0;
- }
- }
- }
- int selectStep(int colorVal) {
- int step = 0;
- if (colorVal > 890 && colorVal < 1090) { // 990
- step = 0;
- } else if (colorVal > 350 && colorVal < 550) { // 450
- step = 1;
- } else if (colorVal >= 160 && colorVal < 360) { // 260
- step = 2;
- } else {
- step = -1;
- }
- return step;
- }
Add Comment
Please, Sign In to add comment