Answer the question
In order to leave comments, you need to log in
When connecting a servo to arduino, the functions of the code stop working. What to do?
We have several functions in the program. One of them should be compressing the grip of the manipulator with a Servo (Model: Towerpro mg996R). But, unfortunately, when you connect it to the program, other functions stop working. Food is carried out from batteries (Power of 12 Volts). Arduino UNO board is used.
Code example:
#define DRIVE_D0 3 //DRIVE_D0
#define DRIVE_D1 5 //DRIVE_D1
#define DRIVE_D2 6 //DRIVE_D2
#define DRIVE_D3 9 //DRIVE_D3
#define button_UP 4
#define button_LEFT 12
#include <Servo.h>
Servo s1;
int speed_motor = 80;
String a;
// variables will change:
int button_State_UP = 0;
int button_State_LEFT = 0;
void full_stop_motors()
{
// Задаём направление
digitalWrite (DRIVE_D0, 0);
digitalWrite (DRIVE_D1, 0);
digitalWrite (DRIVE_D2, 0);
digitalWrite (DRIVE_D3, 0);
}
void press_servo()
{
s1.write(60);
}
void let_off_servo()
{
s1.write(0);
}
void up()
{
// Задаём направлениеd
while (button_State_UP != 1) {
button_State_UP = digitalRead(button_UP);
analogWrite (DRIVE_D0, 0);
analogWrite (DRIVE_D1, speed_motor);
}
full_stop_motors();
}
void down()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void ydown()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void turn_left()
{
while (button_State_LEFT != 1) {
button_State_LEFT = digitalRead(button_LEFT);
analogWrite (DRIVE_D2, 0);
analogWrite (DRIVE_D3, speed_motor);
}
full_stop_motors();
}
void turn_right_c()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1300);
full_stop_motors();
}
void turn_right()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1400);
full_stop_motors();
}
void centre()
{
turn_left();
full_stop_motors();
turn_right_c();
full_stop_motors();
}
void setup()
{
pinMode(button_UP, INPUT);
pinMode(button_LEFT, INPUT);
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
pinMode(DRIVE_D0, OUTPUT);
pinMode(DRIVE_D1, OUTPUT);
pinMode(DRIVE_D2, OUTPUT);
pinMode(DRIVE_D3, OUTPUT);
full_stop_motors();
Serial.begin(9600);
}
void loop() {
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
if (Serial.available()) { //From RPi to Arduino
char r = Serial.read(); //conveting the value of chars to integer
if (r == 'r') {
turn_right();
Serial.println(r);
}
else if (r == 'l') {
turn_left();
Serial.println(r);
}
else if (r == 'y') {
ydown();
Serial.println(r);
}
else if (r == 'u') {
up();
Serial.println(r);
}
else if (r == 'd') {
down();
Serial.println(r);
}
else if (r == 's') {
full_stop_motors();
Serial.println(r);
}
else if (r == 'c') {
centre();
Serial.println(r);
}
else if (r == '*') {
press_servo();
Serial.println(r);
}
else if (r == '/') {
let_off_servo();
Serial.println(r);
}
}
}
#define DRIVE_D0 3 //DRIVE_D0
#define DRIVE_D1 5 //DRIVE_D1
#define DRIVE_D2 6 //DRIVE_D2
#define DRIVE_D3 9 //DRIVE_D3
#define button_UP 4
#define button_LEFT 12
#include <Servo.h>
Servo s1;
int speed_motor = 80;
String a;
// variables will change:
int button_State_UP = 0; // variable for reading the pushbutton status
int button_State_LEFT = 0;
void full_stop_motors()
{
// Задаём направление
digitalWrite (DRIVE_D0, 0);
digitalWrite (DRIVE_D1, 0);
digitalWrite (DRIVE_D2, 0);
digitalWrite (DRIVE_D3, 0);
}
void press_servo()
{
s1.write(60);
}
void let_off_servo()
{
s1.write(0);
}
void up()
{
// Задаём направлениеd
while (button_State_UP != 1) {
button_State_UP = digitalRead(button_UP);
analogWrite (DRIVE_D0, 0);
analogWrite (DRIVE_D1, speed_motor);
}
full_stop_motors();
}
void down()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void ydown()
{
// Задаём направление
analogWrite (DRIVE_D0, speed_motor);
analogWrite (DRIVE_D1, 0);
delay(15000);
full_stop_motors();
}
void turn_left()
{
while (button_State_LEFT != 1) {
button_State_LEFT = digitalRead(button_LEFT);
analogWrite (DRIVE_D2, 0);
analogWrite (DRIVE_D3, speed_motor);
}
full_stop_motors();
}
void turn_right_c()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1300);
full_stop_motors();
}
void turn_right()
{
// Задаём направление
analogWrite (DRIVE_D2, speed_motor);
analogWrite (DRIVE_D3, 0);
delay(1400);
full_stop_motors();
}
void centre()
{
turn_left();
full_stop_motors();
turn_right_c();
full_stop_motors();
}
void setup()
{
pinMode(button_UP, INPUT);
pinMode(button_LEFT, INPUT);
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
pinMode(DRIVE_D0, OUTPUT);
pinMode(DRIVE_D1, OUTPUT);
pinMode(DRIVE_D2, OUTPUT);
pinMode(DRIVE_D3, OUTPUT);
full_stop_motors();
Serial.begin(9600);
}
void loop() {
button_State_LEFT = digitalRead(button_LEFT);
button_State_UP = digitalRead(button_UP);
if (Serial.available()) { //From RPi to Arduino
char r = Serial.read(); //conveting the value of chars to integer
if (r == 'r') {
turn_right();
Serial.println(r);
}
else if (r == 'l') {
turn_left();
Serial.println(r);
}
else if (r == 'y') {
ydown();
Serial.println(r);
}
else if (r == 'u') {
up();
Serial.println(r);
}
else if (r == 'd') {
down();
Serial.println(r);
}
else if (r == 's') {
full_stop_motors();
Serial.println(r);
}
else if (r == 'c') {
centre();
Serial.println(r);
}
else if (r == '*') {
press_servo();
Serial.println(r);
}
else if (r == '/') {
let_off_servo();
Serial.println(r);
}
}
}
Answer the question
In order to leave comments, you need to log in
From the assumptions:
- The servo library occupies the same interrupt that the button handler uses.
- Noises on the power supply, false positives of the button
It is too poorly described what exactly stops working.
Clean up the code from repetitions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question