Hello
I'm intresting in the working off the arduino and start learning. Now i start a litlle project to move a verhicle over a track forward and backwards with a DC motor and 2 LDR module.
I wrote a script witch working good on 1 arduino. When i connect it to a Arduino UNO. The DC motor is running and when i activate LDR module the motor turns backwards. when i activate LDR module 2 it stop for 5 sec and the motor turns forwards. Here is my script
//Pins
const int MO1 = 6;
const int MO2 = 7;
const int PWM = 5;
const int LDR = A0;
const int LDR1 = A1;
//Vars
int LDRValue = 0;
int LDRValue1 = 0;
int LightSensorCounter = 1;
unsigned long previousMillis = 0;
//Settings
int lightSensitivity = 500;
int lightSensitivity1 = 500;
int interval = 3000;
void setup()
{
Serial.begin(9600);
pinMode(PWM, OUTPUT);
pinMode(MO1, OUTPUT);
pinMode(MO2, OUTPUT);
}
void loop() {
moveforward();
}
void moveforward()
{
int ldrValNow;
ldrValNow = analogRead(LDR);
if (ldrValNow > lightSensitivity) {
if (LDRValue <= lightSensitivity) {
LightSensorCounter++;
}
}
LDRValue = ldrValNow;
if (LightSensorCounter % 2 == 0) {
movebackward();
}
else {
digitalWrite(MO1, LOW);
digitalWrite(MO2, HIGH);
analogWrite(PWM, 110);
}
}
void movebackward() {
int ldrValNow1;
ldrValNow1 = analogRead(LDR1);
if (ldrValNow1 > lightSensitivity1) {
if (LDRValue1 <= lightSensitivity1) {
digitalWrite(MO1, LOW);
digitalWrite(MO2, LOW);
previousMillis = millis();
}
else if (millis() - previousMillis > interval) {
digitalWrite(MO1, LOW);
LightSensorCounter++;
}
}
else {
digitalWrite(MO1, HIGH);
digitalWrite(MO2, LOW);
analogWrite(PWM, 110);
}
LDRValue1 = ldrValNow1;
}
Now i want to use this with a master and slave arduino connected with Bluetooth.I want to build one arduino on the verhicle so this is driving the DC motor ( BTW i use a L298 H brigde with external power). The other arduino is for stop and turnig the verhicle with to LDR sensors.
Now my problem is that i need to split the code. Maybe someone can tell me how to split this code so it works with bluetooth
This is what i already try to do Master code
//***************Master*******************
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define LDR A0
#define LDR1 A1
int state = 0;
int LDRValue = 0;
int LDRValue1 = 0;
int LightSensorCounter = 1;
unsigned long previousMillis = 0;
int lightSensitivity = 500;
int lightSensitivity1 = 500;
int interval = 3000;
void setup()
{
Serial.begin(9600);
//pinMode(LDR, INPUT);
//pinMode(LDR1, INPUT);
Serial.begin(9600);
BTSerial.begin(38400);
}
void loop() {
moveforward();
if (BTSerial.available() > 0) {
state = BTSerial.read();
}
}
// Data sending Part
void moveforward()
{
int ldrValNow;
ldrValNow = analogRead(LDR);
if (ldrValNow > lightSensitivity) {
if (LDRValue <= lightSensitivity) {
LightSensorCounter++;
}
}
LDRValue = ldrValNow;
if (LightSensorCounter % 2 == 0) {
movebackward();
}
else {
BTSerial.write('2');
}
}
void movebackward() {
int ldrValNow1;
ldrValNow1 = analogRead(LDR1);
if (ldrValNow1 > lightSensitivity1) {
if (LDRValue1 <= lightSensitivity1) {
BTSerial.write('1');
previousMillis = millis();
}
else if (millis() - previousMillis > interval) {
BTSerial.write('1');
LightSensorCounter++;
}
}
else {
BTSerial.write('0');
}
LDRValue1 = ldrValNow1;
}
Slave code
///*******************Slave*****************
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define MO1 3
#define MO2 4
#define PWM 5
int state = 0;
void setup() {
pinMode(MO1, OUTPUT);
pinMode(MO2, OUTPUT);
pinMode(PWM, OUTPUT);
Serial.begin(9600);
BTSerial.begin(38400);
}
void loop() {
if (BTSerial.available() > 0) {
state = BTSerial.read();
}
// data receiving part
if (state == '2'){
digitalWrite(MO1, LOW);
digitalWrite(MO2, HIGH);
analogWrite(PWM, 120);
}
if (state == '1')
{
digitalWrite(MO1, LOW);
digitalWrite(MO2, LOW);
}
else if (state == '1')
{
digitalWrite(MO1, LOW);
}
if (state == '0')
{
digitalWrite(MO1, HIGH);
digitalWrite(MO2, LOW);
analogWrite(PWM, 120);
}
}