Stap 9: Het is tijd om te laden van de code!
Met de bedrading gecontroleerd en opnieuw gecontroleerd, is het tijd om te laden van de code. Volg de instructies op de site van de Arduino voor het installeren van de Arduino IDE. Nadat de IDE is geïnstalleerd, kopieert u de onderstaande programma en verleden in de Arduino IDE. Verbind uw PC en Arduino omhoog met de USB-kabel. Nu kiest de Raad van bestuur van de Tools -> Pension menu in de IDE, Uno voor dit project (Kies het bestuur hebt indien verschillend van Uno) nu vanaf Tools -> menu Port, kies uw com-poort. Nadat dit is voltooid, klikt u op de knop ' uploaden '. Als alles goed is gegaan, de code was geladen, zo niet zie hier voor het helpen op de IDE en aanverwante kwesties.
/* Bumbley the Arduino obstacle avoider. Goal in life... Bumblels around trying to stay out of your way :-) Written by Scott Beasley - 2015 Free to use or modify. Enjoy. */*/ Uses the Arduino Servo library. */#include <Servo.h> // Turn based on clear direction or left if both are blocked #define turn(dir) (dir >= 2) ? go_left () : go_right ()// Change these defines if you use differnt pins #define LEFT_IR 5 // Digital pin 2. #define RIGHT_IR 4 // Digital pin 5 #define LEFT_SERVO 6 // Pin used for the left servo #define RIGHT_SERVO 7 // Pin used for the right servo// Speed defines #define MAXFORWARDSPEED 120 // Max speed we want moving forward #define MAXBACKWARDSPEED 60 // Max reverse speed #define STOP 90// Various time delays used for driving and servo #define TURNDELAY 450 #define BACKUPDELAY 300/* Globals area. Try to keep them to a minimum :-) */// Create the servo objects with use to interface with Servo motor_left; // Create Left servo motor object Servo motor_right; // Create Right servo motor objectvoid setup ( ) { // Un-comment if you want or need to debug //Serial.begin (9600); // Set Serial monitor at 9600 baud //Serial.println ("My Servo SHR bot is starting up!"); motor_left.attach (LEFT_SERVO); // Attach the servo to the digital pin motor_left.write (STOP); // Set the servo to the middle (neutral) pos motor_right.attach (RIGHT_SERVO); // Attach the servo to the digital pin motor_right.write (STOP); // Set the servo to the middle (neutral) pos // Set modes for proximity sensor pins pinMode (LEFT_IR, INPUT); pinMode (RIGHT_IR, INPUT); // Delay to give user time to make adjustments. Remove after done. //delay (30000); }void loop ( ) { byte direction; // Get a reading from the current sensor direction direction = read_ir_sensors ( ); //Serial.print ("IR sensors reading: "); //Serial.println (direction); // Go forward while nothing is in the sensors read area if (direction == 3) // Forward, march! { go_forward ( ); } else // There is something in the sensors read area { halt ( ); // Stop! go_backward ( ); // Back up a bit delay (BACKUPDELAY); halt ( ); // Stop! turn (direction); // Turn toward the clearest path delay (TURNDELAY); halt ( ); } }// Read the sensor after we find something in the way. This helps find a new // path byte read_ir_sensors ( ) { byte lt_sens = -1, rt_sens = -1, direction; lt_sens = digitalRead (LEFT_IR); rt_sens = digitalRead (RIGHT_IR); if (lt_sens && rt_sens) // Left and right are both blocked, turn left direction = 0; else if (lt_sens) // Left is blocked, turn right direction = 1; else if (rt_sens) // Right is blocked, turn left direction = 2; else if (!lt_sens && !rt_sens) // All clear on both direction = 3; return (direction); }void go_forward ( ) { //Serial.println ("Going forward..."); motor_left.write (MAXFORWARDSPEED); motor_right.write (MAXBACKWARDSPEED); }void go_backward ( ) { //Serial.println ("Going backward..."); motor_left.write (MAXBACKWARDSPEED); motor_right.write (MAXFORWARDSPEED); }void go_left ( ) { //Serial.println ("Going left..."); motor_left.write (MAXFORWARDSPEED); motor_right.write (MAXFORWARDSPEED); }void go_right ( ) { //Serial.println ("Going right..."); motor_left.write (MAXBACKWARDSPEED); motor_right.write (MAXBACKWARDSPEED); }void halt ( ) { //Serial.println ("Halt!"); motor_left.write (STOP); motor_right.write (STOP); }