Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ set(srcs

set(requires "bluepad32" "bluepad32_arduino" "arduino" "btstack" "callbacks")

idf_component_register(SRCS "${srcs}"
idf_component_register(SRCS "LineSensor.cpp" "MotorMovements.cpp" "${srcs}"
INCLUDE_DIRS "."
REQUIRES "${requires}")
27 changes: 27 additions & 0 deletions main/LineSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "sdkconfig.h"
#include <Arduino.h>

#include <QTRSensors.h>

QTRSensors qtr;
uint16_t sensors[2];

void setup() {
// set up Serial Communication and sensor pins
Serial.begin(115200);
qtr.setTypeAnalog(); // or setTypeAnalog()
qtr.setSensorPins((const uint8_t[]) {32, 33}, 2); // pin numbers go in the curly brackets {}, and number of sensors in use goes after

// calibration sequence
for (uint8_t i = 0; i < 250; i++) {
Console.printf("calibrating %d/250\n", i); // 250 is the number of calibrations recommended by manufacturer
qtr.calibrate();
delay(20);
}
}

void loop() {
qtr.readLineBlack(sensors); // Get calibrated sensor values returned into sensors[]
Console.printf("S1: %d S2: %d\n", sensors[0], sensors[1]);
delay(250);
}
64 changes: 64 additions & 0 deletions main/MotorMovements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "sdkconfig.h"
#include <Arduino.h>
#include <ESP32Servo.h>
#include <Bluepad32.h>
#include <uni.h>
#include "controller_callbacks.h"
#include "MotorMovements.h"

#define IN1 16 // Control pin 1
#define IN2 17 // Control pin 2
#define IN3 22 // Control pin 3
#define IN4 23 // Control pin 4

extern ControllerPtr myControllers[BP32_MAX_GAMEPADS]; // BP32 library allows for up to 4 concurrent controller connections, but we only need 1

void GoForward(int duty){

analogWrite(IN1,LOW);
digitalWrite(IN2,duty);
digitalWrite(IN3,duty);
analogWrite(IN4,LOW);

}

void GoBackward(int duty){

digitalWrite(IN1,duty);
digitalWrite(IN2, LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,duty);

}

void GoLeft(int duty){

digitalWrite(IN1,LOW);
digitalWrite(IN2,LOtW);
digitalWrite(IN3,duty);
digitalWrite(IN4,LOW);


}

void GoRight(int duty){

digitalWrite(IN1,LOW);
digitalWrite(IN2,duty);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}

void Stop(){

digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);


}




23 changes: 23 additions & 0 deletions main/MotorMovements.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "sdkconfig.h"
#include <Arduino.h>
#include <ESP32Servo.h>
#include <Bluepad32.h>
#include <uni.h>
#include "controller_callbacks.h"

#define IN1 16 // Control pin 1
#define IN2 17 // Control pin 2
#define IN3 22 // Control pin 3
#define IN4 23 // Control pin 4

extern ControllerPtr myControllers[BP32_MAX_GAMEPADS]; // BP32 library allows for up to 4 concurrent controller connections, but we only need 1

void GoForward(int duty);

void GoBackward(int duty);

void GoLeft(int duty);

void GoRight(int duty);

void Stop();
66 changes: 61 additions & 5 deletions main/arduino_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

#include "sdkconfig.h"
#include <Arduino.h>
#include <ESP32Servo.h>
#include <Bluepad32.h>
#include <uni.h>
#include "controller_callbacks.h"
#include "MotorMovements.h"

#define IN1 16 // Control pin 1
#define IN2 17 // Control pin 2
#define IN3 22 // Control pin 3
#define IN4 23 // Control pin 4



extern ControllerPtr myControllers[BP32_MAX_GAMEPADS]; // BP32 library allows for up to 4 concurrent controller connections, but we only need 1

Expand All @@ -29,26 +38,73 @@ void dumpGamepad(ControllerPtr ctl) {
);
}

Servo myServo;


void setup() {

Serial.begin(115200);

BP32.setup(&onConnectedController, &onDisconnectedController);
BP32.forgetBluetoothKeys();
esp_log_level_set("gpio", ESP_LOG_ERROR); // Suppress info log spam from gpio_isr_service
uni_bt_allowlist_set_enabled(true);
// myServo.attach(12);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}

void loop() {

vTaskDelay(1); // Ensures WDT does not get triggered when no controller is connected


// myServo.write(0); // Rotate to 0 degrees
// delay(1000); // Delay 1000 ms
// myServo.write(180); // Rotate to 180 degrees
// delay(1000);

BP32.update();
for (auto myController : myControllers) { // Only execute code when controller is connected
if (myController && myController->isConnected() && myController->hasData()) {

/*
====================
Your code goes here!
====================
*/
if(myController->axisY() < -200){ // go forward

GoForward(255);

delay(250); // Run for .25 second

}
else if(myController->axisY() > 200){ // go backward

GoBackward(255);

delay(250); // Run for .25 second


}
else if(myController->axisX() < -200){ // turn left

GoLeft(255);

delay(250); // Run for .25 second


}
else if(myController->axisX() > 200){ // turn right

GoRight(255);

delay(250); // Run for .25 second

}



dumpGamepad(myController); // Prints the gamepad state, delete or comment if don't need
}
}
vTaskDelay(1); // Yield CPU to not starve other ESP32 processes and cause WDT reset
}