This is my first arduino project and I am completely uninitiated in C++.
I have cobbled together a functioning robot control out of example sketches for; bluetooth light switch, servo sweep, L293D digital pin dual motor control.
It functions adequately as a concept but there are still a few hurdles to overcome.
Firstly, when the bluetooth input is received it turns confirmation on with Digitalwrite(#,HIGH) until another signal is received. I want RC car style input where the second u stop pressing the button the output stops.
I have tried the operator else if(inputString != "x")
this kills all other inputs like else if(inputString == "y")
and it still requires a new input to overwrite "x"
is there a (inputString == "nothing") operator to identify no signal?
I could get around this by sending end signals from the controller when the button stops being pressed. Is there a bluetooth app for android that will send a second signal when the button stops being pressed? I have noticed that the "arduino bluetooth controller" app only sends the signal when the button is released. This wont work for what i want, could anyone recommend a robust app that can send signal on button press as well as button release?
Secondly, I want to be able to write delays into servo functions without putting a delay in the void loop when used. This is so u can make a servo do something and then still receive inputs for other functions.
I have no idea how i would even approach this.
Thirdly, I want to move the servos left or right based on continuous input signal and write(pos) at the position the servo loses the direction input signal. I'm pretty sure I can already do this but I can't try it until i overcome the first hurdle as stated above.
My sketch to control an LED, 2 motors, and 2 servos.
#include <Servo.h>
Servo myservo1;
Servo myservo2;// create servo object to control a servo
int m1 = 3; // motor1, 1
int m2 = 6; // motor2, 1
int en1 = 4; // motor1, 2
int en2 = 7; // motor2, 2
int pos = 0; // variable to store the servo position
char junk;
String inputString="";
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(13, OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(8);
}
void loop()
{
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
Serial.println(inputString);
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(13, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(13, LOW);
}else if(inputString == "c"){
for (pos = 0; pos <= 160; pos += 2) { // goes to 180 degrees
myservo1.write(pos); // tell servo to go to position in variable 'pos'
}
}else if(inputString == "d"){
for (pos = 180; pos >= 10; pos -= 2) { // goes to 0 degrees
myservo1.write(pos); // tell servo to go to position in variable 'pos'
}
}else if(inputString == "e"){
for (pos = 85; pos >= 85; pos -= 2) { // goes to 90 degrees
myservo1.write(pos); // tell servo to go to position in variable 'pos'
}
}else if(inputString == "f"){ //incase of 'f' turn on both motors
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}else if(inputString == "g"){ //incase of 'g' turn on right motor
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}else if(inputString == "h"){ //incase of 'h' turn on left motor
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}else if(inputString == "i"){ //incase of 'i' stop both motors
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}else if(inputString == "j"){ //incase of 'j' sweep servo 2 back and forth
for (pos = 0; pos <= 160; pos += 2)
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay (1000);
for (pos = 160; pos >= 0; pos -= 2)
myservo2.write(pos); // tell servo to go to position in variable 'pos'
}
inputString = "";
}
}