So, I've been working on a robot car controlled through wifi and on the process of putting a camera on a stand rotated and tilted by 2 servos. As soon as I make any servo objects the right side wheels stop working.
Is this supposed to happen? Or whats going on?
The code in which all the motors work below:
const int RightB = 8;
const int RightF = 9;
const int RightPWM = 10;
const int LeftPWM = 11;
const int LeftB = 12;
const int LeftF = 13;
const int TrigBack = 4;
const int EchoBack = 5;
const int TrigFront = 6;
const int EchoFront = 7;
long durationBack;
int distanceBack;
long durationFront;
int distanceFront;
int command;
int LastAction;
void setup() {
pinMode(RightB, OUTPUT);
pinMode(RightF, OUTPUT);
pinMode(RightPWM, OUTPUT);
pinMode(LeftPWM, OUTPUT);
pinMode(LeftB, OUTPUT);
pinMode(LeftF, OUTPUT);
pinMode(TrigBack, OUTPUT);
pinMode(EchoBack, INPUT);
pinMode(TrigFront, OUTPUT);
pinMode(EchoFront, INPUT);
digitalWrite(RightB, LOW);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 0);
analogWrite(LeftPWM, 0);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, LOW);
Serial.begin(115200);
}
void loop() {
if (LastAction == 1) {
distanceFront = frontsensor();
if (distanceFront < 30) {
forwardLimitStop();
}
}
if (LastAction == 2) {
distanceBack = backsensor();
if (distanceBack < 20) {
backwardLimitStop();
}
}
if (Serial.available() > 0) {
command = Serial.read();
switch (command) {
case 'F': {
if (LastAction != 6) {
forward();
}
break;
}
case 'B': {
if (LastAction != 7) {
backward();
}
break;
}
case 'R': {
right();
break;
}
case 'L': {
left();
break;
}
case 'S': {
stoprobot();
break;
}
}
}
}
int frontsensor() {
digitalWrite(TrigFront, LOW);
delayMicroseconds(2);
digitalWrite(TrigFront, HIGH);
delayMicroseconds(10);
digitalWrite(TrigFront, LOW);
durationFront = pulseIn(EchoFront, HIGH);
distanceFront= durationFront*0.034/2;
return distanceFront;
}
int backsensor() {
digitalWrite(TrigBack, LOW);
delayMicroseconds(2);
digitalWrite(TrigBack, HIGH);
delayMicroseconds(10);
digitalWrite(TrigBack, LOW);
durationBack = pulseIn(EchoBack, HIGH);
distanceBack= durationBack*0.034/2;
return distanceBack;
}
void forward() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, HIGH);
analogWrite(RightPWM, 180);
analogWrite(LeftPWM, 180);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, HIGH);
LastAction = 1;
}
void backward() {
digitalWrite(RightB, HIGH);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 150);
analogWrite(LeftPWM, 150);
digitalWrite(LeftB, HIGH);
digitalWrite(LeftF, LOW);
LastAction = 2;
}
void right() {
digitalWrite(RightB, HIGH);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 150);
analogWrite(LeftPWM, 150);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, HIGH);
LastAction = 3;
}
void left() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, HIGH);
analogWrite(RightPWM, 150);
analogWrite(LeftPWM, 150);
digitalWrite(LeftB, HIGH);
digitalWrite(LeftF, LOW);
LastAction = 4;
}
void stoprobot() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 0);
analogWrite(LeftPWM, 0);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, LOW);
LastAction = 5;
}
void forwardLimitStop() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 0);
analogWrite(LeftPWM, 0);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, LOW);
LastAction = 6;
}
void backwardLimitStop() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 0);
analogWrite(LeftPWM, 0);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, LOW);
LastAction = 7;
}
But in the code on the reply (original message was too long =) the RightB and LeftB motors stop working:
Also have ultrasonic sensors on both sides of the robot. Motor controller is L298N. Wifi module is ESP8266 ESP-01 programmed to just relay messages to arduino.
Tried 2 different arduino boards. Uno r3 and duemilanove.
Any help or explanation much appreciated.
Will try to track down which of the 3 pins is the problem causer tomorrow(or sunday). But posted this before if there is a reasonable explanation.