i tried running my code but it either cant shows the reading of my sweep scanse or it shows the reading of the sweep scanse but i am unable to control it using my bluetooth module HC06. i'm trying to programme the irobot create to do the what the bluetooth commends and also able to make it run autonomous while avoiding obstacle using the sweep scanse
the attachment below is my code
#include<SoftwareSerial.h>
#define rxPin 10
#define txPin 11
#include "roombaDefines.h"
#include <Sweep.h>
unsigned long previousMillisBT1=0;
unsigned long previousMillisSweep=0;
const unsigned long intervalBT1 = 500;
const unsigned long intervalSweep = 1000;
SoftwareSerial softSerial = SoftwareSerial(rxPin,txPin);
SoftwareSerial BT1(12, 13);
Sweep device(Serial1);
uint8_t scanCount = 0;
uint16_t sampleCount = 0;
bool syncValues[500];
uint16_t distances[500];
uint16_t angle[500];
void setup() {
delay(1000);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
BT1.begin(9600);
BT1.println("Hello from Arduino");
softSerial.begin(57600);
Serial.begin(57600);
softSerial.write(128);
softSerial.write(131);
Serial1.begin(115200);
Serial.println("Display numbers:");
}
char c;
void loop() {
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillisBT1) >= intervalBT1) {
BT1.listen();
if(BT1.available() > 0){
c = BT1.read();
movement(c);
}
previousMillisBT1 = currentMillis;
}
if ((unsigned long)(currentMillis - previousMillisSweep) >= intervalSweep) {
device.setMotorSpeed(MOTOR_SPEED_CODE_5_HZ);
device.startScanning();
printCollectedData();
previousMillisSweep = currentMillis;
}
}
i even tried using millis() to divide the loop into 2 activities running together
void gatherSensorReading()
{
// attempt to get the next scan packet
// Note: getReading() will write values into the "reading" variable
bool success = false;
ScanPacket reading = device.getReading(success);
if (success)
{
// store the info for this sample
syncValues[sampleCount] = reading.isSync();
distances[sampleCount] = reading.getDistanceCentimeters();
angle[sampleCount] = reading.getAngleDegrees();
// increment sample count
sampleCount++;
//Serial.println(sampleCount);
}
}
void printCollectedData()
{
gatherSensorReading();
int indexOfFirstSyncReading = 0;
// don't print the trailing readings from the first partial scan
while (!syncValues[indexOfFirstSyncReading])
{
indexOfFirstSyncReading++;
}
// print the readings for all the complete scans
for (int i = indexOfFirstSyncReading; i < sampleCount; i++)
{
Serial.println(String(distances[i]) + "," + String(angle[i], 3));
delay(1000);
}
}
this is the code for the sweep
void movement(char c)
{
Serial.print(c);
switch(c)
{
case'w':
driveWheels(200,200);
state = c;
BT1.println(" Moving forward ");
break;
case'd':
driveWheels(-100,100);
BT1.println(" Turning right ");
break;
case'a':
driveWheels(100,-100);
BT1.println(" Turning left ");
break;
case's':
driveWheels(-200,-200);
state = c;
BT1.println(" Moving backward ");
break;
case'b':
driveStop();
BT1.println(" Stop ");
break;
case'p':
BT1.println("Autonomous");
while(distances[sampleCount]<100 && angle[sampleCount]> 45 && angle[sampleCount]< 135){
turnCW(20,15);
}
driveWheels(50,50);
state = c;
}
}
this is the code for the movement
