├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── githubci.yml ├── Adafruit_SoftServo.cpp ├── Adafruit_SoftServo.h ├── README.md ├── code-of-conduct.md ├── examples └── TrinketKnob │ └── TrinketKnob.ino ├── library.properties └── license.txt /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py trinket_5v 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit SoftServo Arduino Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /Adafruit_SoftServo.cpp: -------------------------------------------------------------------------------- 1 | // This is an ultra simple software servo driver. For best 2 | // results, use with a timer0 interrupt to refresh() all 3 | // your servos once every 20 milliseconds! 4 | // Written by Limor Fried for Adafruit Industries, BSD license 5 | 6 | #if ARDUINO >= 100 7 | #include "Arduino.h" 8 | #else 9 | #include "WProgram.h" 10 | #endif 11 | 12 | #include "Adafruit_SoftServo.h" 13 | /** 14 | * @brief Construct a new Adafruit_SoftServo::Adafruit_SoftServo object 15 | * 16 | */ 17 | Adafruit_SoftServo::Adafruit_SoftServo(void) { 18 | isAttached = false; 19 | servoPin = 255; 20 | angle = 90; 21 | } 22 | /** 23 | * @brief Attacht to a supplied pin 24 | * 25 | * @param pin The pin to attach to for controlling the servo 26 | */ 27 | void Adafruit_SoftServo::attach(uint8_t pin) { 28 | servoPin = pin; 29 | angle = 90; 30 | isAttached = true; 31 | pinMode(servoPin, OUTPUT); 32 | } 33 | /** 34 | * @brief Detach from the supplied pin 35 | * 36 | */ 37 | void Adafruit_SoftServo::detach(void) { 38 | isAttached = false; 39 | pinMode(servoPin, INPUT); 40 | } 41 | /** 42 | * @brief Get the attachment status of the pin 43 | * 44 | * @return boolean true: a pin is attached false: no pin is attached 45 | */ 46 | boolean Adafruit_SoftServo::attached(void) { return isAttached; } 47 | /** 48 | * @brief Update the servo's angle setting and the corresponding pulse width 49 | * 50 | * @param a The target servo angle 51 | */ 52 | void Adafruit_SoftServo::write(uint8_t a) { 53 | angle = a; 54 | 55 | if (!isAttached) 56 | return; 57 | micros = map(a, 0, 180, 1000, 2000); 58 | } 59 | /** 60 | * @brief Pulse the control pin for the amount of time determined when the angle 61 | * was set with `write` 62 | * 63 | */ 64 | void Adafruit_SoftServo::refresh(void) { 65 | digitalWrite(servoPin, HIGH); 66 | delayMicroseconds(micros); 67 | digitalWrite(servoPin, LOW); 68 | } 69 | -------------------------------------------------------------------------------- /Adafruit_SoftServo.h: -------------------------------------------------------------------------------- 1 | // This is an ultra simple software servo driver. For best 2 | // results, use with a timer0 interrupt to refresh() all 3 | // your servos once every 20 milliseconds! 4 | // Written by Limor Fried for Adafruit Industries, BSD license 5 | 6 | #if ARDUINO >= 100 7 | #include "Arduino.h" 8 | #else 9 | #include "WProgram.h" 10 | #endif 11 | /** 12 | * @brief Class for basic software servo control 13 | * 14 | */ 15 | class Adafruit_SoftServo { 16 | public: 17 | Adafruit_SoftServo(void); 18 | void attach(uint8_t pin); 19 | void detach(); 20 | boolean attached(); 21 | void write(uint8_t a); 22 | void refresh(void); 23 | 24 | private: 25 | boolean isAttached; 26 | uint8_t servoPin, angle; 27 | uint16_t micros; 28 | }; 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Adafruit SoftServo [![Build Status](https://github.com/adafruit/Adafruit_SoftServo/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_SoftServo/actions) 3 | 4 | This is the Adafruit SoftServo Arduino Library for Arduino, an ultra simple software servo driver. For best results, use with a timer0 interrupt to `refresh()` all your servos once every 20 milliseconds! 5 | 6 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 7 | 8 | # Contributing 9 | 10 | Contributions are welcome! Please read our [Code of Conduct](https://github.com/adafruit/Adafruit_SoftServo/blob/master/CODE_OF_CONDUCT.md>) 11 | before contributing to help this project stay welcoming. 12 | 13 | ## Documentation and doxygen 14 | Documentation is produced by doxygen. Contributions should include documentation for any new code added. 15 | 16 | Some examples of how to use doxygen can be found in these guide pages: 17 | 18 | https://learn.adafruit.com/the-well-automated-arduino-library/doxygen 19 | 20 | https://learn.adafruit.com/the-well-automated-arduino-library/doxygen-tips 21 | 22 | Written by Written by Limor Fried for Adafruit Industries. 23 | 24 | BSD license, check license.txt for more information 25 | All text above must be included in any redistribution 26 | 27 | To install, use the Arduino Library Manager and search for "Adafruit SoftServo" and install the library. 28 | 29 | -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Adafruit Community Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and leaders pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level or type of 9 | experience, education, socio-economic status, nationality, personal appearance, 10 | race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | We are committed to providing a friendly, safe and welcoming environment for 15 | all. 16 | 17 | Examples of behavior that contributes to creating a positive environment 18 | include: 19 | 20 | * Be kind and courteous to others 21 | * Using welcoming and inclusive language 22 | * Being respectful of differing viewpoints and experiences 23 | * Collaborating with other community members 24 | * Gracefully accepting constructive criticism 25 | * Focusing on what is best for the community 26 | * Showing empathy towards other community members 27 | 28 | Examples of unacceptable behavior by participants include: 29 | 30 | * The use of sexualized language or imagery and sexual attention or advances 31 | * The use of inappropriate images, including in a community member's avatar 32 | * The use of inappropriate language, including in a community member's nickname 33 | * Any spamming, flaming, baiting or other attention-stealing behavior 34 | * Excessive or unwelcome helping; answering outside the scope of the question 35 | asked 36 | * Trolling, insulting/derogatory comments, and personal or political attacks 37 | * Public or private harassment 38 | * Publishing others' private information, such as a physical or electronic 39 | address, without explicit permission 40 | * Other conduct which could reasonably be considered inappropriate 41 | 42 | The goal of the standards and moderation guidelines outlined here is to build 43 | and maintain a respectful community. We ask that you don’t just aim to be 44 | "technically unimpeachable", but rather try to be your best self. 45 | 46 | We value many things beyond technical expertise, including collaboration and 47 | supporting others within our community. Providing a positive experience for 48 | other community members can have a much more significant impact than simply 49 | providing the correct answer. 50 | 51 | ## Our Responsibilities 52 | 53 | Project leaders are responsible for clarifying the standards of acceptable 54 | behavior and are expected to take appropriate and fair corrective action in 55 | response to any instances of unacceptable behavior. 56 | 57 | Project leaders have the right and responsibility to remove, edit, or 58 | reject messages, comments, commits, code, issues, and other contributions 59 | that are not aligned to this Code of Conduct, or to ban temporarily or 60 | permanently any community member for other behaviors that they deem 61 | inappropriate, threatening, offensive, or harmful. 62 | 63 | ## Moderation 64 | 65 | Instances of behaviors that violate the Adafruit Community Code of Conduct 66 | may be reported by any member of the community. Community members are 67 | encouraged to report these situations, including situations they witness 68 | involving other community members. 69 | 70 | You may report in the following ways: 71 | 72 | In any situation, you may send an email to . 73 | 74 | On the Adafruit Discord, you may send an open message from any channel 75 | to all Community Helpers by tagging @community helpers. You may also send an 76 | open message from any channel, or a direct message to @kattni#1507, 77 | @tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or 78 | @Andon#8175. 79 | 80 | Email and direct message reports will be kept confidential. 81 | 82 | In situations on Discord where the issue is particularly egregious, possibly 83 | illegal, requires immediate action, or violates the Discord terms of service, 84 | you should also report the message directly to Discord. 85 | 86 | These are the steps for upholding our community’s standards of conduct. 87 | 88 | 1. Any member of the community may report any situation that violates the 89 | Adafruit Community Code of Conduct. All reports will be reviewed and 90 | investigated. 91 | 2. If the behavior is an egregious violation, the community member who 92 | committed the violation may be banned immediately, without warning. 93 | 3. Otherwise, moderators will first respond to such behavior with a warning. 94 | 4. Moderators follow a soft "three strikes" policy - the community member may 95 | be given another chance, if they are receptive to the warning and change their 96 | behavior. 97 | 5. If the community member is unreceptive or unreasonable when warned by a 98 | moderator, or the warning goes unheeded, they may be banned for a first or 99 | second offense. Repeated offenses will result in the community member being 100 | banned. 101 | 102 | ## Scope 103 | 104 | This Code of Conduct and the enforcement policies listed above apply to all 105 | Adafruit Community venues. This includes but is not limited to any community 106 | spaces (both public and private), the entire Adafruit Discord server, and 107 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 108 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 109 | interaction at a conference. 110 | 111 | This Code of Conduct applies both within project spaces and in public spaces 112 | when an individual is representing the project or its community. As a community 113 | member, you are representing our community, and are expected to behave 114 | accordingly. 115 | 116 | ## Attribution 117 | 118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 119 | version 1.4, available at 120 | , 121 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 122 | 123 | For other projects adopting the Adafruit Community Code of 124 | Conduct, please contact the maintainers of those projects for enforcement. 125 | If you wish to use this code of conduct for your own project, consider 126 | explicitly mentioning your moderation policy or making a copy with your 127 | own moderation policy so as to avoid confusion. 128 | -------------------------------------------------------------------------------- /examples/TrinketKnob/TrinketKnob.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************* 2 | SoftServo sketch for Adafruit Trinket. Turn the potentiometer knob 3 | to set the corresponding position on the servo 4 | (0 = zero degrees, full = 180 degrees) 5 | 6 | Required library is the Adafruit_SoftServo library 7 | available at https://github.com/adafruit/Adafruit_SoftServo 8 | The standard Arduino IDE servo library will not work with 8 bit 9 | AVR microcontrollers like Trinket and Gemma due to differences 10 | in available timer hardware and programming. We simply refresh 11 | by piggy-backing on the timer0 millis() counter 12 | 13 | Required hardware includes an Adafruit Trinket microcontroller 14 | a servo motor or two, and a potentiometer (nominally 1Kohm to 100Kohm 15 | 16 | As written, this is specifically for the Trinket although it should 17 | be Gemma or other boards (Arduino Uno, etc.) with proper pin mappings 18 | 19 | Trinket: USB+ Gnd Pin #0 Pin #1 Pin #2 A1 20 | Connection: Servo+ - Servo1 Servo2 Potentiometer wiper 21 | 22 | *******************************************************************/ 23 | 24 | #include // SoftwareServo (works on non PWM pins) 25 | 26 | // We demonstrate two servos! 27 | #define SERVO1PIN 0 // Servo control line (orange) on Trinket Pin #0 28 | #define SERVO2PIN 1 // Servo control line (orange) on Trinket Pin #1 29 | 30 | #define POTPIN 1 // Potentiometer sweep (center) on Trinket Pin #2 (Analog 1) 31 | 32 | Adafruit_SoftServo myServo1, myServo2; //create TWO servo objects 33 | 34 | void setup() { 35 | // Set up the interrupt that will refresh the servo for us automagically 36 | OCR0A = 0xAF; // any number is OK 37 | TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!) 38 | 39 | myServo1.attach(SERVO1PIN); // Attach the servo to pin 0 on Trinket 40 | myServo1.write(90); // Tell servo to go to position per quirk 41 | myServo2.attach(SERVO2PIN); // Attach the servo to pin 1 on Trinket 42 | myServo2.write(90); // Tell servo to go to position per quirk 43 | delay(15); // Wait 15ms for the servo to reach the position 44 | } 45 | 46 | void loop() { 47 | int potValue; // variable to read potentiometer 48 | int servoPos; // variable to convert voltage on pot to servo position 49 | potValue=analogRead(POTPIN); // Read voltage on potentiometer 50 | servoPos = map(potValue, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) 51 | myServo1.write(servoPos); // tell servo to go to position 52 | myServo2.write(180-servoPos); // tell servo to go to position 53 | 54 | delay(15); // waits 15ms for the servo to reach the position 55 | } 56 | 57 | // We'll take advantage of the built in millis() timer that goes off 58 | // to keep track of time, and refresh the servo every 20 milliseconds 59 | volatile uint8_t counter = 0; 60 | SIGNAL(TIMER0_COMPA_vect) { 61 | // this gets called every 2 milliseconds 62 | counter += 2; 63 | // every 20 milliseconds, refresh the servos! 64 | if (counter >= 20) { 65 | counter = 0; 66 | myServo1.refresh(); 67 | myServo2.refresh(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit SoftServo 2 | version=1.0.4 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=A lightweight software servo library, designed for Trinket/Gemma but good for other Arduino-compats 6 | paragraph=A lightweight software servo library, designed for Trinket/Gemma but good for other Arduino-compats 7 | category=Device Control 8 | url=https://github.com/adafruit/Adafruit_SoftServo 9 | architectures=* 10 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2019 Limor Fried for Adafruit Industries 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holders nor the 14 | names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | --------------------------------------------------------------------------------