I am trying to control a robot through bluetooth and want to be able to actively change either sides speed by adding ex 10 to it but can't figure out how to use if correctly and how if changes the int.
This is my code right now
#include <DualMAX14870MotorShield.h>
char t;
#define LED_PIN 13
DualMAX14870MotorShield motors;
void setup() {
int speedR = 0;
int speedL = 0;
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
if(Serial.available()){
t = Serial.read();
Serial.println(t);
}
{
if(t == 's') //set speed
int speedR = +100;
}
{
if(t == 'b') //stop all
int speedR = 0;
}
{
if(t == 'r') //right
int speedR = speedR + 100;
}
int speedR = speedR;
int speedL = speedL;
motors.setM1Speed(speedR);
motors.setM2Speed(speedL);
}
your code is riddled with mistakes so I take it you're a noob to this!
anyway, basing myself on the DualMAX14870MotorShield example code and your serial commands, maybe this is what you intended:
(Compiles, NOT tested!)
#include <DualMAX14870MotorShield.h>
#define LED_PIN 13
DualMAX14870MotorShield motors; //instate object to control MAX14870 Motor Shield
//declare global variable to hold set motor speeds
int speedR = 0;
int speedL = 0;
void stopIfFault()
{
if (motors.getFault())
{
Serial.println("Driver Fault Detected!");
while (1) //blink LED forever
{
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
}
}
}
void MotorControl(char t) {
switch (t) { //using switch case to check 't' against expected characters
case 's':
//increment both R and L speed speeds by 10
speedR += 10;
speedL += 10;
break;
case 'b':
//stop all
speedR = 0;
speedL = 0;
break;
case 'r':
//increment R speed only by 10
speedR += 10;
break;
case 'l':
//increment L speed only by 10
speedL += 10;
break;
}
if(speedR>400) speedR=400; //Clip speedR if it exceeds maximum allowable value for 400
if(speedL>400) speedL=400; //Clip speedL if it exceeds maximum allowable value for 400
Serial.println(speedR); //print current R speed value to serial monitor
Serial.println(speedL); //print current L speed value to serial monitor
motors.setM1Speed(speedR); //set Motor 1 speed
motors.setM2Speed(speedL); //set Motor 2 speed
}
void setup() {
Serial.begin(9600);
motors.enableDrivers();
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
char t;
if (Serial.available()) { //check if Serial data is available
t = Serial.read(); //read one character from data
Serial.println(t); //output characted to serial monitor
MotorControl(t); //pass character to routine to operate motors
}
stopIfFault();
delay(2);
}
The code worked perfectly, one problem I had is that whenever I turned it on it would show a driver fault so for now I removed that part of the code.
This is my first serious arduino project so I kind of thought it would be pretty bad.
Thanks again for the help!