Alright, so my project is an electric skateboard. I have the remote and the skateboard that have Arduino's.
The remote sends a value to the skate that it reads from it's analog input.
Code for the remote:
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <Servo.h>
SoftwareSerial BTserial(A4, A3); // RX | TX
const byte BTpin = A5;
// HC05 TX vers Arduino pin A4 RX.
// HC05 RX vers Arduino pin A3 TX avec diviseur de tension.
// HC05 STATE pin vers Arduino pin A5.
// BTconnected = false lorsque pas connecte
boolean BTconnected = false;
char c = ' ';
int t1,t2,pileskate,rpm,valeurpot;
int potentiometre = A0; // potentiometre pin A0
String readString;
void setup() {
// BTpin set input
pinMode(BTpin, INPUT);
// serial to computer
Serial.begin(9600);
Serial.println("Remote is powered on \n");
Serial.println("Connect remote to skateboard\n");
// att connection bluetooth
while (!BTconnected)
{
if ( digitalRead(BTpin)==HIGH) { BTconnected = true;};
}
Serial.println("Remote is connected to skate\n");
//comm with HC05
BTserial.begin(57600);
}
void loop() {
//if remote is not connected to skate
if (digitalRead(BTpin)==LOW) {
BTconnected = false;
Serial.print("Please connect remote to skate\n");
while (!BTconnected)
{
if ( digitalRead(BTpin)==HIGH) { BTconnected = true;};
}
Serial.println("Remote is connected to skate");
BTconnected = true;
}
valeurpot = analogRead(potentiometre);
valeurpot=map(valeurpot,0,1023,1000,2000); // convert 0-1023 to 1000-2000
//Serial.println(valeurpot);
BTserial.write(valeurpot);
BTserial.write("a,");
delay(150);
if (BTserial.available()) {
char c = BTserial.read(); //gets one byte from serial buffer
if (c == ',') { //delimited ',' string parse
if (readString.length() >1) {
int n = readString.toInt(); //convert readString into a number
if(readString.indexOf('b') >0) {
rpm=n;
Serial.print("rpm= ");
Serial.println(rpm);
}
if(readString.indexOf('c') >0) {
pileskate=n;
Serial.print("Pile skate = ");
Serial.println(pileskate);
}
if(readString.indexOf('d') >0) {
t1=n;
Serial.print("t1= ");
Serial.println(t1);
}
if(readString.indexOf('e') >0) {
t2=n;
Serial.print("t2= ");
Serial.println(t2);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
Here's the code for the skate:
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <Servo.h>
SoftwareSerial BTserial(A4, A3); // RX | TX
const byte BTpin = A5;
// HC05 TX vers Arduino pin A4 RX.
// HC05 RX vers Arduino pin A3 TX avec diviseur de tension.
// HC05 STATE pin vers Arduino pin A5.
// BTconnected = false lorsque pas connecte
boolean BTconnected = false;
char c = ' ';
int period;
Servo ESC; //define name regulateur de vitesse
String readString;
void setup() {
// BTpin set input
pinMode(BTpin, INPUT);
// serial to computer
Serial.begin(9600);
Serial.println("Skate is powered on \n");
Serial.println("Connect remote \n");
// wait BT connect
while (!BTconnected)
{
if ( digitalRead(BTpin)==HIGH) { BTconnected = true;};
}
Serial.println("Remote is connected \n");
//comm with HC05
BTserial.begin(57600);
//set ESC
ESC.attach(9); //ESC to pin 9
ESC.writeMicroseconds(1000); //set speed to 0
}
void loop() {
//if remote is not connected to skate
if ( digitalRead(BTpin)==LOW) {
BTconnected = false;
ESC.writeMicroseconds(1000);
Serial.print("Please connect remote \n");
while (!BTconnected)
{
if ( digitalRead(BTpin)==HIGH) { BTconnected = true;};
}
Serial.println("Remote is connected");
BTconnected = true;
}
if (BTserial.available()) {
char c = BTserial.read(); //gets one byte from serial buffer
if (c == ',') { //delimited ',' string parse
if (readString.length() >1) {
int n = readString.toInt(); //convert readString into a number
if(readString.indexOf('a') >0) {
ESC.writeMicroseconds(n); //ESC.write(n); //angle
period=n;
Serial.print("ESC= ");
Serial.println(period);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
In the serial monitor, all I see is ESC = 0;
So in short, I've used (which didn't work):
BTserial.write(valeurpot);
BTserial.write("a,");
Then I've used only(which still showed ESC = 0):
BTserial.write("120a,");
Then I've added a delay of 150ms (and it showed ESC = 120):
BTserial.write("120a,");
delay(150);
But this still doesnt work:
BTserial.write(valeurpot);
BTserial.write("a,");
delay(150);