I've written this code from examples ive seen. It compiles and everything but the thing that works is just the LCD screen with the sensor. Motors wont respond to bluetooth commands(using Bluetooth electronics app).
Im using the arduino uno and L298P Shield. Ive testes the motors before, without the bluetooth and they worked. I dont think its something with the way I connected things, but ofcourse that might be a possibility.
Some code explanations. I have a jeep toy that im using. It has 2 motors: first motor is for the front wheels, and it turns the wheels left or right, depending if its running backwards or forwards; second motor runs the back wheels without turning. So thats why I named the motorFront and the motorBack the way I did
Also, dont mind the last case "m", it doesnt exist
EDIT: Pretty sure the bluetooth commands are not sending anything to the arduino. I connected it to the program, but there is no response whatsoever
#include <AFMotor.h>
#include <Wire.h>
//#include <hd44780.h>
#include <hd44780_I2Cexp.h>
#define trigPin 12
#define echoPin 8
hd44780_I2Cexp lcd;
AF_DCMotor motorFront(1, MOTOR12_64KHZ);
AF_DCMotor motorBack(2, MOTOR12_64KHZ);
const int LCD_ROWS = 2;
const int LCD_COLS = 16;
char dataIn = 'S';
char det;
char determinant;
int vel = 255;
int check;
void setup() {
// put your setup code here, to run once:
lcd.begin(LCD_COLS, LCD_ROWS);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motorFront.setSpeed(0);
motorBack.setSpeed(0);
motorFront.run(RELEASE);
motorBack.run(RELEASE);
}
void loop() {
// put your main code here, to run repeatedly:
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance =(duration/2) / 29.1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Atstumas");
lcd.setCursor(12,0);
lcd.print(distance);
lcd.print("cm");
if (distance >=15)
{
lcd.setCursor(0,2);
lcd.print("Safe Zone :)");
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" STEP AWAY!!!");
lcd.setCursor(0,1);
lcd.print(" STEP AWAY!!!");
lcd.setCursor(0,2);
}
int check();
det = check(); //call check() subrotine to get the serial code
//serial code analysis
switch (det){
case 'F': // F, move forward
motorFront.setSpeed(0);
motorFront.run(RELEASE);
motorBack.setSpeed(vel);
motorBack.run(FORWARD);
// motorFront.run(FORWARD);
det = check();
break;
case 'B': // B, move back
motorFront.setSpeed(0);
motorFront.run(RELEASE);
motorBack.setSpeed(vel);
motorBack.run(BACKWARD);
//motorLeft.run(BACKWARD);
det = check();
break;
case 'L':// L, move wheels left
motorFront.setSpeed(vel);
motorBack.setSpeed(0);
motorFront.run(FORWARD);
motorBack.run(RELEASE);
det = check();
break;
case 'R': // R, move wheels right //FORWARD kol kas yra i kaire, BACKWARD yra i desine
motorFront.setSpeed(vel);
motorBack.setSpeed(0);
motorFront.run(BACKWARD);
motorBack.run(RELEASE);
det = check();
break;
case 'I': // I, turn right forward
motorFront.setSpeed(vel);
motorBack.setSpeed(vel/2);
motorFront.run(BACKWARD);
motorBack.run(FORWARD);
det = check();
break;
case 'J': // J, turn right back
motorFront.setSpeed(vel);
motorBack.setSpeed(vel/2);
motorFront.run(FORWARD);
motorBack.run(BACKWARD);
det = check();
break;
case 'G': // G, turn left forward
motorFront.setSpeed(vel);
motorBack.setSpeed(vel/2);
motorFront.run(FORWARD);
motorBack.run(FORWARD);
det = check();
break;
case 'H': // H, turn left back
motorFront.setSpeed(vel);
motorBack.setSpeed(vel/2);
motorFront.run(BACKWARD);
motorBack.run(BACKWARD);
det = check();
break;
case 'S':
// S, stop
motorFront.setSpeed(vel);
motorBack.setSpeed(vel);
motorFront.run(RELEASE);
motorBack.run(RELEASE);
det = check();
break;
case 'b':
//obstacle avoider robot
motorFront.setSpeed(vel); //set the speed of the motors, between 0-255
motorBack.setSpeed (vel);
long Aduration, Adistance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
Aduration = pulseIn(echoPin, HIGH);
Adistance = (Aduration/2) / 29.1;
if (Adistance < 25)
{ motorFront.setSpeed(vel);
motorBack.setSpeed(vel);
motorFront.run(FORWARD); // Turn as long as there's an obstacle ahead.
motorBack.run (FORWARD);
}
else {
delay (15);
motorFront.setSpeed(0);
motorBack.setSpeed(vel);
motorFront.run(RELEASE); //if there's no obstacle ahead, Go Forward!
motorBack.run(FORWARD);
}
break;
}
}
int check(){
if (Serial.available() > 0){// if there is valid data in the serial port
dataIn = Serial.read();// stores data into a varialbe
//check the code
if (dataIn == 'F'){//Forward
determinant = 'F';
}
else if (dataIn == 'B'){//Backward
determinant = 'B';
}
else if (dataIn == 'L'){//Left
determinant = 'L';
}
else if (dataIn == 'R'){//Right
determinant = 'R';
}
else if (dataIn == 'I'){//Froward Right
determinant = 'I';
}
else if (dataIn == 'J'){//Backward Right
determinant = 'J';
}
else if (dataIn == 'G'){//Forward Left
determinant = 'G';
}
else if (dataIn == 'H'){//Backward Left
determinant = 'H';
}
else if (dataIn == 'S'){//Stop
determinant = 'S';
}
else if (dataIn == '0'){//Speed 0
vel = 0;
}
else if (dataIn == '1'){//Speed 25
vel = 25;
}
else if (dataIn == '2'){//Speed 50
vel = 50;
}
else if (dataIn == '3'){//Speed 75
vel = 75;
}
else if (dataIn == '4'){//Speed 100
vel = 100;
}
else if (dataIn == '5'){//Speed 125
vel = 125;
}
else if (dataIn == '6'){//Speed 150
vel = 150;
}
else if (dataIn == '7'){//Speed 175
vel = 175;
}
else if (dataIn == '8'){//Speed 200
vel = 200;
}
else if (dataIn == '9'){//Speed 225
vel = 225;
}
else if (dataIn == 'b'){//Extra On
determinant = 'b';
}
else if (dataIn == 'm'){//Extra On
determinant = 'm';
}
}
return determinant;
}