SlideShare a Scribd company logo
#include <Servo.h> //Servo library
Servo servo_test; //Servo setup
int angle = 0; // Servo setup
// motors
int in1 = 4;
int in2 = 5;
int in3 = 6;
int in4 = 7;
unsigned long previousMillis = 0; // will store last time servos were spinning
unsigned long currentMillis = 0;
const long interval = 5000; // interval between each round of spin
int trigPin = 3; // Trigger for ultrasonic sensor
int echoPin = 2; // Echo for ultrasonic sensor
long duration, cm, inches; // ultrasonic sensor setup
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo_test.attach(9);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose duration is the time (in microseconds)
// from the sending of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // Convert time into distance
delay(250);
currentMillis = millis();
if (cm < 15) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
} else {
// Turn on motors in FORWARD motions
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time spinning the servo
digitalWrite(in1, LOW); // stop all motors
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
for (angle = 0; angle < 360; angle += 5) { // turn servo 1000 degrees
servo_test.write(angle);
delay(5);
}
delay(400);
for (angle = 360; angle>=1; angle-=5) { // reverse servo back to 0 degrees
servo_test.write(angle);
delay(5);
}
delay(1000);
}
}
}

More Related Content

PPT
Arduinomotorcontrolusingservoultrasonic
TXT
Metal detecting robot sketch
PDF
UART MCU
TXT
Ir remote kit_blink.pde
PDF
Timer 0 programming on LPC 1768
PDF
Handling Asynchronous Events in MCUs
DOC
FINISHED_CODE
PPTX
Distance measurement using Ultrasonic sensor on Arduino Uno
Arduinomotorcontrolusingservoultrasonic
Metal detecting robot sketch
UART MCU
Ir remote kit_blink.pde
Timer 0 programming on LPC 1768
Handling Asynchronous Events in MCUs
FINISHED_CODE
Distance measurement using Ultrasonic sensor on Arduino Uno

What's hot (20)

PDF
Handling Interrupts in Microchip MCUs
PDF
Embedded system course projects - Arduino Course
PDF
LED Lighting Product Attachment
DOCX
Micro Assignment 1
PDF
Using Timers in PIC18F Microcontrollers
PDF
Exercises with timers and UART
PPTX
Exception handling in Pipelining in COA
PPTX
Snort Home Lab - Workshop
DOCX
Lampiran 1.programdocx
TXT
Final pid2
PPT
Hexapod ppt
PPTX
Embedded JavaScript
PPTX
The evolution of fieldbus topologies
PDF
Using Timer2 in Microchip MCUs
PDF
Interrupt
PPT
Introduction to Vortex86DX2 Motion-Control Evaluation Board
PDF
Lathe Spindle Sensor
DOCX
Programming avr microcontroller digital i
PPT
Ports 0f 8051
PDF
Handling Interrupts in Microchip MCUs
Embedded system course projects - Arduino Course
LED Lighting Product Attachment
Micro Assignment 1
Using Timers in PIC18F Microcontrollers
Exercises with timers and UART
Exception handling in Pipelining in COA
Snort Home Lab - Workshop
Lampiran 1.programdocx
Final pid2
Hexapod ppt
Embedded JavaScript
The evolution of fieldbus topologies
Using Timer2 in Microchip MCUs
Interrupt
Introduction to Vortex86DX2 Motion-Control Evaluation Board
Lathe Spindle Sensor
Programming avr microcontroller digital i
Ports 0f 8051
Ad

Similar to Mitsubachi Arduino code (20)

PDF
Open bot
PDF
OpenBot-Code
PPTX
Internet of Things (IoT)-Sensors & Actuators - IoT.pptx
DOCX
Password based automations system and security
TXT
Arm code arduino
DOCX
CODING IN ARDUINO
PPTX
Arduino radar system
PPTX
Self Obstacle Avoiding Rover
PPTX
Cs project ppt
PPTX
How Automatic Vaccum cleaner works in today world.
PPTX
Introduction to Arduino IDE Software-1.pptx
PPTX
Introduction to Arduino IDE Software-1 (1).pptx
PDF
Arduino uno basic Experiments for beginner
PDF
Arduino Workshop Day 2 - Advance Arduino & DIY
DOCX
IOT excercise ESP32 Simulation projects.docx
PDF
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
PDF
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
PDF
Arduino Programming for Basic Robotics - University of Moratuwa
PDF
Arduino based applications-part 3
PPTX
Arduino Programming
Open bot
OpenBot-Code
Internet of Things (IoT)-Sensors & Actuators - IoT.pptx
Password based automations system and security
Arm code arduino
CODING IN ARDUINO
Arduino radar system
Self Obstacle Avoiding Rover
Cs project ppt
How Automatic Vaccum cleaner works in today world.
Introduction to Arduino IDE Software-1.pptx
Introduction to Arduino IDE Software-1 (1).pptx
Arduino uno basic Experiments for beginner
Arduino Workshop Day 2 - Advance Arduino & DIY
IOT excercise ESP32 Simulation projects.docx
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Programming for Basic Robotics - University of Moratuwa
Arduino based applications-part 3
Arduino Programming
Ad

More from Yilin Zeng (10)

PDF
Competitive Analysis
PDF
Contextual Inquiry
DOCX
Group Deliverable: Interviews & Personas
DOCX
My User Research Deliverable
PDF
project proposal
PDF
Project: Mitsubachi
PDF
Netflix Analysis
PDF
Seaborn graphing present
PDF
Ethnographic breaching experiment
PDF
Inclusive design playbook
Competitive Analysis
Contextual Inquiry
Group Deliverable: Interviews & Personas
My User Research Deliverable
project proposal
Project: Mitsubachi
Netflix Analysis
Seaborn graphing present
Ethnographic breaching experiment
Inclusive design playbook

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
737-MAX_SRG.pdf student reference guides
DOCX
573137875-Attendance-Management-System-original
PPTX
additive manufacturing of ss316l using mig welding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Artificial Intelligence
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
web development for engineering and engineering
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
737-MAX_SRG.pdf student reference guides
573137875-Attendance-Management-System-original
additive manufacturing of ss316l using mig welding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CH1 Production IntroductoryConcepts.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Artificial Intelligence
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Geodesy 1.pptx...............................................
web development for engineering and engineering
III.4.1.2_The_Space_Environment.p pdffdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Mitsubachi Arduino code

  • 1. #include <Servo.h> //Servo library Servo servo_test; //Servo setup int angle = 0; // Servo setup // motors int in1 = 4; int in2 = 5; int in3 = 6; int in4 = 7; unsigned long previousMillis = 0; // will store last time servos were spinning unsigned long currentMillis = 0; const long interval = 5000; // interval between each round of spin int trigPin = 3; // Trigger for ultrasonic sensor int echoPin = 2; // Echo for ultrasonic sensor long duration, cm, inches; // ultrasonic sensor setup void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); servo_test.attach(9); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose duration is the time (in microseconds) // from the sending of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); cm = (duration/2) / 29.1; // Convert time into distance delay(250); currentMillis = millis();
  • 2. if (cm < 15) { digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } else { // Turn on motors in FORWARD motions digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // save the last time spinning the servo digitalWrite(in1, LOW); // stop all motors digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); for (angle = 0; angle < 360; angle += 5) { // turn servo 1000 degrees servo_test.write(angle); delay(5); } delay(400); for (angle = 360; angle>=1; angle-=5) { // reverse servo back to 0 degrees servo_test.write(angle); delay(5); } delay(1000); } } }