Robotale: радиоуправляемая машинка с Arduino и Bluetooth, которая поможет изучить основы работы с Arduino и не только

/ / *******************************int MotorRight1 = 5; int MotorRight2 = 6; int MotorLeft1 = 10; int MotorLeft2 = 11; void setup (){Serial.begin (9600); pinMode (MotorRight1, OUTPUT); / / Pin 8 (PWM)pinMode (MotorRight2, OUTPUT); / / Pin 9 (PWM)pinMode (MotorLeft1, OUTPUT); / / Pin 10 (PWM)pinMode (MotorLeft2, OUTPUT); / / Pin 11 (PWM)}

void go () / / Forward{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH);

}

void left () / / turn right{digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH);

}void right () / / turn left{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, LOW);

}void stop () / / stop{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW);

}void back () / / Check out{digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, LOW);;

}

void loop (){char val = Serial.read (); Serial.write (val); if (-1! = val) {if ('W' == val)go (); else if ('A' == val)left (); else if ('D' == val)right (); else if ('S' == val)back (); else if ('Q' == val)stop (); delay (500);}else{/ / Stop (); delay (500);}}

90ff672f0b54038392d922b187a07002.jpg

Если же необходимо задействовать все функции, включая распознавание инфракрасного сигнала, команд Bluetooth-модуля и прочие возможности, следует использовать такой код:

/ / ******************************# Include # Include / / *********************** Definition of motor pin ********************* ****int MotorRight1 = 5; int MotorRight2 = 6; int MotorLeft1 = 10; int MotorLeft2 = 11; int counter = 0; const int irReceiverPin = 2; / / OUTPUT signals IR receiver connected to pin 2

