Hello!
I really need help with my project.
I am making a Bluetooth-controlled Car/Tank.
I am using the Serial.read() to drive the car. i have created an app on mit app inventor and for the motors to spin one way it sends a "F" and for them to spin in reverse it sends a "R" so that bit is fine i know how to do that but i also have a slider to control the position of a servo (all the slider does is send a 0 up to 50) so i have "myServo.write (Serial.read());
and that doesnt work obviously because the servo is trying to go the the position "F" and "R"
i need help to make it work
any help would be awesome!
Thanks
Here is all my code
#include <Servo.h>
int Direction = 0;
int ReadVal = 0;
const int trigPin = 7;
const int echoPin = 6;
Servo AimServo;
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
void setup() {
pinMode(13, OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
AimServo.attach(9);
Serial.begin(9600);
}
void loop() {
if(Serial.available()){
Direction = Serial.read();
}
if(Direction == 'F'){ //move forward (all motors rotate in forward direction)
digitalWrite(13,HIGH);
digitalWrite(11,HIGH);
}
else if(Direction == 'B'){ //move reverse (all motors rotate in reverse direction)
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
}
else if(Direction == 'R'){ //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
digitalWrite(11,HIGH);
}
else if(Direction == 'L'){ //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
digitalWrite(13,HIGH);
}
else if(Direction == 'O'){
digitalWrite(13, LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
}
if(ReadVal == "10"){
AimServo.write (10);
}
else if(ReadVal == "20"){
AimServo.write (20);
}
else if(ReadVal == "30"){
AimServo.write (30);
}
else if(ReadVal == "40"){
AimServo.write (40);
}
else if(ReadVal == "50"){
AimServo.write (50);
}
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
delay(100);
if (cm <= 10) {
digitalWrite(13,LOW);
digitalWrite(11,LOW);
delay (500);
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
delay (1000);
digitalWrite(12,LOW);
digitalWrite(10,LOW);
}
}