int checkstate() { uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN; int state; if (vw_get_message(buf, &buflen)) // Non-blocking { if (char(buf[0]) == 'L') { state = 2; } else if (char(buf[0]) == 'J') { state = 1; } joystickx = map(int(buf[1]), 0, 127, 0, 255); joysticky = map(int(buf[2]), 0, 127, 0, 255); } return state; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void followjoystick(int joystickx, int joysticky) { if (joysticky > 130) { if ((joystickx > 120) && (joystickx < 130)) { Serial.println("move straight forward"); int pwm = map(joysticky, 130, 255, 0, 255); moveforward(); analogWrite(leftpwm, pwm); analogWrite(rightpwm, pwm); } else { Serial.println("both forward with ratio"); int pwm = map(joysticky, 130, 255, 100, 255); int joyedit = map(joystickx, 0, 255, 100, 255); int leftvalue = ((pwm - 100) / 155) * (joystickx); int rightvalue = ((pwm - 100) / 155) * (255 - joystickx) ; moveforward(); analogWrite(rightpwm, rightvalue); analogWrite(leftpwm, leftvalue); } } ////////////////////// if (joysticky < 120) { if ((joystickx > 120) && (joystickx < 130)) { Serial.println("move straight backwords"); int pwm = map(joysticky, 120, 0, 0, 255); movebackwards(); analogWrite(leftpwm, pwm); analogWrite(rightpwm, pwm); } else { Serial.println("both backwards with ratio"); int pwm = map(joysticky, 120, 0, 0, 255); int leftvalue = ((pwm - 100) / 155) * (joystickx); int rightvalue = ((pwm - 100) / 155) * (255 - joystickx) ; movebackwards(); analogWrite(rightpwm, rightvalue); analogWrite(leftpwm, leftvalue); } } /////////////////////// if ((joysticky > 120) && (joysticky < 130)) { if (joystickx < 120) { Serial.println("spin clockwise"); int pwm = map(joystickx, 120, 0, 0, 255); spinc(); analogWrite(leftpwm, pwm); analogWrite(rightpwm, pwm); } else if (joystickx > 130) { Serial.println("spin counter clockwise"); int pwm = map(joysticky, 130, 255, 0, 255); spincc(); analogWrite(leftpwm, pwm); analogWrite(rightpwm, pwm); } else { Serial.println("not moving"); analogWrite(leftpwm, 0); analogWrite(rightpwm, 0); } } ////////////////////////// } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void followline() { Serial.println("following line"); if ((analogRead(A1) >= 500) && (analogRead(A0) <= 500) && (analogRead(A2) <= 500)) { moveforward(); analogWrite(leftpwm, 200); analogWrite(rightpwm, 200); } if ((analogRead(A1) >= 500) && (analogRead(A0) >= 500) && (analogRead(A2) <= 500)) { spinc(); analogWrite(leftpwm, 200); analogWrite(rightpwm, 200); } if ((analogRead(A1) <= 500) && (analogRead(A0) >= 500) && (analogRead(A2) <= 500)) { spinc(); analogWrite(leftpwm, 200); analogWrite(rightpwm, 200); } if ((analogRead(A1) >= 500) && (analogRead(A0) <= 500) && (analogRead(A2) >= 500)) { spincc(); analogWrite(leftpwm, 200); analogWrite(rightpwm, 200); } if ((analogRead(A1) <= 500) && (analogRead(A0) <= 500) && (analogRead(A2) >= 500)) { spincc(); analogWrite(leftpwm, 200); analogWrite(rightpwm, 200); } if ((analogRead(A1) <= 500)&& (analogRead(A0) <= 500) && (analogRead(A2) <= 500)) { analogWrite(leftpwm, 0); analogWrite(rightpwm, 0); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void moveforward() { digitalWrite(leftc, LOW); digitalWrite(leftcc, HIGH); digitalWrite(rightc, HIGH); digitalWrite(rightcc, LOW); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void movebackwards() { digitalWrite(leftc, HIGH); digitalWrite(leftcc, LOW); digitalWrite(rightc, LOW); digitalWrite(rightcc, HIGH); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void spinc () { digitalWrite(leftcc, LOW); digitalWrite(leftc, HIGH); digitalWrite(rightcc, LOW); digitalWrite(rightc, HIGH); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void spincc() { digitalWrite(leftcc, HIGH); digitalWrite(leftc, LOW); digitalWrite(rightcc, HIGH); digitalWrite(rightc, LOW); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////