Stap 10: Laden de Code
Download en het bijgevoegde bestand en unzip het naar een locatie die u op uw computer vinden kunt.
Open de Arduino IDE en doe een bestand -> Open en ga naar de locatie die u het bestand hebt uitgepakt.
Klik nu op Extra -> bestuur en selecteer de Arduino Mini Pro w/Atmega328P
Selecteer uw seriële poort en klikt u op de botton voor het uploaden van de code.
Als u problemen ondervindt met de Arduino IDE gaan hier of hier voor hulp. Er vindt u helpteksten over instillation en oplossen van problemen.
Hebt u problemen met de Micro-goochelaar of het stuurprogramma's, ga hier. Eigenlijk beveel ik het lezen over de Dagu controller Instructable alvorens uit te gaan met het gebruik hier.
Meer informatie over de code, hebben een goed te lezen voor het in de Arduino IDE of uw favoriete tekst-editor. Het goed heeft gereageerd en krijgt u een goed idee de werking van de volgeling van de lijn.
Hier is de werkelijke code die gebruikmaakt van de robot:
/* Basic line follower. Goal in life... Finding lines to follow :-) Written by Scott Beasley - 2015 Free to use or modify. Enjoy. NOTES: Before starting the bot up, place it where the CENTER sensor is on the line and the left and right is off. */a/* Uses the MicroM library for the Micro Magician robot contoller board. Download from https://sites.google.com/site/daguproducts/home/arduino-libraries Unzip and copy to your Arduino library folder or follow the instructions here: http://arduino.cc/en/guide/libraries Uses the Metro libary as well. See http://playground.arduino.cc/code/metro for downloding and more infomation. */#include "microM.h" #include "Metro.h"// Sensor pin defines #define SENSOR_L A3 // Left sensor pin #define SENSOR_M A4 // Middle sensor pin #define SENSOR_R A5 // Right sensor pin// Speed defines #define ONLINESPEED 110 // Speed to move when the bot in on the line #define FORWARDTURNSPD 100 // Turning forward speed #define BACKWARDTURNSPD -85 // Backup turning speed// Micro Magician uses 0 for no brake and 1 for brake #define BRAKEOFF 0 #define BRAKEON 1/* Globals area. Try to keep them to a minimum :-) */ Create the motor and Metor objects with use to interface with Metro sensorrd = Metro (5); // Timer for every 5ms// Storage for the "calibation" numbers. Only using the online_avg // but you may want to look at the offline one for compairing sake. int online_avg = 0, offline_avg = 0;void setup ( ) { // Do a SIMPLE calibration. Not a great one, but one. for (int i=0; i < 20; i++) { offline_avg += analogRead (SENSOR_L); offline_avg += analogRead (SENSOR_R); online_avg += analogRead (SENSOR_M); } online_avg = online_avg / 20; offline_avg = offline_avg / 40; // Average of the Left and Right sensors // Start moving the bot forward. microM.Motors (ONLINESPEED, ONLINESPEED, BRAKEOFF, BRAKEOFF); Serial.begin (9600); }void loop ( ) { // Read the sensors every 5ms and act upon it. if (sensorrd.check ( ) != 1) { return; } // Read all 3 senors and return line position condition byte direction = read_sensors ( ); switch (direction) { case 0: // On the line Middle sensor. Keep moving forward. microM.Motors (ONLINESPEED, ONLINESPEED, BRAKEOFF, BRAKEOFF); Serial.println ("moving forward"); break; case 1: // On the line Left sensor. Move right forward. microM.Motors (FORWARDTURNSPD, BACKWARDTURNSPD, BRAKEOFF, BRAKEOFF); Serial.println ("moving right"); break; case 2: // On the line Right sensor. Move left forward. microM.Motors (BACKWARDTURNSPD, FORWARDTURNSPD, BRAKEOFF, BRAKEOFF); Serial.println ("moving left"); break; case 3: // Lost! Move right (spin). microM.Motors (BACKWARDTURNSPD, 0, BRAKEOFF, BRAKEOFF); Serial.println ("lost!"); break; } }// Read all the sensors and figure out where the line is byte read_sensors ( ) { byte sensors = 3; // Init with lost setting and change otherwise. // Read Sensor in middle int mid_read = readSensor (SENSOR_M, 2) - online_avg; // Read Sensor on Left int left_read = readSensor (SENSOR_L, 2) - online_avg; // Read Sensor on Right int right_read = readSensor (SENSOR_R, 2) - online_avg; // Figure out line postion in regards to the 3 sensors // The sensor with the largest number wins! if (mid_read > left_read && mid_read > right_read) sensors = 0; else if (left_read > right_read) sensors = 1; else if (left_read < right_read) sensors = 2; // Return postion found or '3' if no line was found return sensors; }// Read sensor n times and return an average reading int readSensor (int analog_pin, int sample_rate) { int sum = 0; for (int i = 0; i < sample_rate; i++) { sum += analogRead (analog_pin); } return (sum / sample_rate); }