So i have made a 2WD Orangepip (like an arduino) bluetooth controlled car and i am having problems with the motors functioning.
The bluetooth does work as the serial monitor is printing the values i press on the joystick app. However, the motors are not working with the following codes. The motors do work in general as i did 3 other projects.
code:
#include <SoftwareSerial.h>
// Starting of Program
int MOT_A1_PIN = 5;
int MOT_A2_PIN = 6;
int MOT_B1_PIN = 9;
int MOT_B2_PIN = 10;
char val;
int leftServoSpeed = 0;
int rightServoSpeed = 0;
void setup()
{
pinMode(MOT_A1_PIN, OUTPUT);
pinMode(MOT_A2_PIN, OUTPUT);
pinMode(MOT_B1_PIN, OUTPUT);
pinMode(MOT_B2_PIN, OUTPUT);
// Start with drivers off, motors coasting.
digitalWrite(MOT_A1_PIN, LOW);
digitalWrite(MOT_A2_PIN, LOW);
digitalWrite(MOT_B1_PIN, LOW);
digitalWrite(MOT_B2_PIN, LOW);
Serial.begin(9600);
}
void set_motor_pwm(int pwm, int IN1_PIN, int IN2_PIN)
{
if (pwm < 0) { // reverse speeds
analogWrite(IN1_PIN, -pwm);
digitalWrite(IN2_PIN, LOW);
} else { // stop or forward
digitalWrite(IN1_PIN, LOW);
analogWrite(IN2_PIN, pwm);
}
}
void set_motor_currents(int pwm_A, int pwm_B)
{
set_motor_pwm(pwm_A, MOT_A1_PIN, MOT_A2_PIN);
set_motor_pwm(pwm_B, MOT_B1_PIN, MOT_B2_PIN);
}
void spin_and_wait(int pwm_A, int pwm_B, int duration)
{
set_motor_currents(pwm_A, pwm_B);
delay(duration);
}
void loop()
{
while (Serial.available() > 0)
{
val = Serial.read();
Serial.println(val);
}
if( val == 'a' || 'A') // Forward
{
leftServoSpeed = 170;
rightServoSpeed = 140;
}
else if(val == 'c' || 'C') // Backward
{
leftServoSpeed = -170;
rightServoSpeed = -140;
}
else if(val == 'd' || 'D') //Left
{
leftServoSpeed = 170;
rightServoSpeed = -140;
}
else if(val == 'b' || 'B') //Right
{
leftServoSpeed = -170;
rightServoSpeed = 140;
}
else if(val == 'g' || 'G') //Stop
{
leftServoSpeed = 0;
rightServoSpeed = 0;
}
else if(val == 'a' && 'f' ) //Forward Right
{
leftServoSpeed = 170;
rightServoSpeed = 120;
}
else if(val == 'd' && 'f') //Backward Right
{
leftServoSpeed = -170;
rightServoSpeed = -120;
}
else if(val == 'a' && 'h') //Forward Left
{
leftServoSpeed = 120;
rightServoSpeed = 170;
}
else if(val == 'c' && 'h') //Backward Left
{
leftServoSpeed = -120;
rightServoSpeed = -170;
}
}