DC motor and Servo
Page 1 of 1
DC motor and Servo
I trying to make my app that well allow the dc motor to run at three different speeds using three different push buttons ( I actually got this bit working the way I want it) the problem I'm having is I also want to have servo being operated by a slider ( I found a code for that which works) but when I try to put the two together the dc motor wont move at all and the servo tries to move when I use the buttons.
This is the code that I tried using:
#include <SoftwareSerial.h>
#include <Servo.h>
int bluetoothTx = 2; // TX-O pin of BT module to Arduino pin2
int bluetoothRx = 3; // RX-I pin of B module to Arduino pin3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int motorSpeedPin = 9;
Servo myServo1;
char cmd[100];
int cmdIndex;
boolean cmdStartsWith(char *st) {
for(int i=0; ; i++) {
if(st[i]==0) return true;
if(cmd[i]==0) return false;
if(cmd[i]!=st[i]) return false;;
}
return false;
}
void exeCmd() {
if(strcmp(cmd, "speed1 0")==0) analogWrite(motorSpeedPin, 0);
if(strcmp(cmd, "speed1 1")==0) analogWrite(motorSpeedPin, 90);
if(strcmp(cmd, "speed2 0")==0) analogWrite(motorSpeedPin, 0);
if(strcmp(cmd, "speed2 1")==0) analogWrite(motorSpeedPin, 125);
if(strcmp(cmd, "speed3 0")==0) analogWrite(motorSpeedPin, 0);
if(strcmp(cmd, "speed3 1")==0) analogWrite(motorSpeedPin, 200);
if( cmdStartsWith("servo1 ") ) {
int val = atoi(cmd+7);
myServo1.writeMicroseconds(val);
}
void setup() {
bluetooth.begin(9600);
pinMode(motorSpeedPin, OUTPUT);
analogWrite(motorSpeedPin, 0);
myServo1.attach(5, 1000, 1500);
cmdIndex = 0;
}
void loop() {
if(bluetooth.available()) {
char c = (char)bluetooth.read();
if(c=='\n') {
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else {
cmd[cmdIndex] = c;
if(cmdIndex<99) cmdIndex++;
}
}
}
This is the code that I tried using:
#include <SoftwareSerial.h>
#include <Servo.h>
int bluetoothTx = 2; // TX-O pin of BT module to Arduino pin2
int bluetoothRx = 3; // RX-I pin of B module to Arduino pin3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int motorSpeedPin = 9;
Servo myServo1;
char cmd[100];
int cmdIndex;
boolean cmdStartsWith(char *st) {
for(int i=0; ; i++) {
if(st[i]==0) return true;
if(cmd[i]==0) return false;
if(cmd[i]!=st[i]) return false;;
}
return false;
}
void exeCmd() {
if(strcmp(cmd, "speed1 0")==0) analogWrite(motorSpeedPin, 0);
if(strcmp(cmd, "speed1 1")==0) analogWrite(motorSpeedPin, 90);
if(strcmp(cmd, "speed2 0")==0) analogWrite(motorSpeedPin, 0);
if(strcmp(cmd, "speed2 1")==0) analogWrite(motorSpeedPin, 125);
if(strcmp(cmd, "speed3 0")==0) analogWrite(motorSpeedPin, 0);
if(strcmp(cmd, "speed3 1")==0) analogWrite(motorSpeedPin, 200);
if( cmdStartsWith("servo1 ") ) {
int val = atoi(cmd+7);
myServo1.writeMicroseconds(val);
}
void setup() {
bluetooth.begin(9600);
pinMode(motorSpeedPin, OUTPUT);
analogWrite(motorSpeedPin, 0);
myServo1.attach(5, 1000, 1500);
cmdIndex = 0;
}
void loop() {
if(bluetooth.available()) {
char c = (char)bluetooth.read();
if(c=='\n') {
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else {
cmd[cmdIndex] = c;
if(cmdIndex<99) cmdIndex++;
}
}
}
Revexey- Posts : 1
Join date : 2017-04-22
Similar topics
» 2 servo (bldc) with slider + 1 servo with accelerometer of mobile
» accelerometer & dc-motor
» Wemos D1 and Motor Shield.
» Code for Brushless motor+ESC
» Two servo ESP12
» accelerometer & dc-motor
» Wemos D1 and Motor Shield.
» Code for Brushless motor+ESC
» Two servo ESP12
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum