Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ipst.h> // IPST-SE Board
- word L, R, A, B, C,BAR, i;
- word whiteBrown = 700, blackBrown = 300, ref = 455, WHITE = 0, BLACK = 0, BROWN = 0;
- word N = 53, E = 122, S = 194, W = 300;
- void check()
- {
- glcdClear();
- compass_readConfig();
- while (1)
- {
- glcd(1, 1, "R= %d ", analog(1));
- glcd(2, 1, "L= %d ", analog(0));
- }
- }
- void setup() {
- compass_readConfig();
- compass_factoryReset();
- if(sw1()){
- while(sw1());
- setTextSize(2);glcdMode(3);
- glcd(0,0,"ON BLACK");
- glcd(1,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- for(i=0;i<20;i++){BLACK=analog(0)+BLACK;}BLACK=BLACK/20;
- glcd(2,0,"Black=%d",BLACK);delay(2000);setTextColor(GLCD_YELLOW);
- glcd(0,0,"ON WHITE");
- glcd(1,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- for(i=0;i<20;i++){WHITE=analog(0)+WHITE;}WHITE=WHITE/20;
- glcd(2,0,"WHITE=%d",WHITE);delay(2000);setTextColor(GLCD_BLUE);
- glcd(0,0,"ON BROWN");
- glcd(1,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- for(i=0;i<20;i++){BROWN=analog(0)+BROWN;}BROWN=BROWN/20;
- glcd(2,0,"BROWN=%d",BROWN);delay(2000);setTextColor(GLCD_WHITE);
- whiteBrown=(WHITE+BROWN)/2;
- glcd(3,0,"RefH=%d",whiteBrown);
- ref=(WHITE+BLACK)/2;
- glcd(4,0,"RefM=%d",ref);
- blackBrown=(BLACK+BROWN)/2;
- glcd(5,0,"RefL=%d",blackBrown);delay(2000);
- glcd(0,0,"COMPASS");
- glcd(1,0,"ON NORTH ");
- glcd(2,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- N=compass_read();
- glcd(3,0,"NORTH %d",N);delay(2000);
- glcd(0,0,"COMPASS");
- glcd(1,0,"ON EAST ");
- glcd(2,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- E=compass_read();
- glcd(3,0,"EAST %d",E);delay(2000);
- glcd(0,0,"COMPASS");
- glcd(1,0,"ON SOUTH ");
- glcd(2,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- S=compass_read();
- glcd(3,0,"SOUTH %d",S);delay(2000);
- glcd(0,0,"COMPASS");
- glcd(1,0,"ON WEST ");
- glcd(2,0,"press OK()");
- sw_OK_press();
- while(sw_OK());
- W=compass_read();
- glcd(3,0,"SOUTH %d",W);delay(2000);
- setTextColor(GLCD_RED);
- glcd(0,0,"SETUP ");
- glcd(1,0,"COMPLETE ");
- glcd(2,0,"press OK() ");
- glcd(3,0,"to START ");
- glcd(4,0,"____________"); delay(1000);
- }
- OK();
- barCode();fd(30);delay(300);
- if(A){R_first();}
- else {L_first();}
- }
- void loop() {
- }
- void L_first() {
- LLL(); // เจอแยกเลี้ยวซ้าย
- SSS(); // เจอสี่ออนกว่าสีดำหยุด
- barCode(); // อ่านบาร์โค้ด
- if (A) {
- fire13(); // ดับไฟดวง 1 และ 3
- }
- else {
- fire24(); // ดับไฟดวง 2 และ 4
- }
- MazeLeft(); // เดินในเขาวงกต
- LLL(); // เจอแยกเลี้ยวซ้าย
- SSS(); // เจอแยกหยุด
- }
- void R_first() {
- RRR();
- trackUntilWall();
- MazeRight();
- barCode();
- if (A) {
- fire31();
- }
- else {
- fire24();
- }
- RRR();
- SSS();
- }
- void trackUntilWall() {
- while (in(27) && in(30)) {
- L = analog(0);
- R = analog(1);
- if (L < ref && R < ref) {
- fd2(30, 33);
- }
- else if (L > ref && R < ref) {
- sl(40);
- }
- else if (L < ref && R > ref) {
- sr(40);
- }
- }
- }
- void MazeRight() {
- trackWallCompass(N);
- bk(30); delay(200); ao();
- R90(W);
- trackWallCompass(W); // ใข่ใบที่ 1
- bk(30); delay(1000); ao();
- trackTimeBackCompass(W, 1000);
- L90(S);
- trackWallCompass(S); // ใข่ใบที่ 2
- trackWallBackCompass(S);
- fd(30); delay(200); ao();
- L90(E);
- trackWallCompass(E); // ใข่ใบที่ 3
- trackWallBackCompass(E);
- fd(30); delay(250); ao();
- R90(S);
- trackWallCompass(S);
- bk(30); delay(250); ao();
- R90(W);
- trackWallCompass(W);
- bk(30); delay(250); ao();
- L90(N);
- trackWallCompass(N); // ใข่ใบที่ 4
- trackWallBackCompass(N);
- fd(30); delay(250); ao();
- R90(E);
- trackTimeCompass(E, 1500);
- R90(S);
- trackWallCompass(S);
- bk(30); delay(250); ao();
- R90(W);
- trackWallCompass(W);
- bk(30); delay(250); ao();
- L90(S);
- trackWallCompass(S); // ใขใบที่ 5
- trackWallBackCompass(S);
- fd(30); delay(250); ao();
- L90(E);
- trackWallCompass(E);
- bk(30); delay(250); ao();
- R90(S);
- trackWallCompass(S);
- bk(30); delay(250); ao();
- R90(W);
- trackTimeCompass(W, 1900);
- L90(S);
- trackTimeCompass(S, 1500);
- }
- void fire31() {
- fd(30); delay(200);
- trackTimeCompass(S, 2000);
- R90(S + 37);
- trackTimeCompass(S + 37, 500);
- fire(2000);
- bk(30); delay(500);
- L90(E - 37);
- trackTimeCompass(E - 37, 1000);
- fire(2000);
- bk(30); delay(600);
- R90(E);
- trackTimeCompass(E, 2400);
- RRR();
- }
- void fire42() {
- fd(30); delay(200);
- trackTimeCompass(S, 2000);
- L90(S - 42);
- trackTimeCompass(S - 42, 500);
- fire(2000);
- bk(30); delay(800);
- L90(N - 20);
- trackTimeCompass(N - 20, 800);
- fire(2000);
- bk(30); delay(600);
- R90(E);
- trackTimeCompass(E, 2400);
- RRR();
- }
- void MazeLeft() {
- bk(30); delay(100);
- trackWallCompass(N);
- bk(30); delay(300); ao();
- R90(E);
- trackWallCompass(E);
- bk(30); delay(300); ao();
- L90(N);
- trackWallCompass(N);
- bk(30); delay(250); ao();
- R90(W);
- trackWallCompass(W);
- bk(30); delay(300); ao();
- L90(S);
- trackWallCompass(S); // ใข่ใบที่ 1
- trackWallBackCompass(S);
- fd(30); delay(250); ao();
- L90(E);
- trackTimeCompass(E, 1000);
- L90(N);
- trackWallCompass(N);
- bk(30); delay(150); ao();
- R90(W);
- trackWallCompass(W);
- bk(30); delay(100); ao();
- L90(N);
- trackWallCompass(N); // ใข่ใบที่ 2
- trackWallBackCompass(N);
- fd(30); delay(250); ao();
- R90(E);
- trackWallCompass(E);
- bk(30); delay(100); ao();
- L90(N);
- trackWallCompass(N);
- bk(30); delay(150); ao();
- R90(E);
- trackWallCompass(E); // ใข่ใบที่ 3
- trackTimeBackCompass(E, 1000);
- R90(S);
- trackWallCompass(S);
- bk(30); delay(150); ao();
- R90(W);
- trackWallCompass(W); // ใข่ใบที่ 4
- bk(30); delay(1000); ao();
- trackTimeBackCompass(W, 1000);
- L90(S);
- trackWallCompass(S);
- bk(30); delay(1000); ao(); // ใข่ใบที่ 5
- L90(E);
- trackWallCompass(E);
- bk(30); delay(100); ao();
- R90(S);
- trackTimeCompass(S, 3000);
- LLL(); ao();
- }
- void R90(int c) {
- compass_readConfig();
- while (compass_read() < c) {
- sr(30);
- }
- }
- void L90(int c) {
- compass_readConfig();
- while (compass_read() > c) {
- sl(30);
- }
- }
- void trackWallCompass(int c) { //แทร็กเข็มทิศ
- compass_readConfig();
- while (in(27) && in(30)) {
- if (compass_read() <= c + 2 && compass_read() >= c - 2) {
- fd(30);
- }
- else if (compass_read() < c + 2) {
- tr(30);
- }
- else if (compass_read() > c - 2) {
- tl(30);
- }
- } ao();
- }
- void trackTimeCompass(int c, long t) { //แทร็กเข็มทิศ
- long m = millis();
- compass_readConfig();
- while (millis() < (m + t)) {
- if (compass_read() <= c + 2 && compass_read() >= c - 2) {
- fd(30);
- }
- else if (compass_read() < c + 2) {
- tr(30);
- }
- else if (compass_read() > c - 2) {
- tl(30);
- }
- } ao();
- }
- void trackTimeBackCompass(int c, long t) { //แทร็กเข็มทิศ หยุดเมื่อหมดเวลา
- long m = millis();
- compass_readConfig();
- while (millis() < (m + t)) {
- if (compass_read() <= c + 2 && compass_read() >= c - 2) {
- bk(30);
- }
- else if (compass_read() < c + 2) {
- bk2(0, 30);
- }
- else if (compass_read() > c - 2) {
- bk2(30, 0);
- }
- } ao();
- }
- void trackWallBackCompass(int c) { //แทร็กเข็มทิศ หยุดเมื่อเจอกำแพง
- compass_readConfig();
- while (in(14) && in(15)) {
- if (compass_read() <= c + 2 && compass_read() >= c - 2) {
- bk(30);
- }
- else if (compass_read() < c + 2) {
- bk2(0, 30);
- }
- else if (compass_read() > c - 2) {
- bk2(30, 0);
- }
- } ao();
- }
- void SSS() { // เดินตามเส้นห หยุดเมื่อเจอ ทั้งคู่เจอขาว
- while (1) {
- L = analog(0);
- R = analog(1);
- if (L < ref && R < ref) {
- fd2(30, 33);
- }
- else if (L > ref && R < ref) {
- sl(40);
- }
- else if (L < ref && R > ref) {
- sr(40);
- }
- else if (L > ref && R > ref) {
- ao(); break;
- }
- }
- }
- void FFF() {
- while (1) {
- L = analog(0); R = analog(1);
- if (L < ref && R < ref) {
- fd2(30, 33);
- }
- else if (L > ref && R < ref) {
- sl(40);
- }
- else if (L < ref && R > ref) {
- sr(40);
- }
- else if (L > ref && R > ref) {
- fd(30); delay(200);
- break;
- }
- }
- }
- void LLL() {
- while (1) {
- L = analog(0);
- R = analog(1);
- if (L < ref && R < ref) {
- fd2(30, 33);
- }
- else if (L > ref && R < ref) {
- sl(40);
- }
- else if (L < ref && R > ref) {
- sr(40);
- }
- else if (L > ref && R > ref) {
- fd(30); delay(200);
- while (analog(0) < ref) {
- sl(40);
- }
- while (analog(0) > ref) {
- sl(40);
- }
- break;
- }
- }
- }
- void fire13() {
- fd(30); delay(200);
- while (analog(0) < ref) {
- fd2(30, 29);
- }
- FFF();
- trackTime(600);
- fire(2000);
- while (analog(0) < ref) {
- sl(40);
- }
- while (analog(0) > ref) {
- sl(40);
- }
- FFF();
- trackTime(600);
- fire(2000);
- bk(30); delay(500);
- sl(30); delay(225);
- fd(30); delay(1000);
- trackUntilWall(); ao();
- }
- void fire24() {
- fd(30); delay(200);
- while (analog(0) < ref) {
- fd2(40, 35);
- }
- LLL();
- trackTime(600);
- fire(2000);
- while (analog(0) < ref) {
- sl(40);
- }
- while (analog(0) > ref) {
- sl(40);
- }
- FFF();
- trackTime(600);
- fire(2000);
- bk(30); delay(500);
- sr(30); delay(225);
- fd(30); delay(1000);
- trackUntilWall(); ao();
- }
- void RRR() {
- while (in(27) && in(30)) {
- L = analog(0);
- R = analog(1);
- if (L < ref && R < ref) {
- fd2(30, 33);
- }
- else if (L > ref && R < ref) {
- sl(40);
- }
- else if (L < ref && R > ref) {
- sr(40);
- }
- else if (L > ref && R > ref) {
- fd(30); delay(200);
- while (analog(1) < ref) {
- sr(40);
- }
- while (analog(1) > ref) {
- sr(40);
- }
- }
- }
- }
- void trackTime(long t) {
- long m = millis();
- while (millis() < (m + t)) {
- L = analog(0);
- R = analog(1);
- if (L < ref && R < ref) {
- fd2(30, 33);
- }
- else if (L > ref && R < ref) {
- sl(40);
- }
- else if (L < ref && R > ref) {
- sr(40);
- }
- }
- ao();
- }
- void barCode() {
- setTextSize(2);
- setTextColor(GLCD_BLACK);
- setTextBackgroundColor(GLCD_WHITE);
- while (analog(0) < blackBrown) {
- fd(27);
- } ao(); delay(100);
- while (analog(0) > blackBrown && analog(0) < whiteBrown) {
- fd(27);
- } ao(); delay(100);
- if (analog(0) < blackBrown) {
- glcd(2, 0, "BLACK"); A = 1;
- while (analog(0) < blackBrown) {
- fd(27);
- } ao(); delay(100);
- }
- else if (analog(0) > whiteBrown) {
- glcd(2, 0, "WHITE"); A = 0;
- while (analog(0) > whiteBrown) {
- fd(27);
- } ao(); delay(100);
- }
- while (analog(0) > blackBrown && analog(0) < whiteBrown) {
- fd(27);
- } ao(); delay(100);
- if (analog(0) < blackBrown) {
- glcd(3, 0, "BLACK"); B = 2;
- while (analog(0) < blackBrown) {
- fd(27);
- } ao(); delay(100);
- }
- else if (analog(0) > whiteBrown) {
- glcd(3, 0, "WHITE"); B = 0;
- while (analog(0) > whiteBrown) {
- fd(27);
- } ao(); delay(100);
- }
- while (analog(0) > blackBrown && analog(0) < whiteBrown) {
- fd(27);
- } ao(); delay(100);
- if (analog(0) < blackBrown) {
- glcd(4, 0, "BLACK"); C = 4;
- while (analog(0) < blackBrown) {
- fd(27);
- } ao(); delay(100);
- }
- else if (analog(0) > whiteBrown) {
- glcd(4, 0, "WHITE"); C = 0;
- while (analog(0) > whiteBrown) {
- fd(27);
- } ao(); delay(100);
- }
- BAR = A + B + C;
- glcdFillScreen(GLCD_BLUE);
- setTextSize(10);
- setTextBackgroundColor(GLCD_BLUE);
- setTextColor(GLCD_YELLOW);
- glcd(0, 1, "%d ", A + B + C);
- }
- void rdSensor() {
- setTextSize(2); glcdMode(1);
- while (sw_1() == 0) {
- L = analog(0); R = analog(1);
- glcd(0, 0, "L=%d ", L);
- glcd(1, 0, "R=%d ", R);
- glcd(2, 0, "%d ", compass_read());
- }
- }
- void fire(int t) {
- out(12, 1);
- delay(t);
- out(12, 0);
- }
- //////////////////////////////////////////////////
- void barCodenat()
- {
- setTextSize(2);
- setTextColor(GLCD_BLACK);
- setTextBackgroundColor(GLCD_WHITE);
- /////// ทดสอบเดินบนพื่นที่สีดำ
- ao(); delay(500);
- while (analog(1) < 100) //ค่าสีดำ
- {
- fd(20);
- }
- ////////////////////////
- ao(); delay(100);
- bar1();
- }
- void bar1()
- {
- /////เส้นที่ 1
- while (analog(1) > 100 && analog(1) < 320) ////เมื่อเซ็นเซอร์เจอสีน้ำตาล ให้เดินไปเรื่อย
- {
- fd(20);
- }
- //ao();delay(100);
- fd(20); delay(100);
- if (analog(1) < 100)
- {
- A = 4;
- while (analog(1) < 150)
- {
- fd(20);
- }
- }
- else
- {
- A = 0;
- while (analog(1) > 300)
- {
- fd(20);
- }
- }
- bar2();
- }
- void bar2()
- {
- /////เส้นที่ 2
- while (analog(1) > 100 && analog(1) < 300) ////เมื่อเซ็นเซอร์เจอสีน้ำตาล ให้เดินไปเรื่อย
- {
- fd(20);
- }
- fd(20); delay(100);
- if (analog(1) < 100)
- {
- B = 2;
- while (analog(1) < 150)
- {
- fd(20);
- }
- }
- else
- {
- B = 0;
- while (analog(1) > 300)
- {
- fd(20);
- }
- }
- bar3();
- }
- void bar3()
- {
- /////เส้นที่ 3
- while (analog(1) > 100 && analog(1) < 400) ////เมื่อเซ็นเซอร์เจอสีน้ำตาล ให้เดินไปเรื่อย
- {
- fd(20);
- }
- fd(20); delay(100);
- if (analog(1) < 150)
- {
- C = 1;
- }
- else
- {
- C = 0;
- }
- ao();
- BAR = A + B + C;
- glcdFillScreen(GLCD_BLACK);
- setTextSize(10);
- setTextBackgroundColor(GLCD_BLACK);
- setTextColor(GLCD_YELLOW);
- glcd(0, 1, "%d ",BAR);
- delay(200);
- }
- //////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement