I watched this video and some of the channel's other ones to begin the research process and understand what the scratch code and actual code should look like.
Arduino Coding This is a link to the Arduino Coding on the mBot
Sketch Coding This is the link to downloading the mBlock sketch coding
I played around with it a little bit, but I am still yet to figure out how it works and find some information about how to set it up for the mBot (the video does not specify this)
I used these websites to learn about the Ultrasonic sensor and how to connect it to a servo and code it.
This website was the main inspiration for my design for the mBot, and I used it as the basis of learning where the wires go.
I used this video to get an overview of how connections and coding on the Arduino work.
This is the code, circuit, and other general information about the ultrasonic sensor (the ping) for the Arduino.
This is the code for the servo, which can be found by going into the Arduino examples and opening the file:
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
More information about my code for the project can be found on the redesign page
The final code:
#include <Servo.h>
#define trigpin 5//set trigpin
#define echopin 6//set echopin
Servo myservo;// declare servo name type servo
int duration, distance;//declare variable for unltrasonic sensor
void setup() {
Serial.begin(9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
myservo.attach(2);// attach your servo
myservo.writeMicroseconds(1500);
// put your setup code here, to run once:
}
void loop() {
myservo.write(90);// always set servo to 90 to position it to the middle
//ultrasonic code
digitalWrite(trigpin,HIGH);
_delay_ms(10000);// change the delay to make claw hold objects longer
digitalWrite(trigpin, LOW);
duration=pulseIn(echopin,HIGH);
distance=(duration/2)/29.1;
if(distance <=15)// if ultrasonic sensor detects an obstacle less than 1cm in 90 degree angle.
{
myservo.write(0); //servo rotates at full speed to the right
delay(600);
}
else
{
myservo.write(90);// else servo stays at 90 degree angle.
delay(600);
}
Serial.print("cm"); //print distance unit cm
Serial.println(distance);//distance
// put your main code here, to run repeatedly:
}
**** Just a note on the code: it can for sure be somehow programmed to reduce the amount of time the robot takes to see the object in front of it, but due to the time constraints of this project, I was not able to figure that out. The improved code will be available here later, or on the Instructables communication page (yet to come).