int param; void main() { init_mobile_serial(); while (1) { data = get_sci(); if (data > 0) { printf("\nsci: %d", data); beep(); /* right or left turn */ if (data & 0b01000000) { param = (data & 0b00001111); if (data & 0b00010000) { /* right */ turnRight(param); } else { /* left */ turnLeft(param); } } /* forward or back */ else if (data & 0b00100000) { param = (data & 0b00001111); if (data & 0b00010000) { /* back */ goBack(param); } else { /* forward */ goForward(param); } } /* stop */ else if (data & 0b00010000) { alloff(); } } } } void turnLeft(int clicks) { int leftSpeed; int rightSpeed; float duration; leftSpeed = -100; rightSpeed = 100; duration = (float)clicks * 0.1; start_process(doMove(leftSpeed, rightSpeed, duration)); } void turnRight(int clicks) { int leftSpeed; int rightSpeed; float duration; leftSpeed = 100; rightSpeed = -100; duration = (float)clicks * 0.1; start_process(doMove(leftSpeed, rightSpeed, duration)); } void goForward(int clicks) { float duration; duration = (float)clicks * 0.3; start_process(doMove(100, 100, duration)); } void goBack(int clicks) { float duration; duration = (float)clicks * 0.3; start_process(doMove(-100, -100, duration)); } void doMove(int leftSpeed, int rightSpeed, float duration) { motor(2, leftSpeed); motor(3, rightSpeed); sleep(duration); alloff(); return; }