char val;/ / *********************** Set to detect the IRcode ****************** *******long IRfront = 0×00FFA25D; / / Forward codelong IRback = 0×00FF629D; / / Check outlong IRturnright = 0×00FFC23D; / / Rightlong IRturnleft = 0×00FF02FD; / / Leftlong IRstop = 0×00FFE21D; / / Stoplong IRcny70 = 0×00FFA857; / / CNY70 self-propelled modelong IRAutorun = 0×00FF 906F; / / Self-propelled mode ultrasoundlong IRturnsmallleft = 0×00FF22DD;/ / ************************* Defined CNY70 pin ******************* *****************const int SensorLeft = 7; / / Left sensor input pinconst int SensorMiddle = 4; / / The sensor input pinconst int SensorRight = 3; / / Right sensor input pinint SL; / / Left sensor statusint SM; / / The sensor statusint SR; / / Right sensor statusIRrecv irrecv (irReceiverPin); / / Define an object to receive infrared signals IRrecvdecode_results results; / / Decoding results will result in structural variables in decode_results/ / ************************* Defined ultrasound pin ****************** ************int inputPin = 13; / / define pin ultrasonic signal receiver rxint outputPin = 12; / / define ultrasonic signal transmitter pin 'txint Fspeedd = 0; / / in front of distanceint Rspeedd = 0; / / the right distanceint Lspeedd = 0; / / left distanceint directionn = 0; / / = 8 post = 2 front left and right = 6 = 4Servo myservo; / / set myservoint delay_time = 250; / / settling time after steering servo motorsint Fgo = 8; / / Forwardint Rgo = 6; / / turn rightint Lgo = 4; / / turn leftint Bgo = 2; / / reverse/ / ************************************************ ******************** (SETUP)void setup (){Serial.begin (9600); pinMode (MotorRight1, OUTPUT); / / Pin 8 (PWM)pinMode (MotorRight2, OUTPUT); / / Pin 9 (PWM)pinMode (MotorLeft1, OUTPUT); / / Pin 10 (PWM)pinMode (MotorLeft2, OUTPUT); / / Pin 11 (PWM)irrecv.enableIRIn (); / / Start infrared decodingpinMode (SensorLeft, INPUT); / / define left SensorspinMode (SensorMiddle, INPUT) ;/ / definition sensorspinMode (SensorRight, INPUT); / / definition of the right sensordigitalWrite (2, HIGH); pinMode (inputPin, INPUT); / / define ultrasound input pinpinMode (outputPin, OUTPUT); / / define ultrasonic output pinmyservo.attach (9); / / define servo motor output section 5 pin (PWM)

}/ / ************************************************ ****************** (Void)void advance (int a) / / Forward{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH); delay (a * 100);}void right (int b) / / turn right (single wheel){digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH); digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); delay (b * 100);}void left (int c) / / turn left (single wheel){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); delay (c * 100);}void turnR (int d) / / turn right (wheel){digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH); delay (d * 100);}void turnL (int e) / / turn left (wheel){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, LOW); delay (e * 100);}void stopp (int f) / / Stop{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); delay (f * 100);}void back (int g) / / Check out{digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, LOW);; delay (g * 100);}void detection () / / measure three angles (front Left. Right){int delay_time = 250; / / settling time after steering servo motorsask_pin_F (); / / read from front

if (Fspeedd <10) / / if the distance is less than 10 cm in front of{stopp (1); / / clear the output databack (2); / / Check out 0.2 seconds}if (Fspeedd <25) / / if the distance is less than 25 cm in front of{stopp (1); / / clear the output dataask_pin_L (); / / read from leftdelay (delay_time); / / wait for stable servo motorask_pin_R (); / / read the right distancedelay (delay_time); / / wait for stable servo motor

if (Lspeedd> Rspeedd) / / If the distance is greater than the right from the left{directionn = Lgo; / / go left}

if (Lspeedd <= Rspeedd) / / if the distance is less than or equal to the left to the right distance{directionn = Rgo; / / go right}

if (Lspeedd <15 && Rspeedd <15) / / if the distance to the left and right are less than 10 cm distance{directionn = Bgo; / / to go after}}else / / add as greater than 25 cm in front of{directionn = Fgo; / / to move forward}}/ / ************************************************ *********************************void ask_pin_F () / / Measure the distance from the front{myservo.write (90);digitalWrite (outputPin, LOW); / / make ultrasonic transmitter low voltage 2 μ sdelayMicroseconds (2);digitalWrite (outputPin, HIGH); / / make ultrasonic transmitting high voltage 10 μ s, where at least 10 μ sdelayMicroseconds (10);digitalWrite (outputPin, LOW); / / maintain low voltage ultrasonic transmitterfloat Fdistance = pulseIn (inputPin, HIGH); / / read worse time differenceFdistance = Fdistance/5.8/10; / / will turn to time distance (unit: cm)Serial.print («F distance:»); / / output distance (unit: cm)Serial.println (Fdistance); / / display the distanceFspeedd = Fdistance; / / will enter Fspeedd (former speed) from Reading}/ / ************************************************ ********************************void ask_pin_L () / / Measure the distance from the left{myservo.write (177);delay (delay_time);digitalWrite (outputPin, LOW); / / make ultrasonic transmitter low voltage 2 μ sdelayMicroseconds (2);digitalWrite (outputPin, HIGH); / / make ultrasonic transmitting high voltage 10 μ s, where at least 10 μ sdelayMicroseconds (10);digitalWrite (outputPin, LOW); / / maintain low voltage ultrasonic transmitterfloat Ldistance = pulseIn (inputPin, HIGH); / / read worse time differenceLdistance = Ldistance/5.8/10; / / will turn to time distance (unit: cm)Serial.print («L distance:»); / / output distance (unit: cm)Serial.println (Ldistance); / / display the distanceLspeedd = Ldistance; / / will be read into the distance Lspeedd (left-speed)}/ / ************************************************ ******************************void ask_pin_R () / / Measure the distance from the right{myservo.write (5);delay (delay_time);digitalWrite (outputPin, LOW); / / make ultrasonic transmitter low voltage 2 μ sdelayMicroseconds (2);digitalWrite (outputPin, HIGH); / / make ultrasonic transmitting high voltage 10 μ s, where at least 10 μ sdelayMicroseconds (10);digitalWrite (outputPin, LOW); / / maintain low voltage ultrasonic transmitterfloat Rdistance = pulseIn (inputPin, HIGH); / / read worse time differenceRdistance = Rdistance/5.8/10; / / will turn to time distance (unit: cm)Serial.print («R distance:»); / / output distance (unit: cm)Serial.println (Rdistance); / / display the distanceRspeedd = Rdistance; / / will be read into the distance Rspeedd (Right-speed)}/ / ************************************************ ****************************** (LOOP)void loop (){SL = digitalRead (SensorLeft);SM = digitalRead (SensorMiddle);SR = digitalRead (SensorRight);performCommand ();/ / ************************************************ *************************** normal remote modeif (irrecv.decode (& results)){ / / Decoding is successful, you receive a set of infrared signals/ ************************************************* ********************** /if (results.value == IRfront) / / Forward{advance (10) ;/ / forward}/ ************************************************* ********************** /if (results.value == IRback) / / Check out{back (10) ;/ / after retirement}/ ************************************************* ********************** /if (results.value == IRturnright) / / turn right{right (6); / / turn right}/ ************************************************* ********************** /if (results.value == IRturnleft) / / turn left{left (6); / / turn left);}/ ************************************************* ********************** /if (results.value == IRstop) / / Stop{digitalWrite (MotorRight1, LOW);digitalWrite (MotorRight2, LOW);digitalWrite (MotorLeft1, LOW);digitalWrite (MotorLeft2, LOW);}/ / ************************************************ *********************** cny70 model black self-propelled mode: LOW White:if (results.value == IRcny70){while (IRcny70){SL = digitalRead (SensorLeft);SM = digitalRead (SensorMiddle);SR = digitalRead (SensorRight);

if (SM == HIGH) / / in sensors in black areas{if (SL == LOW & SR == HIGH) / / left and right black white, turn left{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); analogWrite (MotorLeft1, 0); analogWrite (MotorLeft2, 80);}else if (SR == LOW & SL == HIGH) / / left and right black white, turn right{analogWrite (MotorRight1, 0) ;/ / right turnanalogWrite (MotorRight2, 80); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH);}else / / Both sides white, straight{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH); analogWrite (MotorLeft1, 200); analogWrite (MotorLeft2, 200); analogWrite (MotorRight1, 200); analogWrite (MotorRight2, 200);}}else / / the sensors in the white area{if (SL == LOW & SR == HIGH) / / left and right black white, fast turn left{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW);}else if (SR == LOW & SL == HIGH) / / left and right black white, quick right turn{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH);}else / / are white, stop{digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, LOW);;}}if (irrecv.decode (& results)){irrecv.resume (); Serial.println (results.value, HEX); if (results.value == IRstop){digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, HIGH); break;}}}results.value = 0;}/ / ************************************************ self-propelled mode ultrasound ***********************if (results.value == IRAutorun){while (IRAutorun){myservo.write (90); / / return to the pre-prepared so that the servo motor position once the measure under preparationdetection (); / / measure the angle and direction of judgment to where to moveif (directionn == 8) / / If directionn (direction) = 8 (forward){if (irrecv.decode (& results)){irrecv.resume (); Serial.println (results.value, HEX); if (results.value == IRstop){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); break;}}results.value = 0; advance (1); / / normal forwardSerial.print («Advance»); / / display direction (forward)Serial.print (»);}if (directionn == 2) / / If directionn (direction) = 2 (reverse){if (irrecv.decode (& results)){irrecv.resume (); Serial.println (results.value, HEX); if (results.value == IRstop){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); break;}}results.value = 0; back (8); / / reverse (car)turnL (3); / / move slightly to the left (to prevent stuck in dead alley)Serial.print («Reverse»); / / display direction (backwards)}if (directionn == 6) / / If directionn (direction) = 6 (right turn){if (irrecv.decode (& results)){irrecv.resume (); Serial.println (results.value, HEX); if (results.value == IRstop){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); break;}}results.value = 0; back (1); turnR (6); / / turn rightSerial.print («Right»); / / display direction (turn left)}if (directionn == 4) / / If directionn (direction) = 4 (turn left){if (irrecv.decode (& results)){irrecv.resume (); Serial.println (results.value, HEX); if (results.value == IRstop){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); break;}}results.value = 0; back (1); turnL (6); / / turn leftSerial.print («Left»); / / display direction (turn right)}

if (irrecv.decode (& results)){irrecv.resume (); Serial.println (results.value, HEX); if (results.value == IRstop){digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW); break;}}}results.value = 0;}/ ************************************************* ********************** /else{digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW);}

irrecv.resume (); / / Continue to accept a set of infrared signals}}

void performCommand () {if (Serial.available ()) {val = Serial.read ();}if (val == 'f') {/ / Forwardadvance (10);} Else if (val == 'z') {/ / Stop Forwardstopp (10);} Else if (val == 'b') {/ / Backwardback (10);} Else if (val == 'y') {/ / Stop Backwardback (10);} else if (val == 'l') {/ / RightturnR (10);} Else if (val == 'r') {/ / LeftturnL (10);} Else if (val == 'v') {/ / Stop Turnstopp (10);} Else if (val == 's') {/ / Stopstopp (10);}

}

© Habrahabr.ru