Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
/* April 4, 2019 Reddit motor + servo driver with buttons for each Board = Uno This sketch has two buttons. The buttons are set to function so that they will ONLY change when going from the unpressed-to-pressed state. One button turns a DC motor on and off, and the other button turns a servo on and off. When the servo is on, it moves back and forth between two settings. */ bool use_servo_change_messages = true; // This sketch prints some serial messages for testing purposes. // The servo will print a message when it changes value and when it reverses. // If the serial messages causes the servo to run jittery then you can turn those serial messages off by setting this value to false. // The other serial messages don't turn off but they should not cause any issues. int dc_motor_button_pin = 7; // This is where the button for the DC motor is connected. int servo_button_pin = 8; // This is where the button for the servo is connected. // Both above pins are using INPUT_PULLUP, so they should connect to a ground pin when pressed. int dc_motor_pin = 3; // The digital output to the DC motor controller is connected here. int servo_pin = 5; // The servo control pin is connected here. int servo_interval_time = 350; // This is the time interval that the servo will change at, in milliseconds. int servo_change_value = 10; // This is the amount that the servo position will change, at each servo_interval_time interval. int servo_current_value = 30; // This is the starting point for the servo. It should be a number between the minimum and maximum servo values below. int servo_minimum_value = 30; // This is the low end of where you want the servo to stop at. Zero is the lowest value you can put here. int servo_maximum_value = 200; // This is the high end of where you want the servo to stop at. 255 is the highest value you can put here. int servo_direction = 1; // This variable will flip back and forth between 1 and -1. // Setting it to 1 will make the servo begin by turning upwards (in position value) and setting it to -1 will make the servo begin by turning downwards. int button_debounce_time = 300; // This is to prevent the buttons from bouncing. // The variables above, you can change the values of. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ // The variables below, do not change the values of. int dc_motor_button_previous_state = 0; int dc_motor_button_current_state = 0; int servo_button_previous_state = 0; int servo_button_current_state = 0; bool buttons_enabled = true; unsigned long button_press_begin_time = 0; unsigned long button_press_current_time = 0; int dc_motor_enabled = 0; int servo_enabled = 0; bool servo_interval_enabled = true; unsigned long servo_interval_begin_time = 0; unsigned long servo_interval_current_time = 0; void setup() { Serial.begin(9600); pinMode(dc_motor_button_pin, INPUT_PULLUP); pinMode(servo_button_pin, INPUT_PULLUP); analogWrite(servo_pin, servo_current_value); // This causes the servo to immediately go to its initial position. pinMode(dc_motor_pin, OUTPUT); digitalWrite(dc_motor_pin, LOW); Serial.println("Exiting setup()"); } void loop() { if (buttons_enabled == true) { dc_motor_button_current_state = 0; if (digitalRead(dc_motor_button_pin) == LOW) { dc_motor_button_current_state = 1; } if (dc_motor_button_previous_state != dc_motor_button_current_state) { button_pressed(); } if (dc_motor_button_previous_state == 0) { if (dc_motor_button_current_state == 1) { if (dc_motor_enabled == 1) { dc_motor_enabled = 0; Serial.println("DC motor = OFF"); } else { dc_motor_enabled = 1; Serial.println("DC motor = ON"); } digitalWrite(dc_motor_pin, dc_motor_enabled); //button_pressed(); } } dc_motor_button_previous_state = dc_motor_button_current_state; servo_button_current_state = 0; if (digitalRead(servo_button_pin) == LOW) { servo_button_current_state = 1; } if (servo_button_previous_state != servo_button_current_state) { button_pressed(); } if (servo_button_previous_state == 0) { if (servo_button_current_state == 1) { if (servo_enabled == 1) { servo_enabled = 0; Serial.println("servo = OFF"); } else { servo_enabled = 1; Serial.println("servo = ON"); } //button_pressed(); } } servo_button_previous_state = servo_button_current_state; } else { button_press_current_time = millis(); if (button_press_current_time > button_press_begin_time) { if (button_press_current_time >= (button_press_begin_time + button_debounce_time)) { buttons_enabled = true; } } else { button_press_begin_time = millis(); } } if (servo_enabled == 1) { if (servo_interval_enabled == true) { change_servo_value(servo_change_value, servo_direction); check_current_servo_value(); analogWrite(servo_pin, servo_current_value); if (use_servo_change_messages == true) { Serial.print("servo value = "); Serial.println(servo_current_value); } servo_interval_begin_time = millis(); servo_interval_enabled = false; } else { servo_interval_current_time = millis(); if (servo_interval_current_time >= servo_interval_begin_time) { if (servo_interval_current_time >= (servo_interval_begin_time + servo_interval_time)) { servo_interval_enabled = true; } } else { servo_interval_begin_time = millis(); } } } } // end of main loop() void button_pressed() { buttons_enabled = false; button_press_begin_time = millis(); } void change_servo_value(int changeValue, int servoDirection) { // This changes the servo value up or down, depending on if servoDirection is 1 or -1. servo_current_value = servo_current_value + (changeValue * servoDirection); } void check_current_servo_value() { // This prevents the servo from turning past the minimum and maximum values, // and changes the direction when it hits either of the end values. if (servo_current_value <= servo_minimum_value) { servo_current_value = servo_minimum_value; flip_servo_direction(); } if (servo_current_value >= servo_maximum_value) { servo_current_value = servo_maximum_value; flip_servo_direction(); } } void flip_servo_direction() { // Because the direction control value is 1 or -1, you can change it to the "other" value just by multiplying it by -1. servo_direction = servo_direction * -1; if (use_servo_change_messages == true) { Serial.println("servo = reversing"); } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
9 hours ago | 1.88 KB
Untitled
9 hours ago | 0.67 KB
Untitled
9 hours ago | 3.33 KB
Untitled
9 hours ago | 1.00 KB
Untitled
9 hours ago | 0.33 KB
Untitled
9 hours ago | 1.55 KB
disable countdown timer in cart for drop pric...
PHP | 17 hours ago | 0.23 KB
bcachefs strange error
20 hours ago | 1.29 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!