Components and supplies
Servos (Tower Pro MG996R)
Jumper wires (generic)
UTSOURCE Electronic Parts
Arduino UNO
Project description
Code
Servo seep.code
c_cpp
Servo seep.code
c_cpp
Downloadable files
ServoTimer2.sweep.circuit
ServoTimer2.sweep.circuit
ServoTimer2.sweep.code
https://github.com/ashraf-minhaj/Arduino-ServoTimer2-basic-sweep-/tree/code-sweep
ServoTimer2.sweep.code
https://github.com/ashraf-minhaj/Arduino-ServoTimer2-basic-sweep-/tree/code-sweep
ServoTimer2.sweep.circuit
ServoTimer2.sweep.circuit
ServoTimer2.sweep.code
https://github.com/ashraf-minhaj/Arduino-ServoTimer2-basic-sweep-/tree/code-sweep
Comments
Only logged in users can leave comments
reid25
2 years ago
First of all you'll have to download the library and paste it to arduino library folder. then goto sketch>include library>add zip. file from the library folder. Now you're good to go. https://www.mycenturahealth.me/
Anonymous user
2 years ago
Ashraf, you were doing so well, until you suggested supplying the servo from the 5V pin of the Arduino. This is never a good idea, as a servo can draw multiple times the current available on that pin, and it suggests to other beginners the 5V pin is available for generic motorised projects- NO, Typically the symptom will be crashes or restarts of the Arduino sketch, worst case it will burn out tracks on the Arduino board if the servo stalls, or is under load.
Ardu-Fan
2 years ago
lastchancename; You already wrote, what I was going to write.
ashraf_minhaj
2 years ago
Thank you, I'll keep that in mind for future projects and add a Caution message in this project. Anyway, As this servo has no load there won't possibly be any crashes though.
Steve_Massikker
2 years ago
I am very grateful for your work. The users of my project will find this great instruction very useful. Thanks Ashraf!
dezshredder
2 years ago
Thanks for the great write up Ashraf. I'm having this exact problem with conflicts between Servo.h and TMRpcm.h. Trying to get ServoTimer2 to work but getting stuck on an early step. I try to add the library which I have here /<path to docs>.../Arduino/libraries/ServoTimer2/ And it gives me this error msg "A subfolder of your sketchbook is not a valid library." I've tried redownloading and renaming the library a few times. Any help would be greatly appreciated.
dezshredder
2 years ago
actually I got it to add by just taking the ServoTimer.h file out of the directory. but I still can't get it to work correctly in my sketch. It never gets out of the the doSomething() function; ```#include <ServoTimer2.h> #define bobPin 2 ServoTimer2 servoBob; #define trigPin 8 #define echoPin 9 long duration, distance; #include <SD.h> #include <TMRpcm.h> TMRpcm ghostAudio; TMRpcm spookyAudio; int takeReading; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); takeReading = 1; servoBob.attach(bobPin); servoBob.write(750); Serial.print("\ Initializing SD card..."); pinMode(10, OUTPUT); // speaker pin if (!SD.begin(4)) { // set this to your card type and cs pin Serial.println("failed!"); return; } Serial.println("done."); ghostAudio.speakerPin = 10; spookyAudio.speakerPin = 10; } void loop() { // put your main code here, to run repeatedly: if (takeReading == 1){ digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); delayMicroseconds(2); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) / 29.1; } // Serial.print(distance); // Serial.println(" cm"); if (distance <= 0) { Serial.println("Out of range"); itsSpooky(); } if (distance > 1 && distance < 60) { takeReading = 0; Serial.print("my distance is: "); Serial.println(distance); doSomething(); } if ( distance > 61){ // takeReading = 1; Serial.print("my distance > 60 and is: "); Serial.println(distance); itsSpooky(); } } void doSomething(){ servoBob.write(1500); delay(1000); ghostAudio.play("LAUGH5.WAV"); // ghostAudio.play("LAUGH3.WAV"); delay(8000); Serial.println("HI"); Serial.println("HI"); Serial.println("HI"); Serial.println("HI"); Serial.println("HI"); delay(1000); takeReading = 1; } void itsSpooky(){ servoBob.write(750); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); } ```
Anonymous user
5 years ago
I am building this 'useless box' https://youtu.be/cpdjQ0gheDQ and adding a DF player to make it 'talk' for each cycle. Does the 2nd library have to be for servos only?
Anonymous user
5 years ago
hello, iam trying to integrate your library in my sketch, because my servo doesnt move if i use servo.h with timerone.h Well, as soon as i try to compile it, (just your example to try if its working) i have this in IDE: Arduino:1.8.12 (Windows Store 1.8.33.0) (Windows 10), Scheda:"Arduino Uno" In file included from sketch\\EDI_TEST2_solo_servo.ino.cpp:1:0: C:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.33.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\cores\\arduino/Arduino.h:126:14: note: previous declaration as 'typedef bool boolean' typedef bool boolean; ^~~~~~~ exit status 1 Errore durante la compilazione per la scheda Arduino Uno. --------------------------------------------------------------------------------------------------- Do you think there is something wrong about compiling sketch with arduino uno? please let me know because iam stuck with it... thank you
dezshredder
5 years ago
Thanks for the great write up Ashraf. I'm having this exact problem with conflicts between Servo.h and TMRpcm.h. Trying to get ServoTimer2 to work but getting stuck on an early step. I try to add the library which I have here /<path to docs>.../Arduino/libraries/ServoTimer2/ And it gives me this error msg "A subfolder of your sketchbook is not a valid library." I've tried redownloading and renaming the library a few times. Any help would be greatly appreciated.
dezshredder
2 years ago
actually I got it to add by just taking the ServoTimer.h file out of the directory. but I still can't get it to work correctly in my sketch. It never gets out of the the doSomething() function; ```#include <ServoTimer2.h> #define bobPin 2 ServoTimer2 servoBob; #define trigPin 8 #define echoPin 9 long duration, distance; #include <SD.h> #include <TMRpcm.h> TMRpcm ghostAudio; TMRpcm spookyAudio; int takeReading; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); takeReading = 1; servoBob.attach(bobPin); servoBob.write(750); Serial.print("\ Initializing SD card..."); pinMode(10, OUTPUT); // speaker pin if (!SD.begin(4)) { // set this to your card type and cs pin Serial.println("failed!"); return; } Serial.println("done."); ghostAudio.speakerPin = 10; spookyAudio.speakerPin = 10; } void loop() { // put your main code here, to run repeatedly: if (takeReading == 1){ digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); delayMicroseconds(2); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) / 29.1; } // Serial.print(distance); // Serial.println(" cm"); if (distance <= 0) { Serial.println("Out of range"); itsSpooky(); } if (distance > 1 && distance < 60) { takeReading = 0; Serial.print("my distance is: "); Serial.println(distance); doSomething(); } if ( distance > 61){ // takeReading = 1; Serial.print("my distance > 60 and is: "); Serial.println(distance); itsSpooky(); } } void doSomething(){ servoBob.write(1500); delay(1000); ghostAudio.play("LAUGH5.WAV"); // ghostAudio.play("LAUGH3.WAV"); delay(8000); Serial.println("HI"); Serial.println("HI"); Serial.println("HI"); Serial.println("HI"); Serial.println("HI"); delay(1000); takeReading = 1; } void itsSpooky(){ servoBob.write(750); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); Serial.println("SPOOKY"); } ```
Steve_Massikker
6 years ago
I am very grateful for your work. The users of my project will find this great instruction very useful. Thanks Ashraf!
Anonymous user
7 years ago
Ashraf, you were doing so well, until you suggested supplying the servo from the 5V pin of the Arduino. This is never a good idea, as a servo can draw multiple times the current available on that pin, and it suggests to other beginners the 5V pin is available for generic motorised projects- NO, Typically the symptom will be crashes or restarts of the Arduino sketch, worst case it will burn out tracks on the Arduino board if the servo stalls, or is under load.
ashraf_minhaj
2 years ago
Thank you, I'll keep that in mind for future projects and add a Caution message in this project. Anyway, As this servo has no load there won't possibly be any crashes though.
Ardu-Fan
2 years ago
lastchancename; You already wrote, what I was going to write.
ashraf_minhaj
22 Followers
•22 Projects
12
16
How to Use ServoTimer2 Library (Simple Explain) Servo Sweep | Arduino Project Hub
Anonymous user
2 years ago
hello, iam trying to integrate your library in my sketch, because my servo doesnt move if i use servo.h with timerone.h Well, as soon as i try to compile it, (just your example to try if its working) i have this in IDE: Arduino:1.8.12 (Windows Store 1.8.33.0) (Windows 10), Scheda:"Arduino Uno" In file included from sketch\\EDI_TEST2_solo_servo.ino.cpp:1:0: C:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.33.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\cores\\arduino/Arduino.h:126:14: note: previous declaration as 'typedef bool boolean' typedef bool boolean; ^~~~~~~ exit status 1 Errore durante la compilazione per la scheda Arduino Uno. --------------------------------------------------------------------------------------------------- Do you think there is something wrong about compiling sketch with arduino uno? please let me know because iam stuck with it... thank you