(This is fixed. Thanks for the tips) Hi, I am making an Arduino controlled bluetooth car for my endproject of school but I'm having some trouble. My bluetooth module worked perfectly before using a terminal and my own Mit app inventor app, but now it doesn't anymore and only puts out black diamond questionmarks. Also when I'm uploading the code, my dc-motor goes forward in 2 little shocks and then stops for no reason. Can anyone help me with this?
#include <SoftwareSerial.h>
char t;
int trigPin = 6; // TRIG pin
int echoPin = 7; // ECHO pin
int input1 = 9;
int input2 = 8;
int input3 = 2;
int input4 = 3;
int ledRed1 = 10;
int ledRed2 = 11;
int ledYellow1 = 12;
int ledYellow2 = 13;
const byte rxPin = 5;
const byte txPin = 4;
SoftwareSerial mySerial(rxPin, txPin);
float duration_us, distance_cm;
bool distance = false;
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(9, OUTPUT); //left motors forward
pinMode(8, OUTPUT); //left motors reverse
pinMode(2, OUTPUT); //right motors forward
pinMode(3, OUTPUT); //right motors reverse
pinMode(ledRed1, OUTPUT); //Led
pinMode(ledRed2, OUTPUT); //Led
pinMode(ledYellow1, OUTPUT); //Led
pinMode(13, OUTPUT); //Led
mySerial.begin(9600);
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // configure the trigger pin to output mode
pinMode(echoPin, INPUT); // configure the echo pin to input mode
digitalWrite(ledRed1, HIGH);
digitalWrite(ledRed2, HIGH);
digitalWrite(ledYellow1, HIGH);
digitalWrite(ledYellow2, HIGH);
digitalWrite(input3, HIGH);
digitalWrite(input2, LOW);
}
void loop() {
// If any data is available at the Bluetooth Serial Port
if (mySerial.available()) {
t = Serial.read();
Serial.println();
Serial.write(t);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(50);
digitalWrite(trigPin, LOW);
duration_us = pulseIn(echoPin, HIGH); // measure duration of pulse from ECHO pin
distance_cm = 0.017218 * duration_us; // calculate the distance
//Serial.print("distance: "); // print the value to Serial Monitor
//Serial.print(distance_cm);
//Serial.println(" cm");
if (distance_cm >= 10)
distance = true;
else
distance = false;
if (t == 'F' && distance == false) { //move forward
digitalWrite(input3, HIGH);
digitalWrite(input4, LOW);
}
else if (t == 'B') { //move reverse
digitalWrite(input3, LOW);
digitalWrite(input4, HIGH);
}
else if (t == 'R') { //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
}
else if (t == 'L') { //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
}
else if (t == '1') { //turn led on or off)
digitalWrite(ledRed1, HIGH);
digitalWrite(ledRed2, HIGH);
digitalWrite(ledYellow1, HIGH);
digitalWrite(ledYellow2, HIGH);
}
else if (t == '0') {
digitalWrite(ledRed1, LOW);
digitalWrite(ledRed2, LOW);
digitalWrite(ledYellow1, LOW);
digitalWrite(ledYellow2, LOW);
}
else if(t == 'S' || distance == true){ //STOP (all motors stop)
digitalWrite(input1,LOW);
digitalWrite(input2,LOW);
digitalWrite(input3,LOW);
digitalWrite(input4 ,LOW);
}
delay(10);
}