Install Dabble Arduino/ESP32 library into Arduino IDE
/*
Dabble program for proportional control using Ipad acceleration sensors
*/
#define CUSTOM_SETTINGS
#define INCLUDE_SENSOR_MODULE
#include <DabbleESP32.h>
//Assign pins for left motor
int enableleftPin = 25; // EN1 on L298N
int motorleftPin1 = 32; // IN1 on L298N
int motorleftPin2 = 12; // IN2 on L298N
//Assign pins for right motor
int enablerightPin = 33; // EN2 on L298N
int motorrightPin1 = 18; // IN3 on L298N
int motorrightPin2 = 4; // IN4 on L298N
int xaxis=0;
int yaxis=0;
void setup() {
Serial.begin(115200); // make sure your Serial Monitor is also set at this baud rate.
Dabble.begin("JonathansEsp32"); //set bluetooth name of your device
pinMode(motorleftPin1, OUTPUT);
pinMode(motorleftPin2, OUTPUT);
pinMode(enableleftPin, OUTPUT);
pinMode(motorrightPin1, OUTPUT);
pinMode(motorrightPin2, OUTPUT);
pinMode(enablerightPin, OUTPUT);
}
void loop() {
settings();
if( xaxis> 120){
forward(); } else
{
reverse();
}
Dabble.processInput(); //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
}
void settings ()
{
xaxis= map(Sensor.getAccelerometerXaxis(),-9,9,-255,255);
yaxis= map(Sensor.getAccelerometerYaxis(), -9,9,-255,255);
Serial.println(xaxis);
Serial.println(yaxis);
}
void forward()
{
analogWrite(enableleftPin, xaxis- yaxis);
analogWrite(enablerightPin, xaxis+ yaxis);
digitalWrite( motorleftPin1, HIGH);
digitalWrite( motorleftPin2, LOW);
digitalWrite( motorrightPin1, HIGH);
digitalWrite( motorrightPin2, LOW);
}
void reverse()
{
analogWrite(enableleftPin, -xaxis - yaxis);
analogWrite(enablerightPin, -xaxis + yaxis);
digitalWrite( motorleftPin1, LOW);
digitalWrite( motorleftPin2, HIGH);
digitalWrite( motorrightPin1, LOW);
digitalWrite( motorrightPin2, HIGH);
}