stap 1-7:
Bouw een Robot stap 1-7
je kunt de belangrijkste bestanden van dit projekt “downloaden” van Ron’s NAS (zie blog)
of hier in de weergegeven listings met copy/paste.
SMARS DEMO 1:
#include <AFMotor.h> // SMARS Demo 1 with line sensor // This sketch makes the robot move inside a delimitated area // (you can make a perimeter with insulating tape) AF_DCMotor motorL(1); // left motor AF_DCMotor motorR(2); // right motor // declare variables int lineNumber; // defines the variable where it will // store the line sensor value void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // changes the following values to make the robot drive as // straight as possible motorL.setSpeed(200); // sets motorL speed motorR.setSpeed(200); // sets motorR speed motorL.run(RELEASE); // turns motorL off motorR.run(RELEASE); // turns motorR off } void loop() { lineNumber = analogRead(A0); // reads the sensor value from pin A0 // and stores it in the variable lineNumber while (lineNumber < 800) // (WIT) repeats the following part // of code until the light sensor will find a darker zone { motorL.run(FORWARD); // forward motorR.run(FORWARD); lineNumber = analogRead(A0); // reads the sensor value from pin A0 // and stores it in the variable lineNumber Serial.print("linenumber: "); Serial.println(lineNumber); // send the value to the serial monitor } // the following operations will make the robot goes backward for // 1 second and turns left for 0.15 second motorL.run(RELEASE); // stop motorR.run(RELEASE); delay(200); motorL.run(BACKWARD); // backward motorR.run(BACKWARD); delay(1000); motorL.run(RELEASE); // stop motorR.run(RELEASE); delay(200); motorL.run(BACKWARD); // turn left motorR.run(FORWARD); delay(150); motorL.run(RELEASE); // stop motorR.run(RELEASE); delay(200); motorL.run(FORWARD); // forward motorR.run(FORWARD); delay(1000); Serial.print("linenumber: "); Serial.println(lineNumber); // send the value to the serial monitor }
SMARS DEMO 3:
#include <AFMotor.h> // SMARS Demo 3 with ultrasonic sensor // This sketch makes the robot avoid obstacles. AF_DCMotor motorL(1); // left motor AF_DCMotor motorR(2); // right motor int distancecm = 0; const int trigPin = 18; const int echoPin = 19; long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // changes the following values to make the robot drive // as straight as possible motorL.setSpeed(200); // sets motorL speed motorR.setSpeed(200); // sets motorR speed motorL.run(RELEASE); // turns motorL off motorR.run(RELEASE); // turns motorR off } void loop() { delay(200); distancecm = mdistance(); if (distance <= 20) { motorL.run(RELEASE); // stop motorR.run(RELEASE); delay(200); motorL.run(BACKWARD); // backward motorR.run(BACKWARD); delay(200); motorL.run(RELEASE); // stop motorR.run(RELEASE); delay(200); motorL.run(BACKWARD); // turn left motorR.run(FORWARD); delay(100); motorL.run(RELEASE); // stop motorR.run(RELEASE); delay(200); } else { motorL.run(FORWARD); // forward motorR.run(FORWARD); } } int mdistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.println(distance); // send the value to the serial monitor return distance; }
SMARS MOTOR TEST:
#include <AFMotor.h> // SMARS test AF_DCMotor motorL(1); // left motor AF_DCMotor motorR(2); // right motor void setup() { // changes the following values to make the robot drive // as straight as possible motorL.setSpeed(200); // sets motorL speed motorR.setSpeed(200); // sets motorR speed } void loop() { motorL.run(FORWARD); // forward //motorR.run(FORWARD); //delay(1500); //motorL.run(RELEASE); // stop //motorR.run(RELEASE); //delay(500); //motorL.run(BACKWARD); // backward //motorR.run(BACKWARD); //delay(1500); //motorL.run(RELEASE); // stop //motorR.run(RELEASE); //delay(500); //motorL.run(FORWARD); // turn right //motorR.run(RELEASE); //delay(500); //motorL.run(RELEASE); // stop //motorR.run(RELEASE); //delay(500); //motorL.run(RELEASE); // turn left //motorR.run(FORWARD); //delay(500); }